9

How can I insert \MakeUppercase inside the table of contents for sections and subsection?

\renewcommand{\l@section}[2]%
{\@dottedtocline{1}{.5em}{1.3em}%
 {{\bfseries\selectfont#1}}{#2}}
Werner
  • 603,163
Meemphys
  • 111

1 Answers1

9

This is a fairly general approach for changing the appearance of the entries in the table of contents.

The patch with \xpatchcmd* is just a way for avoiding copying the definition from latex.ltx and modifying it. The two places where #7 appears in \addtocontents are replaced by \@nameuse{format#1}{#7} so we can define \formatsection and so on to do what we want to the title in the TOC.

\documentclass{article}

%%% Patching the kernel \@sect command
\usepackage{regexpatch}
\makeatletter
\xpatchcmd*{\@sect}{\fi#7}{\fi\@nameuse{format#1}{#7}}{}{}

%%% for sections and subsections we want uppercase
\protected\def\formatsection{\MakeUppercase}
\protected\def\formatsubsection{\MakeUppercase}

%%% the other titles are left unchanged
\let\formatsubsubsection\@firstofone
\let\formatparagraph\@firstofone
\let\formatsubparagraph\@firstofone

%%% the following is necessary only if hyperref is used
\AtBeginDocument{%
  \pdfstringdefDisableCommands{%
    \let\formatsection\@firstofone
    \let\formatsubsection\@firstofone
  }%
}
\makeatother

\usepackage{hyperref}

\setcounter{secnumdepth}{3}

\begin{document}

\tableofcontents

\section{This is a section}

\subsection{This is a subsection}

\subsubsection{This is a subsubsection}

\end{document}

enter image description here

egreg
  • 1,121,712
  • This works great, but how would you do it for \section*? – Paulius K. May 26 '13 at 21:10
  • @PauliusK. \section* doesn't go to the TOC by default. Just add \MakeUppercase in the manually added \addcontentsline. – egreg May 26 '13 at 21:15
  • Well this is embarassing... Nevertheless, your answer is the cleanest sollution I've found for uppercasing sections in ToC. Doing it is way too difficult in LaTeX. – Paulius K. May 26 '13 at 21:26
  • @PauliusK. Maybe classes such as KOMA-Script ones (scrartcl, scrreprt or scrbook) or memoir provide "native" methods (although I'm not sure). Titles in all capitals are heavy and, in my opinion, should be avoided. – egreg May 26 '13 at 21:29