jbezos / titlesec

Bundle for titles in LaTeX (sections, contents and page styles)
http://www.texnia.com/titlesec.html
MIT License
32 stars 7 forks source link

Corrupted contents on the PDF bookmarks panel when defining new titleclass #26

Open staerz opened 4 years ago

staerz commented 4 years ago

First of all, thanks for that package!

When using it in a rather largely structured document, I think to have identified a minor, but annoying bug:

The contents on the bookmarks panel of the document is corrupted: When adding a new titleclass i.e. exactly following the example in the manual:

\titleclass{\subchapter}{straight}[\chapter]
\newcounter{subchapter}
\renewcommand*\thesubchapter{\thechapter.\arabic{subchapter}}

(and defining the corresponding \titleformat and \titlespacing, of course), in the bookmark view the newly defined titleclass seems to originate from the wrong hierarchical item, and may even affect other items.

Here is a MWE:

\documentclass[12pt,a4paper,openany]{book} 
\renewcommand{\familydefault}{cmss}

\usepackage[pagestyles, outermarks,
  newparttoc% <- added
]{titlesec}
\usepackage{titletoc}
\usepackage[linktocpage]{hyperref}

\titleclass{\part}{top}
\titleformat{\part}[display]
  {\normalfont\Huge}{\centering\thepart}{1em}{\centering}%
  [\setcounter{chapter}{0}]% <- added to reset the chapter counter for each part

\contentsmargin{0pt}%<- added (as suggested by jvd in a comment)

\titlecontents{part}[10mm]% <- changed
  {\vspace{12pt}\large\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\titlerule*[.75em]{o}\thecontentspage}

\titlecontents{chapter}[10mm]
  {\vspace{4pt}\normalsize\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\titlerule*[.75em]{-}\thecontentspage}

\titlecontents{section}[22.3mm]
  {\vspace{0pt}\normalsize\normalfont}
  {\contentslabel[\thecontentslabel]{12.3mm}}{}
  {\titlerule*[.75em]{.}\thecontentspage}

% introducing new level "subchapter"
\titleclass{\subchapter}{straight}[\chapter]
\newcounter{subchapter}
\renewcommand*\thesubchapter{\thechapter.\arabic{subchapter}}

\titleformat{\subchapter}
  {\normalfont\large\scshape}{\thesubchapter}{1.0em}{}

\titlespacing*{\subchapter}
  {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\renewcommand\thechapter{\thepart\arabic{chapter}}
\setcounter{secnumdepth}{1}
\begin{document}
\pagenumbering{roman}
\tableofcontents
\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}
\cleardoublepage 
\pagenumbering{arabic}
\renewcommand\thepart{I}
\part{Island}
\chapter{Desert}
\subchapter{Testing}
\section{Shelter}
\chapter{Paradise}
\section{Beach}

\cleardoublepage% only for the example
\setcounter{page}{51}% only for the example
\renewcommand\thepart{W}
\part{Water}
\chapter{Ice}
\section{Snow}
\chapter{Liquid}
\section{Wave}

\cleardoublepage% only for the example
\setcounter{page}{401}% only for the example
\renewcommand\thepart{L}
\part{Land}
\chapter{Continent}
\section{Mountain}
\chapter{Rock}
\section{Stone}

\clearpage
\renewcommand\thepart{A}
\part{Animals}
\chapter{Mammals}
\section{Humans}
\chapter{Reptiles}
\section{Lizards}
\end{document}

The resulting bookmark and ToC I get is this one: testcase_bookmarks

I can see a similar behaviour when defining a \subsubsubsection: There the bookmark is originating from the root of the document and consecutive sections (but higher in the hierarchy) are shown as sub-items: maindoc_bookmarks

I'm not a LaTeX expert and have no idea how bookmarks are generated at all but I would assume this to be something that the titlesec/titletoc would be responsible for - or I'm not using it correctly, i.e. miss a setting or definition somewhere.

In any case, feedback is welcome.

jbezos commented 4 years ago

Thank you for your report. To be honest, I don't know what's happening, because some time ago it worked. Maybe a change in hyperref or something I've accidentally broken, but after a few tests I'd say there are two separate problems: (1) bookmarks are messed up when \part is ‘re-classed’, and (2) subchapters are treated like chapters.

As hyperref is involved, which does a lot of tricky things (like titlesec), I think it may take some time to figure out where both problems are.

jbezos commented 4 years ago

The first problem seems to be related directly to hyperref. The following example works:

\documentclass{book} 

\usepackage{titlesec}

\usepackage[linktocpage]{hyperref}

\titleformat{\part}[display]
  {\normalfont\Huge}{\centering\thepart}{1em}{\centering}%
%  [\setcounter{chapter}{0}]

\begin{document}

\part{Island}
\chapter{Desert}
\section{Shelter}

\part{Water}
\chapter{Ice}
\section{Snow}

\end{document}

But if you uncomment [\setcounter{chapter}{0}] it fails (tested also with your MWE). It seems hyperref needs some additional setup if chapter numbers are duplicated.

Now I have to investigate the second issue (\subchapter).

staerz commented 4 years ago

Hi Javier,

I'm happy to see that you immediately jumped onto that issue :wink:

And I'm also happy that you can reproduce the issue on your side which is the first necessary step in understanding what is going wrong and finally finding a fix.

DoctorHayes commented 3 years ago

I had a similar issue with sections and fixed it with the following statements in the document header.

\newcommand{\sectionbreak}{\phantomsection}
\newcommand{\subsectionbreak}{\phantomsection}
\newcommand{\subsubsectionbreak}{\phantomsection}