3

I'm creating new classes with titlesec package to work in this hierarchy:

PART
- Prechap
-- Presec
--- Presubsec

I implemented them as seen in the MWE below. When I compiled the file with LuaLaTeX the hierarchy is not respected and they seem to have the same level. The figure shows in the right what I have and what I wish I have in the left.

Right: what I have. Left: what I wish I have.

Also, in the Table of Contents, the Part is not typeset in small caps. How do I get the hierarchy level as shown above with the classes I have made?

\documentclass[12pt,a4paper]{book}

\usepackage[loadonly]{titlesec}
\usepackage{titletoc}
\usepackage{tocloft}
\usepackage{lipsum}
\usepackage{bookmark}
\usepackage{hyperref}

\hypersetup{draft}

\titleclass{\part}[-1]{page}

% Changes in part name appearence
\titleformat{\part}% command
    [block]% shape
    {\centering\fontsize{30pt}{30pt}\selectfont}% format
    {}% label
    {0 cm}% sep
    {\thispagestyle{empty}}% before-code
    []% after-code

% Changes spaces in between part
\titlespacing{\part}{0 pt}{.4\textheight}{0 cm}

% Add part to toc
\titlecontents{part}[0em]{\Large\bfseries\scshape}{}{}{\hfill\contentspage}[]

% Create prechap class
\titleclass{\prechap}{top}[\part]
\newcounter{prechap}

% Formats prechap entry
\titleformat{\prechap}% command
    [block]% shape
    {\centering\fontsize{20pt}{20pt}\selectfont}% format
    {}% label
    {0 cm}% sep
    {\uppercase}% before-code
    [\thispagestyle{empty}]% after-code

% Changes spaces in between prechap
\titlespacing{\prechap}{0 cm}{1 cm}{1 cm}

% Add prechaps to toc
\titlecontents{prechap}[0em]{\bfseries}{}{}{\hfill\contentspage}[]

% Create presec class
\titleclass{\presec}{straight}[\prechap]
\newcounter{presec}

% Formats presec entry
\titleformat{\presec}% command
    [hang]% shape
    {\fontsize{14pt}{14pt}\selectfont}% format
    {}% label
    {0 cm}% sep
    {\bfseries}% before-code
    []% after-code

% Changes spaces in between presec
\titlespacing{\presec}{0 cm}{1 cm}{.2 cm}

% Add presecs to toc
\titlecontents{presec}[1em]{\scshape}{}{}{\hfill\contentspage}[]

% Create presubsec class
\titleclass{\presubsec}{straight}[\presec]
\newcounter{presubsec}

% Formats presubsec entry
\titleformat{\presubsec}% command
    [hang]% shape
    {\fontsize{14pt}{14pt}\selectfont}% format
    {}% label
    {0 cm}% sep
    {\itshape}% before-code
    []% after-code

% Changes spaces in between presubsec
\titlespacing{\presubsec}{0 cm}{1 cm}{.2 cm}

% Add presubsecs to toc
\titlecontents{presubsec}[2em]{\itshape}{}{}{\hfill\normalfont\contentspage}[]

\begin{document}
    \tableofcontents

    \prechap{Preface}
    \lipsum[1]

    \prechap{Prelude}
    \lipsum[2]

    \presec{Section one}
    \lipsum[3]

    \presubsec{Subsection one}
    \lipsum[4]

    \presubsec{Subsection two}
    \lipsum[5]

    \part{Part One}

    \lipsum[6]

    \prechap{Prechap One}
    \lipsum[7]

    \presec{Section Two}
    \lipsum[8]

    \presubsec{Subsection Three}
    \lipsum[9]
\end{document}
  • 1
    Regarding small caps: PDF does not allow a lot of formatting for outline items. You can only set a color and whether the text should be bold or italic. There is no option to turn the text into (real) small caps – Marcel Krüger Aug 30 '19 at 12:26
  • The small caps it's not in the bookmarks but in the ToC page. – Levy Aug 30 '19 at 14:49
3
+50

When hyperref adds entries to the bookmarks, it takes the level information from commands of the form \toclevel@<name>, where <name> is the name of the text division. For the book class those are

\def\toclevel@part{-1}
\def\toclevel@chapter{0}
\def\toclevel@section{1}

and so on.

For your new division types prechap, presec and presubsec those commands are not defined, which hyperref warns you about in the log:

Package hyperref Warning: bookmark level for unknown prechap defaults to 0.
Package hyperref Warning: bookmark level for unknown presec defaults to 0.
Package hyperref Warning: bookmark level for unknown presubsec defaults to 0.

As all levels get a new default level of 0, the bookmarks in your PDF file appear "flat". The solution is simply to add the following lines to your document's preamble to make hyperref aware what levels you want your new divisions to correspond to:

\makeatletter
\def\toclevel@prechap{0}
\def\toclevel@presec{1}
\def\toclevel@presubsec{2}
\makeatother

Now the bookmarks are nested as expected.

enter image description here

  • Thank you! I solved the problem. – Levy Aug 30 '19 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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