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.

laspic
  • 87
  • 1
  • 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 Mar 07 '18 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 Mar 07 '18 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. – samcarter_is_at_topanswers.xyz Mar 07 '18 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 Mar 07 '18 at 15:25
  • Ok guys, I got it. Sorry, I'm still not familiar with the uses. Here is a MWE. :-) – laspic Mar 07 '18 at 15:28
  • Very helpful are numbers in the bookmarks, see hyperref option bookmarknumbered. I would not add the prefix Chapter. Mostly the bookmarks are displayed in a narrow side bar. A long non-informative prefix just removes the informative title beyond the right margin. – Heiko Oberdiek Mar 07 '18 at 19:25
  • @HeikoOberdiek I didn't know about this option either, and your advice makes sense. Perhaps, it will finally be the better solution... – laspic Mar 08 '18 at 08:13

2 Answers2

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

Heiko Oberdiek
  • 271,626
  • 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 Apr 08 '22 at 08: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, ... – Heiko Oberdiek Apr 08 '22 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 Apr 12 '22 at 00: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 Apr 12 '22 at 00: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. – Heiko Oberdiek Apr 12 '22 at 19:07
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}