0

Is it possible to define multiple page style using fancyhdr package, for toc and for the document, setting also in a different mode the plain pages for toc and the plain pages for the document.

This is my code:

\documentclass[a4paper,12pt,oneside]{report}

\usepackage[utf8]{inputenc}

\usepackage{fancyhdr}
\usepackage{blindtext}
\usepackage{lastpage}

\fancypagestyle{document}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage\ |\ \pageref{LastPage}}
}

\fancypagestyle{plain}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage\ |\ \pageref{LastPage}}
}

\fancypagestyle{tocstyle}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage}
}

\begin{document}\sloppy
    
    \pagenumbering{Roman}
    \pagestyle{tocstyle}
    \tableofcontents
    \listoffigures
    \listoftables  
    \clearpage 
    
    \pagenumbering{arabic}
    \pagestyle{document}
    
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    
\end{document}

The document is formatted well, but not the toc, because plain pages of the toc are affected by having the same arrangement of the document pages.

Is there a way to define a complete page style for different document sections, or to suppress the fact of having "plain" pages?

1 Answer 1

1

The first pages of ToC, LoF, LoT is always pagestyle plain, like all other first chapter pages. This causes the chosen page style tocstyle not to be effective on these first pages.

Update: Added a solution with the plain definitions inside the other pagestyles.

  1. One solution this would be to redefine plain to be the same as tocstyle before the ToC etc. and then redefine it to be the desired plain style for the document after the \clearpage that separates these ToC pages from the main document. A comprehensive way to do this would be to include the \fancypagestyle{plain}... inside the other \fancypagestyle definitions, thereby bundling them up with these page styles. See Solution 1.
  2. Another way is to inject an additional \thispagestyle{tocstyle} in the ToC, LoF and LoT with commands like
\addtocontents{toc}{\protect\thispagestyle{tocstyle}}

This needs an additional LaTeX run to get effectuated. See Solution 2.

Here are the complete solutions with these ideas. For demonstration purposes I have added the name of the page style in the left footer so that you can clearly see where it comes from.

Solution 1:

\documentclass[a4paper,12pt,oneside]{report}

\usepackage[utf8]{inputenc}

\usepackage{fancyhdr}
\usepackage{blindtext}
\usepackage{lastpage}

\fancypagestyle{document}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage\ |\ \pageref{LastPage}}
  \fancyfoot[L]{document}
  \fancypagestyle{plain}{%
    \fancyhf{}
    \renewcommand{\headrulewidth}{0pt}
    \fancyfoot[R]{\thepage\ |\ \pageref{LastPage}}
    \fancyfoot[L]{plain-document}
  }
}

\fancypagestyle{tocstyle}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage}
  \fancyfoot[L]{tocstyle}
  % Make plain pagestyle the same as tocstyle
  % The optional argument needs fancyhdr version 4.0 or later.
  % Otherwise you have to copy all the definitions from tocstyle.
  \fancypagestyle{plain}[tocstyle]{\fancyfoot[L]{plain-tocstyle}} 
}

\begin{document}\sloppy
    \pagenumbering{Roman}
    \pagestyle{tocstyle}
    \tableofcontents
    \listoffigures
    \listoftables  
    \clearpage 
    
    \pagenumbering{arabic}
    \pagestyle{document}
    
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    
\end{document}

Solution 2:

\documentclass[a4paper,12pt,oneside]{report}

\usepackage[utf8]{inputenc}

\usepackage{fancyhdr}
\usepackage{blindtext}
\usepackage{lastpage}

\fancypagestyle{document}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage\ |\ \pageref{LastPage}}
  \fancyfoot[L]{document}
}

\fancypagestyle{plain}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage\ |\ \pageref{LastPage}}
  \fancyfoot[L]{plain}
}

\fancypagestyle{tocstyle}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{\thepage}
  \fancyfoot[L]{tocstyle}
}

\begin{document}\sloppy
    \pagenumbering{Roman}
    \pagestyle{tocstyle}
    \addtocontents{toc}{\protect\thispagestyle{tocstyle}}
    \addtocontents{lot}{\protect\thispagestyle{tocstyle}}
    \addtocontents{lof}{\protect\thispagestyle{tocstyle}}
    \tableofcontents
    \listoffigures
    \listoftables  
    \clearpage 
    
    \pagenumbering{arabic}
    \pagestyle{document}
    
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    
\end{document}

enter image description here

5
  • Thank you for your reply @Pieter. My undestanding is that I can define multiple page style but I can have only one plain style at time. Normally I like to define the structure of my documents in a separated file (as well as foot notes, captions, etc.) but in this case the only chance I have is to inject the style as you shown me or redefine plain style when it is needed. Thanks again. Dec 30, 2021 at 21:21
  • Yes, you can have only one plain page style at a time. That applies also to other page styles. However, you can put the \fancypagestyle{plain}{....} inside the other \fancypagestyle definitions, thereby "bundling them up". Jan 1, 2022 at 9:43
  • Dear @Pieter your last comment saved my day! it was exactly what I was looking for: defining and embedding a plain page style for each defined page style. I change my code putting every customized \fancypagestyle{plain}{...} inside every\fancypagestyle definitions as you suggested me. I owe you. Jan 2, 2022 at 10:46
  • I will update my answer to include this solution (and I am going to put this in the manual too). Jan 2, 2022 at 16:00
  • Surely adding the plain style inside a custom page style definition is more redundant (because I think that usually it is needed to replicate the normal page style also for plain pages) but once you put your layout definition in a separated files, I guess the main code is more readable and simpler (anyway, injecting the style in toc, tof and tot is not a big deal) Jan 2, 2022 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .