11

I'm using titlesec to define the layout of my headings. It's working but I've run into a problem when I use the package to define the layout of \part's.
When I try to reference to a \part, which is defined with titlesec, using \nameref I get the wrong title.

Does anybody know a workaround for this or what I'm doing wrong? I've made this example to show what I'm talking about:

\documentclass{report}
\usepackage{titlesec}
\usepackage{hyperref}

% If you uncomment the following 5 lines the nameref in line 16 will refer to "Chapter title 2" instead of "Part title 2"
%\titleformat{\part}[display]
%   {\Huge\filcenter}
%   {\partname{} \thepart}
%   {0em}
%   {\hrule}

\begin{document}

\part{Part title 1}
\chapter{Chapter title 1}
Hello have a look at \autoref{part:part_2} \nameref{part:part_2}
\chapter{Chapter title 2}

\part{Part title 2}\newpage
\label{part:part_2}
\chapter{Chapter title 3}

\end{document}

Looking forward to hear your input.

1 Answer 1

10

The \@currentlabelname internal command which holds the section name is not updated, for some reason, when redefining part with titlesec. A way to get around this is to redefine the \part command to force this updating. As \part has a syntax which is a bit complicated (it has a star form and can take an optional argument), the code is a bit lengthly in order to take into account all the cases:

\makeatletter
\let\titlesec@part\part
\renewcommand{\part}{\@ifstar\part@star\part@nostar}
\def\part@star#1{\NR@gettitle{#1}\titlesec@part*{#1}}
\def\part@nostar{\@ifnextchar[\part@nostar@opt\part@nostar@nopt}
\def\part@nostar@nopt#1{\NR@gettitle{#1}\titlesec@part{#1}}
\def\part@nostar@opt[#1]#2{\NR@gettitle{#2}\titlesec@part[#1]{#2}}
\makeatother

This code must be after the \titleformat{\part} command, as shown in the following complete example:

\documentclass{report}
\usepackage{titlesec}
\usepackage{hyperref}

\titleformat{\part}[display]
   {\Huge\filcenter}
   {\partname{} \thepart}
   {0em}
   {\hrule}
\makeatletter
\let\titlesec@part\part
\renewcommand{\part}{\@ifstar\part@star\part@nostar}
\def\part@star#1{\NR@gettitle{#1}\titlesec@part*{#1}}
\def\part@nostar{\@ifnextchar[\part@nostar@opt\part@nostar@nopt}
\def\part@nostar@nopt#1{\NR@gettitle{#1}\titlesec@part{#1}}
\def\part@nostar@opt[#1]#2{\NR@gettitle{#2}\titlesec@part[#1]{#2}}
\makeatother

\begin{document}

\part{Part title 1}
\chapter{Chapter title 1}
Hello have a look at \autoref{part:part_2} \nameref{part:part_2}
\chapter{Chapter title 2}

\part{Part title 2}\label{part:part_2}
\chapter{Chapter title 3}

Other tests:

\part*{Part title 3}\label{part:part_3}

\part[Short part title 4]{Part title 4}\label{part:part_4}

\nameref{part:part_3}, \nameref{part:part_4}

\end{document}
2
  • Thanks a lot! I would never have figured that out my self...! Dec 7, 2010 at 8:35
  • Ended up here looking for a solution working with classicthesis.sty and for chapter rather than part. This works, thanks a lot!
    – euronion
    Aug 12, 2023 at 18:35

You must log in to answer this question.

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