6

I'm using hyperref package to do cross-referencing, but I'm encountering some problems and it is related to \usepackage{titlesec,titleps} , for instance:

\documentclass[a4paper,12pt,twoside]{report}
\usepackage{hyperref}
\usepackage{titlesec,titleps} %If I remove the \titleclass for \part it works fine!
\titleclass{\part}{top}\titleformat{\part}{\centering\normalfont\Huge\bfseries}{}{0pt}{}
\titleformat{\section}{\normalfont\bfseries}{\thesection\hspace{3pt}$\vert$}{3pt}{}
\titleformat{\subsection}{\normalfont\slshape}{\thesubsection\hspace{3pt}$\vert$}{3pt}{}
\titleformat{\subsubsection}{\normalfont}{\thesubsubsection\hspace{3pt}$\vert$}{3pt}{}
\titleformat{\paragraph}{\normalfont\slshape}{\theparagraph\hspace{3pt}$\vert$}{3pt}{}{}
\titleformat{\subparagraph}{\normalfont}{\thesubparagraph\hspace{3pt}$\vert$}{3pt}{}{}

\begin{document}
\part{Part One}
\label{P1}
\chapter{Chapter One}
\label{C1}
\section{Section One}
\label{S1}
Here the cross-reference for \autoref{C1} "\nameref{C1}" and \autoref{S1} "\nameref{S1}" work fine, but not for \autoref{P1} "\nameref{P1}".

As also for \autoref{C2} "\nameref{C2}" and \autoref{S2} "\nameref{S2}", but not for \autoref{P2} "\nameref{P2}".\\

\part{Part Two}
\label{P2}
\chapter{Chapter Two}
\label{C2}
\section{Section Two}
\label{S2}

\end{document}

So if I cross-reference to \nameref{P2} from anywhere in "Part One" it gives the name of the "Section One" (the last before Part Two, if I remove that section it goes to the one before it), while the "Part II" from the \autoref{P2} is right. If I refer to \nameref{P1} in "Part One" it gives me the error "??". Is there a way to know what is going on? Thanks for your help and attention!

hehe_br
  • 303
  • @DavidCarlisle the problem is with the \titleclass for the \part, is there a work around for it? Or maybe is it a bug? – hehe_br Nov 07 '14 at 18:30
  • thanks for editing the example, it should be much easier to fix now (someone if not me, I can confirm I see same in texlive 2014) – David Carlisle Nov 07 '14 at 18:55

1 Answers1

12

Not sure which package is breaking what, but \part ends up not saving the part name in a form that nameref can use. This just redefines it to save it explicitly, it assumes you don't need * or [] argument forms of \part.

\documentclass[a4paper,12pt,twoside]{report}

\usepackage{titlesec,titleps} %If I remove the \titleclass for \part it works fine!
%\titleclass{\part}{top}\titleformat{\part}{\centering\normalfont\Huge\bfseries}{}{0pt}{}
\titleformat{\section}{\normalfont\bfseries}{\thesection\hspace{3pt}$\vert$}{3pt}{}
\titleformat{\subsection}{\normalfont\slshape}{\thesubsection\hspace{3pt}$\vert$}{3pt}{}
\titleformat{\subsubsection}{\normalfont}{\thesubsubsection\hspace{3pt}$\vert$}{3pt}{}
\titleformat{\paragraph}{\normalfont\slshape}{\theparagraph\hspace{3pt}$\vert$}{3pt}{}{}
\titleformat{\subparagraph}{\normalfont}{\thesubparagraph\hspace{3pt}$\vert$}{3pt}{}{}
\usepackage{hyperref}
\makeatletter
\let\oldpart\part
\def\part#1{\def\@currentlabelname{#1}\oldpart{#1}}
\makeatother
\begin{document}
\part{Part One}
\label{P1}
\chapter{Chapter One}
\label{C1}
\section{Section One}
\label{S1}
Here the cross-reference for \autoref{C1} "\nameref{C1}" and \autoref{S1} "\nameref{S1}" work fine, but not for \autoref{P1} "\nameref{P1}".

As also for \autoref{C2} "\nameref{C2}" and \autoref{S2} "\nameref{S2}", but not for \autoref{P2} "\nameref{P2}".\\

\part{Part Two}
\label{P2}

\chapter{Chapter Two}
\label{C2}
\section{Section Two}
\label{S2}

\end{document}
David Carlisle
  • 757,742
  • Your macro works even with the \titleclass on. Another important aspect also was to load \usepackage{hyperref} after the \usepackage{titlesec} it solved another issue I was having with \usepackage{titletoc} to inset a TOC in the \part page. Thanks for your help! – hehe_br Nov 07 '14 at 19:53
  • @David Carlisle: unfortunately, classicthesisdoes not work together with your solution. Do you know what I should do to use \nameref referring to \part{}\label{} when using classicthesis (texlive 2015)? The relevant code section in classicthesis.sty (lines 600-632) does not look very promising. – Hotschke Jun 12 '16 at 15:58
  • I had the same problem in the package beamer, \nameref did not show the name of the "part". This \part redefinition solved it seamlessly. – loved.by.Jesus Feb 28 '19 at 23:43
  • Probably related https://tex.stackexchange.com/a/6658/167404 – Alexey Shrub Sep 10 '20 at 18:30
  • @Hotschke and others ending up here looking for a solution for classicthesis: The solution mentioned in https://tex.stackexchange.com/a/6658 works for classicthesis. – euronion Aug 12 '23 at 18:34