Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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
1  
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
add comment

2 Answers 2

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}}
share|improve this answer
add comment

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}}
\chapter{Some thoughts about Section~\protect\RthirdSection}
%...
\newcommand{\RmyEquation}{\ref{eq:myEquation}}
\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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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