2

I managed to add in the TOC the string Chapter x before each chapter title (following this post).

The chapter titles format was :

A Nice Title

They are now :

Chapter 1   A Nice Title

Everything works great.

But, in a second step, how do I proceed to add it in the bookmarks displayed by the PDF viewers ? Even more, how do I add it only for those include in the \mainmatter part ?

Info : I use xelatex.

[EDIT]

Here is a MWE :

\documentclass[a4paper,oneside]{book}

\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{titletoc}
\titlecontents*{chapter}
  [0pt]% <left>
  {}
  {\chaptername\ \thecontentslabel\quad}
  {}
  {\bfseries\hfill\contentspage}

\title{My document}

\begin{document}

\frontmatter
\chapter{Introduction} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]

\mainmatter
\chapter{Second chapter} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]
\chapter{Last chapter} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]

\backmatter
\chapter{Conclusion}
\lipsum[8]

\tableofcontents
\end{document}

What gives :

enter image description here

I'd like the same format for the chapters titles in the PDF bookmarks. It seems that a solution could be found with the bookmark package... but it's a mystery to me.

8
  • 1
    Can you make a minimal working example (MWE)? Commented Mar 7, 2018 at 14:45
  • Hum... sure, I could. But is it really necessary in this case ? There is no specificity here, it's a generic question. However, the one which is included in the linked post works.
    – laspic
    Commented Mar 7, 2018 at 14:53
  • As mentioned by samcarter, we will need a MWE as the solution will probably depend on the document class and preamble you use. BTW: the TOC by PDF viewers is often called the bookmarks.
    – daleif
    Commented Mar 7, 2018 at 14:53
  • @archi02 Even if there is nothing special in your MWE it will gve us something to start with and not having to start from scratch. Commented Mar 7, 2018 at 14:57
  • For example, I have a solution to this, but it relies on features in the memoir class that are not readily in other classes.
    – daleif
    Commented Mar 7, 2018 at 15:25

2 Answers 2

1

Probably there is a much cleaner solution with bookmark, but you can patch hyperref internals:

\documentclass{book}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{hyperref}
\usepackage{xparse}
\usepackage{titletoc}% http://ctan.org/pkg/titletoc
\titlecontents*{chapter}% <section-type>
  [0pt]
  {}
  {\chaptername\ \thecontentslabel\quad}
  {}
  {\bfseries\hfill\contentspage}

\makeatletter
\ExplSyntaxOn
\cs_new:Npn\__hack_bookmarkprefix:w#1.#2\q_stop{
  \str_if_eq:nnT{#1}{chapter}{
    \chaptername\ #2\ 
  }
}
\cs_new:Nn\__hack_bookmarkprefix:n{
  \__hack_bookmarkprefix:w#1\q_stop
}
\cs_new:Nn\__hack_bookmark:nnnn{
  \@@BOOKMARK[#1][#2]{#3}{#4}
}
\cs_generate_variant:Nn\__hack_bookmark:nnnn{nnnf}
\RenewDocumentCommand\BOOKMARK{O{1} O{-} m m}{
  \__hack_bookmark:nnnf{#1}{#2}{#3}{\__hack_bookmarkprefix:n{#3}#4}%
}
\ExplSyntaxOff
\makeatletter

\begin{document}
\tableofcontents

\frontmatter
\chapter{Introduction} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]

\mainmatter
\chapter{Second chapter} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]
\chapter{Last chapter} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]

\backmatter
\chapter{Conclusion}
\lipsum[8]
\end{document}
5
  • I've copy-pasted this code and launched it with xelatex. It stops with the error ! Undefined control sequence. l.27 \RenewExpandableDocumentCommand \BOOKMARK{O{1} O{-} m m}{ ... Did I do something wrong ?
    – laspic
    Commented Mar 7, 2018 at 17:05
  • Have you also copied \usepackage{xparse}? Commented Mar 7, 2018 at 17:08
  • You're version of xparse might also be too old. \RenewExpandableDocumentCommand exists since the xparse version from March 2017. Commented Mar 7, 2018 at 17:15
  • I edited the answer to be compatible with older versions. Commented Mar 7, 2018 at 18:59
  • Perfect! (indeed the version provided by ubuntu 17.04 is dated 2016-01-19, v6377). Thanks! :-)
    – laspic
    Commented Mar 7, 2018 at 19:40
2

The following example uses package bookmark and its hook support to smuggle in the chapter prefix. Also the numbers are added by option numbered (package bookmark, that executes option bookmarknumbered of package hyperref).

\documentclass[a4paper,oneside]{book}

\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{titletoc}
\titlecontents*{chapter}
  [0pt]% <left>
  {}
  {\chaptername\ \thecontentslabel\quad}
  {}
  {\bfseries\hfill\contentspage}    

\usepackage{bookmark}
\usepackage{etoolbox}

\makeatletter
\newcommand*{\AddChapterPrefixInBookmarks}{%
  \if@mainmatter
    \ifnum\bookmarkget{level}=0 %
      \preto\bookmark@text{\@chapapp\space}%
    \fi
  \fi
}
\makeatother

\bookmarksetup{
  numbered,
  addtohook=\AddChapterPrefixInBookmarks,
}

% Workaround for numbered sections in unnumbered
% chapter "Introduction" to avoid chapter number
% zero.
\renewcommand*{\thesection}{%
  \ifcase\value{chapter}%
  \else
    \thechapter.%
  \fi
  \arabic{section}%
}

\title{My document}

\begin{document}
\frontmatter
\tableofcontents
\chapter{Introduction} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]
\subsection{Subsection}

\mainmatter
\chapter{Second chapter} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]
\chapter{Last chapter} \lipsum[1]
\section{First section} \lipsum[2-3]
\section{Second section} \lipsum[4-5]
\section{Last section} \lipsum[6-7]
\subsection{Subsection}

\backmatter
\chapter{Conclusion}
\lipsum[8]

\tableofcontents
\end{document}

Bookmarks

5
  • This works for me if I comment out the \renewcommand*{\thesection} part. Even unnumbered sections seems to work as they should (so the “workaround” doesn't seem necessary?). I’m using XeTeX 3.14159265-2.6-0.999991 (TeX Live 2019/Debian) on Ubuntu 20.04.
    – zrajm
    Commented Apr 8, 2022 at 8:09
  • @zrajm I do not know, what you are doing. If the redefinition of \thesection is removed from the example in the answer, then the sections in chapter Introduction start with a chapter number zero: 0.1 First section, 0.2 Second section, ... Commented Apr 8, 2022 at 17:15
  • Well, I don't know what I'm doing either. :) – But it works just fine, and I'm not getting any extra zeroes. (So I'm happy.)
    – zrajm
    Commented Apr 12, 2022 at 0:00
  • I'd like to be able to insert a dot, so that the chapter numbers in the table of content show up as 1. First Section, 1.1. First Subsection etc. (notice the dot after the number). I've been unable to insert one however – I'm assuming I need to know some low-lever TeX command that I don't know, maybe?
    – zrajm
    Commented Apr 12, 2022 at 0:03
  • @zrajm It does not look like your problem is related to this question. Therefore, I suggest that you make a new question with a minimal, but complete example showing your problem. Commented Apr 12, 2022 at 19:07

You must log in to answer this question.

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