Page Layout ⇒ fancyhdr | Book Title in even Headers, Part Name on odd
fancyhdr | Book Title in even Headers, Part Name on odd
I would to get the title name (book) at even headers (left), and part name at odd headers (right).
I try to get it using the fancyhdr package; There's no problem for chapters or sections, but I'm unable to put the part name at right page headers.
I'm using a book class.
How can i solve it?
Thank you for your time,
Steve,
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
fancyhdr | Book Title in even Headers, Part Name on odd
Code: Select all
\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\makeatletter
\def\@part[#1]#2{%
\gdef\parttitle{#1}
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\bfseries \partname\nobreakspace\thepart
\par
\vskip 20\p@
\fi
\Huge \bfseries #2\par}%
\@endpart
}
\def\@spart#1{%
\gdef\parttitle{#1}
{\centering
\interlinepenalty \@M
\normalfont
\Huge \bfseries #1\par}%
\@endpart
}
\makeatother
\begin{document}
\part{Bar}
\parttitle
\part[Foo]{Bar}
\parttitle
\end{document}
Best regards and welcome to the board
Thorsten
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Re: fancyhdr | Book Title in even Headers, Part Name on odd
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[RO,LE]{\small\thepage}
\fancyhead[LO]{\small 'the book title'}% odd page header and number to right top
\fancyhead[RE]{\small @part }%Even page header and number at left top
\fancyfoot[L,R,C]{}
But, only puts the odd pages. There's a way (on this way please) to get the part name at even headers?
thank you,
Steve,
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
fancyhdr | Book Title in even Headers, Part Name on odd
You only have to combine my proposal with your page layout.Stevenet wrote:[…] There's a way (on this way please) to get the part name at even headers? […]
Code: Select all
\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\title{The Book Title}
\author{The Author}
\makeatletter
\let\thetitle\@title % Document title saved in command
\let\theauthor\@author % Document author saved in command
\def\@part[#1]#2{%
\gdef\parttitle{#1}
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\bfseries \partname\nobreakspace\thepart
\par
\vskip 20\p@
\fi
\Huge \bfseries #2\par}%
\@endpart
}
\def\@spart#1{%
\gdef\parttitle{#1}
{\centering
\interlinepenalty \@M
\normalfont
\Huge \bfseries #1\par}%
\@endpart
}
\makeatother
\fancyhf{}
\fancyhead[RO,LE]{\small\thepage}
\fancyhead[LO]{\small\thetitle} % Odd page header and number to right top
\fancyhead[RE]{\small\parttitle} % Even page header and number at left top
\pagestyle{fancy}
\begin{document}
\part{Bar}
\parttitle
\part[Foo]{Bar}
\parttitle
\end{document}
And please use the »Code« button when posting code to tag it as such (as you can see it in my posts). It just keeps your posts clear and legible.
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Re: fancyhdr | Book Title in even Headers, Part Name on odd
Next time i'll put the code, using the tag.
Thank you again!
Steve,
fancyhdr | Book Title in even Headers, Part Name on odd
Two questions more;
- On the same book, (using now the above fancyhdr headers) i need that "last pages"; this is the blank even pages before new chapter-starting odd pages, will show really empty, so if at this last-even pages, if there's no content, should miss the headers. I'm actually using an \input for each chapter, so every chapter is a file inserted. How can i improve this?
- I have some long chapter titles, that are 'newlined' with \\. Although, note that the spacing between lines, is too much for a title. How con 'compress' the spacing between lines on chapter titles?
Steve,
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
fancyhdr | Book Title in even Headers, Part Name on odd
Section 15 of the fancyhdr manual suggests to redefine the \cleardoublepage command. A diverting to achieve what you want is introduced in the below code (for more clarity again the complete example as given earlier).Stevenet wrote:[…] On the same book, (using now the above fancyhdr headers) i need that "last pages"; this is the blank even pages before new chapter-starting odd pages, will show really empty, so if at this last-even pages, if there's no content, should miss the headers. I'm actually using an \input for each chapter, so every chapter is a file inserted. How can i improve this? […]
Why do you force line breaks in chapter headings? The automatic line break should work well.Stevenet wrote:[…] I have some long chapter titles, that are 'newlined' with \\. Although, note that the spacing between lines, is too much for a title. How con 'compress' the spacing between lines on chapter titles? […]
Code: Select all
\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\title{The Book Title}
\author{The Author}
\makeatletter
\let\thetitle\@title % Document title saved in command
\let\theauthor\@author % Document author saved in command
\def\@part[#1]#2{%
\gdef\parttitle{#1}
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\markboth{}{}%
{\centering
\interlinepenalty \@M
\normalfont
\ifnum \c@secnumdepth >-2\relax
\huge\bfseries \partname\nobreakspace\thepart
\par
\vskip 20\p@
\fi
\Huge \bfseries #2\par}%
\@endpart
}
\def\@spart#1{%
\gdef\parttitle{#1}
{\centering
\interlinepenalty \@M
\normalfont
\Huge \bfseries #1\par}%
\@endpart
}
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
\hbox{}
\thispagestyle{empty}
\newpage
\if@twocolumn\hbox{}\newpage\fi\fi\fi}
\makeatother
\fancyhf{}
\fancyhead[RO,LE]{\small\thepage}
\fancyhead[LO]{\small\thetitle} % Odd page header and number to right top
\fancyhead[RE]{\small\parttitle} % Even page header and number at left top
\pagestyle{fancy}
\begin{document}
\part{Foo}
\chapter{Foo}
\chapter{Bar}
\end{document}
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Re: fancyhdr | Book Title in even Headers, Part Name on odd
The question of titles spacing; Well, sometimes in order to better explain the chapter's content, could be better to force line breaks in titles (ie when a phrase starts with something like 'Research investment: \\ The regional potential'). But when i do that... i get an over spacing between lines...
thank you for your time,
Steve,
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
fancyhdr | Book Title in even Headers, Part Name on odd
Code: Select all
\chapter{Research investment: \\[-0.5ex] The regional potential}
Code: Select all
\chapter[Research investment: The regional potential]{Research investment: \\[-0.5ex] The regional potential}
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Re: fancyhdr | Book Title in even Headers, Part Name on odd
best regards,
Steve,