4

Consider the following example, if the plain page style has been modified with \fancypagestyle, then \pagestyle{fancy} doesn't work if one has applied \pagestyle{plain} before.

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\rightmark}
\fancyhead[L]{\leftmark}
\fancypagestyle{plain}{\fancyhf{}}

\begin{document}

\pagestyle{plain} % with this, '\pagestyle{fancy}' below doesn't work \section{Test}

\clearpage \pagestyle{fancy} % doesn't work \subsection{test}

\end{document}

Page two should be like

enter image description here

But now it is

enter image description here

How can I fix this?

Jinwen
  • 8,518

2 Answers2

6

The fancy page style takes by default its settings from the environment. So it can change if you change some header. As mentioned in the documentation you can use the style fancydefault instead:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
%
  \fancyhf{}
  \fancyhead[R]{\rightmark}
  \fancyhead[L]{\leftmark}

\fancypagestyle{plain}{\fancyhf{}}

\begin{document}

\pagestyle{plain} % \section{Test}

\clearpage \pagestyle{fancydefault} % \subsection{test}

\end{document}

The alternativ is to define fancy with \fancypagestyle too:

\documentclass{article}
\usepackage{fancyhdr}
\fancypagestyle{fancy}{%
  \fancyhf{}%
  \fancyhead[R]{\rightmark}%
  \fancyhead[L]{\leftmark}}
\pagestyle{fancy}
\fancypagestyle{plain}{\fancyhf{}}

\begin{document}

\pagestyle{plain} % \section{Test}

\clearpage \pagestyle{fancy} % \subsection{test}

\end{document}

Ulrike Fischer
  • 327,261
  • Thanks for this! I've been making this mistake since the first day I encountered with fancyhdr, and it is not until now that I realize that it may cause trouble. – Jinwen Mar 09 '21 at 08:42
  • Perhaps \pagestyle{fancy} should be placed after \fancypagestyle{fancy}? \fancyhf{} doesn't seem to get executed if \pagestyle{fancy} is in front of \fancypagestyle{fancy}. – Jinwen Mar 09 '21 at 08:55
  • yes, you are right, edited. – Ulrike Fischer Mar 09 '21 at 09:00
4

Unfortunately, this is how \pagestyle works. \pagestyle{plain} executes all the commands given in its defintion (in addition to the fancyhdr setup), so \fancyhf{} is executed in the global context.

A later \pagestyle{fancy} doesn't undo this. Only when the \pagestyle{plain} is given in a local context, its changes are restricted to that context. This happens for example with \thispagestyle, or when you give \pagestyle{} in a group (see solution 1). Otherwise you must put the header definitions explicitely in pagestyle fancy (see solution 2).

In the next version of fancyhdr I will have a better solution for this, but for now you can use one of the solutions below.

Solution 1: Use a group. Note the \clearpage must be inside the group, otherwise the original headers will leak into the previous page.

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\rightmark}
\fancyhead[L]{\leftmark}
\fancypagestyle{plain}{\fancyhf{}}

\begin{document} \begingroup \pagestyle{plain} % with this, '\pagestyle{fancy}' below doesn't work \section{Test}

\clearpage \endgroup \pagestyle{fancy} % doesn't work \subsection{test}

\end{document}

Solution 2: Put the header definitons inside the pagestyle fancy. Note This will only work in fancyhdr version 4, otherwise you will have to use a different pagestyle name.

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancypagestyle{fancy}{
  \fancyhead[R]{\rightmark}
  \fancyhead[L]{\leftmark}
}
\fancypagestyle{plain}{\fancyhf{}}

\begin{document}

\pagestyle{plain} % with this, '\pagestyle{fancy}' below doesn't work \section{Test}

\clearpage \pagestyle{fancy} % doesn't work \subsection{test}

\end{document}

enter image description here