3

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 Answers 3

5

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

3
  • 2
    titleref is not titlesec. Oct 19, 2017 at 16:21
  • @UlrikeFischer Thanks, the README also contains an entry for titlesec, answer updated accordingly. Oct 19, 2017 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, 2017 at 16:46
1

Since this question popped up as I was searching for answers to the same conflict using titlesec, hyperref and \section*{} that needed labelling, I like to share the workaround that Ulrike Fisher published on the forum a few years later. So this question will be crosslinked to that answer and hence be of benefit of all.

Ulrike defined a new command, that can be used to augment the starred section (and others) requiring a label. You can find her answer here: nameref is not working for unnumbered sections due to titilesec and latexbangla .

0

Another workaround is just keeping your numbered sections, but visually hide them by using a combination of removing the numbering of sections and the removing of numbering from toc:

\usepackage{tocloft}
\renewcommand\numberline[1]{}
\titleformat{\section}{\normalfont\Large\bfseries}{}{0pt}{}

This creates a TOC with nicely aligned links without numbers, linking to nicely aligned section headers without numbers, precisely to where the numbered section would link.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .