[Tex/LaTex] Cross-references in headers, clash between fancyhdr and hyperref

cross-referencingfancyhdrheader-footerhyperref

I am writing my thesis and using fancyhdr for the headers. Just the standard "Chapter name on odd pages and section name on even pages".
I also need to use the hyperref package to break up long urls in my bibliography.
The problem I have is that when I use this package (and breakurl), the cross-references in the header appear as question marks. That is, if I have a section/chapter whose name includes a reference to an equation, for example, that reference does not show properly in the header.

Below is a minimal example. After compiling, see the header in Page 15, for example. If I comment out

\usepackage[breaklinks=true]{hyperref}
\usepackage{breakurl}

the header displays the reference correctly.

Any ideas why or how to work around this?

\documentclass[a4paper,11pt,twoside]{book}

\usepackage[rmargin=2cm,includeheadfoot,bmargin=2cm,tmargin=3cm, lmargin=4cm]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead[]{\footnotesize{\leftmark}}
\rhead[\footnotesize{\rightmark}]{}
\bibliographystyle{plain}

%If I comment the following two lines, no problem.
\usepackage[breaklinks=true]{hyperref}
\usepackage{breakurl}


\begin{document}

\title{Fake Title}
\author{Me}
\date{\today}
\maketitle

\chapter{First Chapter}

\section{First}
a \newpage b \newpage c \newpage
\section{Second}
\begin{equation}
x=2
\label{eq:myEquation}
\end{equation}
a \newpage b \newpage c \newpage
\section{Third}
\label{sec:thirdSection}
a \newpage b \newpage c \newpage

\chapter{Some thoughts about Section~\ref{sec:thirdSection}}
\section{First}
a \newpage b \newpage c \newpage
\section{About Equation~\ref{eq:myEquation}}
a \newpage b \newpage c \newpage
\section{First}
a \newpage b \newpage c \newpage
\end{document}

Best Answer

The problem is the header in uppercase. Even without hyperref you get warnings like

LATEX WARNING: REFERENCE `SEC:THIRDSECTION' ON PAGE 13 UNDEFINED on input line 44.

If hyperref is loaded, then \ref is not expandable and its argument is converted to uppercase before it is used for getting the reference data.

There are several ways to deal with this. One way is to use a robust macro that includes \ref and its argument. Then this command does not get expanded if used in \MakeUppercase and the argument is not converted to uppercase:

% before \tableofcontents
\DeclareRobustCommand*{\RefSecThirdSection}{\ref{sec:thirdSection}}
\begin{document}
...
\tableofcontents
...
\chapter{Some thoughts about Section~\RefSecThirdSection}

Using e-TeX's \protected\def instead of LaTeX's \DeclareRobustCommand will not help, because of the bookmarks. There \MakeUppercase is disabled and the command is made expandable again. Alternative is \pdfstringdefDisableCommands:

\protected\def\RefSecThirdSection{\ref{sec:thirdSection}}
\pdfstringdefDisableCommands{%
  \def\RefSecThirdSection{\ref{sec:thirdSection}}%
}

For convenience, this can be put in a macro, e.g.:

\newcommand*{\headref}[1]{%
  \csname headref@#1\endcsname
}
\newcommand*{\declareheadref}[1]{%
  \protected\expandafter\def\csname headref@#1\endcsname{%
    \ref{#1}%
  }%
  \expandafter\pdfstringdefDisableCommands\expandafter{%
    \expandafter\def\csname headref@#1\endcsname{%
      \ref{#1}%
    }%
  }%
}
\declareheadref{sec:thirdSection}
\begin{document}
...
\tableofcontents
...
\chapter{Some thoughts about Section~\headref{sec:thirdSection}}

Because of the expandibility of \headref, babel shorthands cannot be supported in the label name.

If you do not want a link, then \getrefnumber of package refcount can be used. \getrefnumber is expandable, thus the contents of the reference is converted to uppercase, not the label name.

\usepackage{refcount}
...
\refused{sec:thirdSection}
\chapter{Some thoughts about Section~\getrefnumber{sec:thirdSection}}

A third way is using uppercase label names in the first place:

\label{SEC:THIRDSECTION}
...
\chapter{Some thoughts about Section~\ref{SEC:THIRDSECTION}}