1

The following code from Automatically remove ruler when no header is present compiles fine in Tex Live 2016:

\documentclass{article}
\usepackage{fancyhdr}
    \makeatletter
    \let\org@fancyhead\@fancyhead
    \renewcommand*{\@fancyhead}[5]{\sbox0{#2#3#4}\ifdim\wd0=\z@ \let\headrule\relax \fi \org@fancyhead{#1}{#2}{#3}{#4}{#5}} % remove ruler if header is empty
    \makeatother

\begin{document}
Foo
\end{document}

But in Tex Live 2017, it gives the error LaTeX Error: \@fancyhead undefined. Why? And how can I fix this?

Alan Munn
  • 218,180
Sverre
  • 20,729
  • 2
    This is the danger of depending on internal macros of a package. The \@fancyhead macro has been renamed \f@nch@head (and a lot of other similar macros have also been renamed.) – Alan Munn Jun 20 '17 at 18:57

1 Answers1

4

For reasons that aren't documented in the change history, lots of internal macros in fancyhdr have changed names. This is one of the dangers of depending on code that modifies such internal macros as opposed to modifying user facing macros. In this particular case, \@fancyhead is now \f@nch@head. So the following code works:

\documentclass{article}
\usepackage{lipsum}
\usepackage{fancyhdr}
\makeatletter
\let\org@fancyhead\f@nch@head
\renewcommand*{\f@nch@head}[5]{\sbox0{#2#3#4}\ifdim\wd0=\z@ \let\headrule\relax \fi \org@fancyhead{#1}{#2}{#3}{#4}{#5}} % remove ruler if header is empty
\makeatother
\pagestyle{fancy}
\fancyhead{}
\begin{document}
\section{My first section}
\lipsum
\newpage
\fancyhead[C]{Header is set}
\section{My next section}
\lipsum
\end{document}
Alan Munn
  • 218,180