# Cross-references in headers, clash between fancyhdr and hyperref

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{fancyhdr}
\pagestyle{fancy}
\bibliographystyle{plain}

%If I comment the following two lines, no problem.
\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}
$$x=2 \label{eq:myEquation}$$
a \newpage b \newpage c \newpage
\section{Third}
\label{sec:thirdSection}
a \newpage b \newpage c \newpage

\section{First}
a \newpage b \newpage c \newpage
a \newpage b \newpage c \newpage
\section{First}
a \newpage b \newpage c \newpage
\end{document}

-
Funny. The source of the problem is that fancyhdr writes the whole header in uppercase, including your label name. When not using hyperref, your \ref is expanded to its replacement before uppercasing, but hyperref implicitly protects \ref, so the label name ends up in the header as \ref{SEC:THIRDSECTION} for instance. I don't know if a general fix for this already exists, but a quick fix would be to always write label names in uppercase. – Stephan Lehmke Aug 25 '12 at 5:43
I tried the package textcase with the overload option and enclosed the \ref{label} in a NoCaseChange, but this did not help. – Sveinung Aug 25 '12 at 8:39
You already got some answers how to solve the coding problem. But imho the core of the problem is your use of \ref in a chapter title: I would hate a book which use such numbers in a title. I don't want to go to section XY to find out what the chapter is about. – Ulrike Fischer Aug 25 '12 at 10:15
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). – Werner Aug 27 '12 at 2:27

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
...


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]{%
}
\ref{#1}%
}%
\expandafter\pdfstringdefDisableCommands\expandafter{%
\ref{#1}%
}%
}%
}
\begin{document}
...
\tableofcontents
...


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}


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

\label{SEC:THIRDSECTION}
...

-

As mentioned by Stephan, writing labels in ALL CAPITALS should fix the problem, as \MakeUppercase modifies the argument of \ref{sec:thirdSection} to \ref{SEC:THIRDSECTION} which obviously does not exist. However, you can also hide the case change from \MakeUppercase by using an auxiliary macro:

\newcommand{\RthirdSection}{\ref{sec:thirdSection}}
%...
\newcommand{\RmyEquation}{\ref{eq:myEquation}}


Note that you also have to \protect the macros used.

This suggestion stems from the UK TeX FAQ entry Case-changing oddities.

-
Thank you very much everyone!!! This last one, defining a new macro and protecting it is the one I like the most. Just tried it and it works like a charm! – Cristian Aug 27 '12 at 0:51

A solution with minimal changes to the document (no new macros or changing existing labels/references) is to add an upper-case version as a second label on the item being referenced. For example, if the problematic reference is

\begin{theorem}\label{mainresult} Suppose...
\end{theorem}

\section{Proof of Theorem~\ref{mainresult}}


adding another label makes the reference work in headers with no other changes.

\begin{theorem}\label{mainresult}\label{MAINRESULT} Suppose...
\end{theorem}

\section{Proof of Theorem~\ref{mainresult}}
`

While this is not as elegant as protecting the reference from uppercase conversion, it is of some interest as a rare case when putting two labels on the same item makes sense.

-