2

I'm having a formatting issue with my document's table of contents (ToC).

Specifically, I'm trying to capitalize the first letter of each word in the ToC entries using the \capitalisewords command from the mfirstuc package. I'm also using the \titlecontents command from the titleps package to format my ToC.

However, \capitalisewords does not seem to work as expected. The ToC entries remain unchanged.

Interestingly, other case-changing commands like \MakeUppercase work perfectly.

Here's a minimal working example (MWE) that demonstrates the issue:

\documentclass{report}
\usepackage{lipsum, mfirstuc, titletoc}
\usepackage[colorlinks = true]{hyperref}

\titlecontents{chapter}% <section-type>
  [0pt]% <left>
  {\addvspace{.5em}}% <above-code>
  {\thecontentslabel\quad\capitalisewords}% <numbered-entry-format>
  {\capitalisewords}% <numberless-entry-format>
  {\bfseries\hfill\contentspage}% <filler-page-format>
  []%

\titlecontents{section}[3.8em]{}{\contentslabel{2.3em}\capitalisewords}{\hspace*{-2.3em}\capitalisewords}{\titlerule*[1pc]{.}\contentspage}

\titlecontents{subsection}[7em]{}{\contentslabel{3.2em}\capitalisewords}{\hspace*{-3.2em}\capitalisewords}{\titlerule*[1pc]{.}\contentspage}

\begin{document}

\tableofcontents

\chapter*{starred chapter title}
\addcontentsline{toc}{chapter}{starred chapter title}
\lipsum[1]

\chapter{chapter title}
\lipsum[2]

\section{section title}
\lipsum[3]

\subsection{subsection title}
\lipsum[3-18]

\end{document}

Here is the resulting ToC:

enter image description here

2 Answers 2

2

You can use etoc as a kind of a broker, I started from 28. Another compatibility mode in its documentation.

\documentclass{report}
\usepackage{lipsum, mfirstuc, titletoc}
\usepackage[colorlinks = true]{hyperref}

\titlecontents{chapter}% <section-type>
  [0pt]% <left>
  {\addvspace{.5em}}% <above-code>
  {\thecontentslabel\quad}% <numbered-entry-format>
  {}% <numberless-entry-format>
  {\bfseries\hfill\contentspage}% <filler-page-format>
  []%

\titlecontents{section}[3.8em]{}{\contentslabel{2.3em}}{\hspace*{-2.3em}}{\titlerule*[1pc]{.}\contentspage}

\titlecontents{subsection}[7em]{}{\contentslabel{3.2em}}{\hspace*{-3.2em}}{\titlerule*[1pc]{.}\contentspage}


\newcommand\CapWrd[1]{\expandafter\capitalisewords\expandafter{#1}}

\usepackage{etoc}
\makeatletter
\etocsetstyle{chapter}
   {}
   {\etocifnumbered
     {\l@chapter{\numberline{\etocthenumber}\etoclink{\CapWrd{\etocthename}}}{\etocpage}}
     {\l@chapter{\etoclink{\CapWrd{\etocthename}}}{\etocpage}}%
   }
   {}
   {}
\etocsetstyle{section}
   {}
   {\etocifnumbered
     {\l@section{\numberline{\etocthenumber}\etoclink{\CapWrd{\etocthename}}}{\etocpage}}
     {\l@section{\etoclink{\CapWrd{\etocthename}}}{\etocpage}}%
   }
   {}
   {}
\etocsetstyle{subsection}
   {}
   {\etocifnumbered
     {\l@subsection{\numberline{\etocthenumber}\etoclink{\CapWrd{\etocthename}}}{\etocpage}}
     {\l@subsection{\etoclink{\CapWrd{\etocthename}}}{\etocpage}}%
   }
   {}
   {}
\makeatother
\begin{document}

\tableofcontents

\chapter*{starred chapter title}
\addcontentsline{toc}{chapter}{starred chapter title}
\lipsum[1]

\chapter{chapter title}
\lipsum[2]

\section{section title}
\lipsum[3]

\subsection{subsection title}
\lipsum[3-18]

\end{document}

Watch the preamble the additions start with \newcommand\CapWrd then \usepackage{etoc}. And \capitalisewords was removed entirely from the titletoc config.

etoc toc

explanation: \etocthename contains the title (with no hyperref decoration). The \CapWrd wrapper uses \capitalisewords on a one-expanded argument. Ah wait I see this is \xcapitalisewords from mfirstuc package. (strange name because x implies full expansion usually). So in the above you can do rather \let\CapWrd\xcapitalisewords or use the latter directly.

remark: if using \etocnumber in place of \etocthenumber there will be an hyperlink from the numbers, and they will show red, from your hyperref configuration. The page numbers use \etocpage but by default hyperref uses no hyperlink for page number so they remain black (and not hyperlinked...).

10
  • 1
    I am no titletoc savvy so I don't knwo what in the configuration from the OP implies that the title numbers lost their hyperlinks (which they have normally). In my answer etocthenumber is used to give exact same result as without etoc but if you want hyperlinked numbers (as would be the case without those titletoc configuration, use \etocnumber.
    – user691586
    Commented Jun 27 at 19:44
  • 1
    \l@chapter, \l@section, ..., are core LaTeX commands defined by the standard classes. A .toc file contains a bunch of \contentsline command which ultimately use them. When the title is numbered there is a \numberline command inside the argument containing the title text. If you look inside titletoc source code you find for example \def\l@section{\ttl@lselect{section}} which means that titletoc redefines these macros to do the typesetting as configured by \titlecontents command by user. So the etoc configuration in my answer is a trick to intervene on the title itself.
    – user691586
    Commented Jun 28 at 7:47
  • 1
    Regarding your code you are using \etocname not \etocthename contrarily to my answer. As a result, the hyperref data is there. Your LaTeX3 definition at some point attempts to do a full expansion, and this causes the error because \hyper@@link expands to something starting with \let \Hy@reserved@a \relax, the \let and \relax are unexpandable and \Hy@reserved@a is not yet defined hence the Undefined control sequence. error. Use \etocthename and add \etoclink wrapper \etoclink{\myCapitaliseWords{\etocthename}} if you want hyperlink.
    – user691586
    Commented Jun 28 at 7:56
  • 1
    The problems with OP template have been resolved in chat
    – user691586
    Commented Jun 29 at 7:35
  • 1
    ;-) sadly I can't access my PayPal for years because I can not speak to a human to explain I need to update my credit card details and no cellular phone was connected to my account
    – user691586
    Commented Jun 29 at 10:24
2

Well the argument the last command in the content declaration receives is not simple text. In the case of the chapter, it is for example \Hy@toclinkstart {\numberline {1}chapter title}\Hy@toclinkend and \capitalisewords can't handle that.

You could try \text_titlecase_all:nn instead.

\documentclass{report}
\usepackage{lipsum}
\usepackage{titletoc}

\usepackage[colorlinks = true]{hyperref}
\ExplSyntaxOn
\let\MakeTitlecaseAll\text_titlecase_all:nn
\ExplSyntaxOff

\titlecontents{chapter}% <section-type>
  [0pt]% <left>
  {\addvspace{.5em}}% <above-code>
  {\thecontentslabel\quad\MakeTitlecaseAll{en}}% <numbered-entry-format>
  {\MakeTitlecaseAll{en}}% <numberless-entry-format>
  {\bfseries\hfill\contentspage}% <filler-page-format>
  []%

\titlecontents{section}[3.8em]{}{\contentslabel{2.3em}\MakeTitlecaseAll{en}}{\hspace*{-2.3em}\MakeTitlecaseAll{en}}{\titlerule*[1pc]{.}\contentspage}

\titlecontents{subsection}[7em]{}{\contentslabel{3.2em}\MakeTitlecaseAll{en}}{\hspace*{-3.2em}\MakeTitlecaseAll{en}}{\titlerule*[1pc]{.}\contentspage}

\begin{document}

\tableofcontents

\chapter*{starred chapter title}
\addcontentsline{toc}{chapter}{starred chapter title}
\lipsum[1]

\chapter{chapter title}
\lipsum[2]

\section{section title}
\lipsum[3]

\subsection{subsection title}
\lipsum[3-18]

\end{document}

enter image description here

2
  • Thanks for the suggestion! However, redefining it this way means sacrificing some functionalities that come with the mfirstuc package, specifically the \MFUnocap command, which allows certain words to be excluded from capitalization. I was wondering if there's a way to build a new command on top of \capitalisewords that can handle the passed arguments, apply the desired capitalization, and then return the text. @UlrikeFischer
    – Mycroft_47
    Commented Jun 27 at 18:23
  • In my document, I'm not using \capitalisewords directly but through an intermediary command \myCapitaliseWords (see here) that does some preprocessing on the text. This command is my adaptation of this answer. So, I'm curious if it's possible to modify this adaptation to handle arguments properly. Any ideas? @UlrikeFischer
    – Mycroft_47
    Commented Jun 27 at 18:23

You must log in to answer this question.

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