0

The cross-reference (\nameref) in the code below does not populate unless I remove the titlesec package. I need both packages. How can I get around this?

\documentclass{article}
\usepackage{titlesec}
\usepackage{hyperref}
\begin{document}
\section*{Section title}\label{first}
See section \nameref{first}.
\end{document}
3

See README of hyperref contains:

titlesec
--------
  "nameref" supports titlesec, but hyperref does not
  (unsolved is the anchor setting, missing with unnumbered
  section, perhaps problems with page breaks with numbered ones).

Thus, you can use nameref with titlesec, but hyperref is unsupported then. The example compiles with nameref instead of hyperref:

\documentclass{article}
\usepackage{titlesec}
\usepackage{nameref}
\begin{document}
\section*{Section title}\label{first}
See section \nameref{first}.
\end{document}

However, \nameref{first} is empty, because the \label{first} does not reference a numbered section. Unnumbered sections are not supported.

Workaround

The following workaround defines a macro \starsection that passes its argument to \section*. Then an internal command (\@currentlabelname) is defined via the internal interface of nameref (macro \NR@gettitle). Then, \label can pick up the current section title for use with \nameref.

\documentclass{article}
\usepackage{titlesec}
\usepackage{nameref}

\makeatletter
\newcommand*{\starsection}[1]{%
  \section*{#1}%
  \NR@gettitle{#1}%
}
\makeatother

\begin{document}
\starsection{Section title}\label{first}
See section \nameref{first}.
\end{document}

Result

  • 2
    titleref is not titlesec. – Ulrike Fischer Oct 19 '17 at 16:21
  • @UlrikeFischer Thanks, the README also contains an entry for titlesec, answer updated accordingly. – Heiko Oberdiek Oct 19 '17 at 16:23
  • I am not able to get my example to format if I use nameref and titlesec, but not hyperref. – Tom Price Oct 19 '17 at 16:46

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.