Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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}
share|improve this answer
    
Thanks a lot! I would never have figured that out my self...! –  Richard Lindquist Capion Dec 7 '10 at 8:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.