1

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.

  • 1
    Can you make a minimal working example (MWE)? – user36296 Mar 7 '18 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 Mar 7 '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 7 '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. – user36296 Mar 7 '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 7 '18 at 15:25
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}
  • 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 Mar 7 '18 at 17:05
  • Have you also copied \usepackage{xparse}? – Marcel Krüger Mar 7 '18 at 17:08
  • You're version of xparse might also be too old. \RenewExpandableDocumentCommand exists since the xparse version from March 2017. – Marcel Krüger Mar 7 '18 at 17:15
  • I edited the answer to be compatible with older versions. – Marcel Krüger Mar 7 '18 at 18:59
  • Perfect! (indeed the version provided by ubuntu 17.04 is dated 2016-01-19, v6377). Thanks! :-) – laspic Mar 7 '18 at 19:40
1

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

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.