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}}
2
  • 1
    Are you using tocloft? Also, what \documentclass are you using? Your redefinition of \l@section doesn't really help much since it doesn't provide any context.
    – Werner
    May 11 '13 at 5:58
  • Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. May 11 '13 at 7:54
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

4
  • 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

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.