[texhax] Problem with the nameref package

Heiko Oberdiek heiko.oberdiek at googlemail.com
Mon May 19 20:27:31 CEST 2014


On 19.05.2014 17:03, José Carlos Santos wrote:

> \documentclass{book}
> \usepackage{hyperref}
> \begin{document}
> \tableofcontents
> \chapter[Short title]{A very, very long title}\label{title}
> See chapter ``\nameref{title}''.
> \end{document}
> 
> then the command \nameref{title} gets the short version of the chapter
> title, whereas I am interested in the long one. Is there a way of
> forcing the nameref package to behave as I want?

The title for the label is written into the `.aux` file
=> moving argument.
Therefore it is quite dangerous for a package like `nameref'
to prefer the long title that might contain fragile stuff.

Nevertheless I have added this as feature request into my
ToDo list.

A workaround:

\documentclass{book}
\usepackage{hyperref}

\usepackage{nameref}
\newif\ifnamereflongtitle
\makeatletter
\def\@chapter[#1]#2{%
  \ifnamereflongtitle
    \NR at gettitle{#2}%
  \else
    \NR at gettitle{#1}%
  \fi
  \NR at chapter[{#1}]{#2}%
}
\makeatother

\begin{document}
\tableofcontents
\namereflongtitletrue
\chapter[Short title]{A very, very long title}\label{title}
See chapter ``\nameref{title}''.
\end{document}

Yours sincerely
  Heiko Oberdiek




More information about the texhax mailing list