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}}
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityHow 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}}
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}
\section*
doesn't go to the TOC by default. Just add \MakeUppercase
in the manually added \addcontentsline
.
– egreg
May 26 '13 at 21:15
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
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\documentclass{...}
and ending with\end{document}
. – Marco Daniel May 11 '13 at 7:54