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?

0

2 Answers 2

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}
3
  • 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 9, 2021 at 8: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 9, 2021 at 8:55
  • yes, you are right, edited. Mar 9, 2021 at 9: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

4
  • You mentioned "In the next version", will "Solution 2" still be available by then?
    – Jinwen
    Mar 9, 2021 at 9:07
  • Solutions 1 and 2 are available now if your fancyhdr.sty is up to date. Mar 9, 2021 at 10:46
  • I mean, solution 2 will not break with the future version, is that correct?
    – Jinwen
    Mar 9, 2021 at 10:49
  • Yes, the future version will use a new command. So existing code will remain working. Mar 9, 2021 at 11:30

You must log in to answer this question.

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