[Tex/LaTex] Getting error using \MakeUppercase with tocloft

capitalizationtable of contentstocloft

Trying to use tocloft package to customize a table of contents, but I am running into errors using \MakeUppercase or \uppercase with certain commands, namely \cftsecfont.

When I run the following code I get errors suggesting there are missing or extra }s. Similar errors when I replace \MakeUppercase with \uppercase. If I delete that command entirely it runs with no errors.

\documentclass{article}
\usepackage{tocloft}
\begin{document}
\renewcommand{\cftsecfont}{\bfseries\MakeUppercase}
\tableofcontents
\section{Section One}
\subsection{Subsection}
\section{Section Two}
\end{document}

Similar errors using \cftsubsecfont, etc. What am I doing wrong?

Best Answer

You can patch the \l@section (and friends) macros - they are responsible for setting the section/subsection titles in the ToC - to \MakeUppercase the first argument it receives (which includes the number and title):

enter image description here

\documentclass{article}
\usepackage{tocloft,etoolbox}% http://ctan.org/pkg/{tocloft,etoolbox}
\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\l@section}{#1}{\MakeUppercase{#1}}{}{}% Sections use UPPERCASE in ToC
\patchcmd{\l@subsection}{#1}{\MakeUppercase{#1}}{}{}% Subsections use UPPERCASE in ToC
\makeatother
\begin{document}
\tableofcontents
\section{Section One}
\subsection{Subsection}
\section{Section Two}
\end{document}

The above patch is suggested based on the construction of \l@section (and friends) (from tocloft.dtx):

\renewcommand*{\l@section}[2]{%
  \ifnum \c@tocdepth >\z@
     % <snip>
     {\cftsecfont #1}\nobreak
     \cftsecfillnum{#2}}%
  \fi}

After the patch, \l@section resembles

\renewcommand*{\l@section}[2]{%
  \ifnum \c@tocdepth >\z@
     % <snip>
     {\cftsecfont \MakeUppercase{#1}}\nobreak
     \cftsecfillnum{#2}}%
  \fi}

which forces an UPPERCASE title to be set.