Is there another way to change section names in ToC to uppercase besides Uppercase sections and subsections on ToC Cause this one doesn't work for me very well. Can it be done with tocloft package?
1 Answer
Update: for a solution working with hyperref, see below.
An option, patching \l@section
:
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\l@section}{#1}{\MakeUppercase{#1}}{}{}
\makeatother
\begin{document}
\tableofcontents
\section{A test section}
\section{Another test section}
\end{document}
If your section titles contain math or commands (such as \label
), it's best to use \MakeTextUppercase
from the textcase
package:
\documentclass{article}
\usepackage{textcase}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\l@section}{#1}{\MakeTextUppercase{#1}}{}{}
\makeatother
\begin{document}
\tableofcontents
\section{A test section $a=b$}\label{sec:test}
\section{Another test section with a reference:~\protect\ref{sec:test}}
\end{document}
The solutions above won't work if the hyperref
package is used. In this case, one can tamper with \contentsline
(this borrows some code from Heiko Oberdiek's answer to this question in comp.text.tex
):
\documentclass{article}
\usepackage{textcase}
\usepackage{hyperref}
\makeatletter
\let\oldcontentsline\contentsline
\def\contentsline#1#2{%
\expandafter\ifx\csname l@#1\endcsname\l@section
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\oldcontentsline{#1}{\MakeTextUppercase{#2}}%
}{%
\oldcontentsline{#1}{#2}%
}%
}
\makeatother
\begin{document}
\tableofcontents
\section{A test section $a=b$}\label{sec:test}
\section{Another test section with a reference:~\ref{sec:test}}
\end{document}
The above approach also works using \MakeUppercase
instead of \MakeTextUppercase
; if this is so, then textcase
is not necessary.
-
3Will that work with
hyperref
(most people tend to forget to mention that they also usehyperref
– daleifCommented Jan 31, 2014 at 22:50 -
@daleif Good point! No, it won't work with
hyperref
. I'll add a remark about this. Commented Jan 31, 2014 at 22:53 -
1@daleif now I've added a solution compatible with
hyperref
. Commented Feb 1, 2014 at 0:02 -
For some reason, this does not work with polyglossia and right to left languages. I tried with both Hebrew and Syriac. Any suggestions?– PaulCommented Oct 30, 2018 at 13:10