Page Layoutfancyhdr | Book Title in even Headers, Part Name on odd

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
Stevenet
Posts: 13
Joined: Sun Oct 30, 2011 6:34 pm

fancyhdr | Book Title in even Headers, Part Name on odd

Post by Stevenet »

Hi,

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,
Last edited by Stevenet on Mon Nov 07, 2011 4:08 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

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

Post by localghost »

It could be done by a supplement to some helper macros for the \part command.

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}
This way you get a command \parttitle which you can use wherever needed.


Best regards and welcome to the board
Thorsten
Stevenet
Posts: 13
Joined: Sun Oct 30, 2011 6:34 pm

Re: fancyhdr | Book Title in even Headers, Part Name on odd

Post by Stevenet »

Well, i'm trying something like this;

\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,
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

fancyhdr | Book Title in even Headers, Part Name on odd

Post by localghost »

Stevenet wrote:[…] There's a way (on this way please) to get the part name at even headers? […]
You only have to combine my proposal with your page layout.

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}
Are you aware that your approach (integrated into the above code) puts the book title on odd pages and the part name on even? That contradicts what you originally asked for in your initial post.

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.
Stevenet
Posts: 13
Joined: Sun Oct 30, 2011 6:34 pm

Re: fancyhdr | Book Title in even Headers, Part Name on odd

Post by Stevenet »

You right! Thank you very much, your code fits perfectly what i need.

Next time i'll put the code, using the tag.

Thank you again!

Steve,
Stevenet
Posts: 13
Joined: Sun Oct 30, 2011 6:34 pm

fancyhdr | Book Title in even Headers, Part Name on odd

Post by Stevenet »

Hi again,

Two questions more;
  1. 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?
  2. 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?
thanks again,

Steve,
Last edited by Stevenet on Mon Nov 07, 2011 4:07 pm, edited 1 time in total.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

fancyhdr | Book Title in even Headers, Part Name on odd

Post by localghost »

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? […]
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:[…] 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? […]
Why do you force line breaks in chapter headings? The automatic line break should work well.

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}
Stevenet
Posts: 13
Joined: Sun Oct 30, 2011 6:34 pm

Re: fancyhdr | Book Title in even Headers, Part Name on odd

Post by Stevenet »

Thanks again. The question of blank pages is perfectly solved!

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,
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

fancyhdr | Book Title in even Headers, Part Name on odd

Post by localghost »

The \\ command for manual line breaks accepts an optional argument where you can insert valid length values. You could try something like this.

Code: Select all

\chapter{Research investment: \\[-0.5ex] The regional potential}
The final adjustment is up to you. Remember that this line break will also occur in the ToC entry for the concerned chapter. So you might want to have a manual line break for the actual heading and an untouched ToC entry.

Code: Select all

\chapter[Research investment: The regional potential]{Research investment: \\[-0.5ex] The regional potential}
The optional argument also goes into the headers and so does not cause them to be messed up.
Stevenet
Posts: 13
Joined: Sun Oct 30, 2011 6:34 pm

Re: fancyhdr | Book Title in even Headers, Part Name on odd

Post by Stevenet »

Again, thanks a lot. Now titles spacing are more compressed and looks optimal for our book.

best regards,

Steve,
Post Reply