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


the header displays the reference correctly.

Any ideas why or how to work around this?


\usepackage[rmargin=2cm,includeheadfoot,bmargin=2cm,tmargin=3cm, lmargin=4cm]{geometry}

%If I comment the following two lines, no problem.


\title{Fake Title}

\chapter{First Chapter}

a \newpage b \newpage c \newpage
a \newpage b \newpage c \newpage
a \newpage b \newpage c \newpage

\chapter{Some thoughts about Section~\ref{sec:thirdSection}}
a \newpage b \newpage c \newpage
\section{About Equation~\ref{eq:myEquation}}
a \newpage b \newpage c \newpage
a \newpage b \newpage c \newpage
  • 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. 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
  • 2
    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. 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


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
\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:


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

  \csname headref@#1\endcsname
  \protected\expandafter\def\csname headref@#1\endcsname{%
    \expandafter\def\csname headref@#1\endcsname{%
\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.

\chapter{Some thoughts about Section~\getrefnumber{sec:thirdSection}}

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

\chapter{Some thoughts about Section~\ref{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:

\chapter{Some thoughts about Section~\protect\RthirdSection}
\section{About Equation~\protect\RmyEquation}

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

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

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

  • CAVEAT! This doesn't work for \begin{equation}…\end{equation} where, for some reason, only the last \label is taken into account. My advice: when replacing \label{abc} by two versions, do it in the opposite order: first the uppercase and then the lowercase (\label{ABC}\label{abc}). This will not make your \eqref pointing to formulas disappear!
    – yannis
    Jan 14 '17 at 11:12
  • I'm preparing a final thesis copy for binding and I've been instructed to take unwanted colour out of the pdf, so I've disabled hyperref and have a broken link in my header. This solution is very good for the case where I have ONLY one appendix that refers to the results of a particular section and I don't want to go through and make large changes. Nov 28 '17 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.