4

General description of the issue

I have an appendix whose section names have to be identical to the names of sections of the main part of the dissertation. Therefore I used the \nameref command of the package nameref.

[...]
\mainmatter
\section{My section name}
\label{mySection}
[...]
\appendix
\section{\nameref{mySection}}
[...]

This works. However, the header of the left page in the book class is supposed to display the name of the first section of the page. When using \nameref, the header displays ?? instead of the section name. Why does it behave so? How can I get around that?

Simple example

As an example, create a new file main.tex containing

\documentclass[10pt,a4paper]{book}
\usepackage{nameref}

\begin{document}

\section{Alice in wonderland}
\label{section:alice}

\pagebreak
a %Dummy text otherwise calling \pagebreak twice only skips one page
\pagebreak %Need to skip two pages, not just one because only the header of the left page of the book class displays the section name, not the one of the right page

%\section{Alice in wonderland} %Writes the header correctly
\section{\nameref{section:alice}} %Header shows "??"

\end{document}

Compile with pdflatex main.tex. On the top of the third page, the header displays 0.2 ?? instead of 0.2 ALICE IN WONDERLAND.

  • 2
    That is probably because the default headers in book are upper case, so the header actually runs \nameref{SECTION:ALICE} which does not exist. Dirty fix: write the label in UPPER case. If you look in the log, this is exactly what LaTeX warns. – daleif Feb 12 '18 at 9:55
  • See tex.stackexchange.com/q/252134/117534 or tex.stackexchange.com/q/99712/117534. Welcome to TeX.SE! – Troy Feb 12 '18 at 10:00
  • @Troy I've been looking for similar questions before posting but did not find these… Perhaps my search was too oriented towards the \nameref{} command… Anyway, thanks for linking to those other questions, that's helpful! – Gael Lorieul Feb 12 '18 at 10:26
  • @GaelLorieul No worries. Can you confirm if any of the solutions given in the linked questions are acceptable to you? If so then we can close this question as a duplicate (If daleif will write an answer, then that's fine too.) – Troy Feb 12 '18 at 10:28
  • @daleif your solution might be "dirty" as you put it, but it seems that other solutions are either complicated or require the modification of the document class. So it might actually be the best solution, at least of those that I've come across. Thanks! (PS : why not change your comment into an answer so that I can validate it?) – Gael Lorieul Feb 12 '18 at 10:28
6

By default the book class upper cases all headers. The way this upper casing works, means that when \nameref{section:alice} is written to the header, the actual data is

\nameref{SECTION:ALICE}

which is a reference to an unknown label, which we also see in the log of the MWE

LaTeX Warning: Reference `SECTION:ALICE' on page 3 undefined on input line 17

A dirty fix would be to actually use \label{SECTION:ALICE} instead.


Nowadays, using pure upper case headers, may be seen as shouting, so I usually just disable the automatic upper casing.

Normally I never use the book class, instead I use the memoir class, which has the nameref feature build in, as \titleref. Also in memoir it is very easy to disable the default upper casing via

\nouppercaseheads
\pagestyle{headings} % reactivate the page style (\nou.. changes an internal macro in the headers, thus the header macros has to be applied again)

The memoir class can be used as a drop in replacement for the book class.

3

You can use Diego Dias' trick here that's inspired by the refcount package.

\documentclass[10pt,a4paper]{book}
\usepackage{nameref}

\makeatletter
\newcommand{\getnamereftext}[1]{%
  \@ifundefined{r@#1}{}{%
    \unexpanded\expandafter\expandafter\expandafter{%
      \expandafter\expandafter\expandafter\@thirdoffive\csname r@#1\endcsname
    }%
  }%
}
\makeatother

\begin{document}

\section{Alice in wonderland}
\label{section:alice}

\clearpage
a %Dummy text otherwise calling \pagebreak twice only skips one page
\clearpage %Need to skip two pages

\section{\getnamereftext{section:alice}}

\end{document}

enter image description here

  • 1
    @daleif Indeed! I don't know what happened. Fixed. – egreg Feb 12 '18 at 11:00
0

Your \nameref{}, which works similarly to \label{} has to be outside of the \section{} command:

\section{\nameref{section:alice}} %Header shows "??"

\section{ Alice in Wonderland} \nameref{section:alice} %works, returns label

As pointed out by daleif: Putting both \label{} and \nameref{} in uppercase works perfectly as a dirty workaround without returning the content of \label{}

  • I don't think that \nameref{} should be outside \section{}: unlike \label{} which sets an (invisible) label, \nameref{} generates a text string that is visible in the generated pdf file. Hence \section{ Alice in Wonderland} \nameref{section:alice} writes Alice in Wonderland below the section header. Besides, the whole point of using \nameref{} was to not write the section name manually, such that if I modify it in the core of the dissertation, then the name of the section in the appendix is updated automatically. – Gael Lorieul Feb 12 '18 at 10:23
  • Ok, now I understand your problem fully. I will edit my answer and remove the upper part. @daleif answer works for me, does it for you? – Sebastian Feb 12 '18 at 10:33
  • @daleif yes it does, but since he's the one who proposed it first, it would be fair to let him a chance to post it as an answer… – Gael Lorieul Feb 12 '18 at 10:34
  • ofc, he's welcome to do so. It was not my intention to grab his answer... if a previous answer to another question is correct, you may mark the question or an answer as a duplicate – Sebastian Feb 12 '18 at 10:39

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.