0
\usepackage{titlesec}
\titleformat{\section}
{\bfseries}{\thesection}{1em}{\MakeUppercase}

Using this package I can make section upper case:

\section{This will be upper case}

enter image description here

but in toc will be original text from section, is it possible to make uppercase also in toc? enter image description here

1
  • Yes, you might take a look at the companion package titletoc. – Bernard Jul 17 '18 at 8:59
2

A solution without an additional package could be a new \Section command that changes both by using the \section command with uppercase text.

\documentclass{article}
\newcommand{\Section}[2][]{%
  \def\shortsec{#1}%
  \ifx\shortsec\empty%
    \section{\MakeUppercase{#2}}
  \else%
    \section[\MakeUppercase{#1}]{\MakeUppercase{#2}}
  \fi}
\begin{document}
\tableofcontents
\Section[t]{title}
\end{document}

result

edit: to make it work with the hyperref package, one needs to adjust the code (with the \ifdefined line I modified the behavior, so it can be used with and without hyperref):

\documentclass{article}
\usepackage{hyperref}

\ifdefined\texorpdfstring\else\newcommand{\texorpdfstring}[2]{#1}\fi
\newcommand{\Section}[2][]{%
  \def\shortsec{#1}%
  \ifx\shortsec\empty%
    \section{\texorpdfstring{\MakeUppercase{#2}}{#2}}
  \else%
    \section[\texorpdfstring{\MakeUppercase{#1}}{#1}]{\texorpdfstring{\MakeUppercase{#2}}{#2}}
  \fi}
\begin{document}
\tableofcontents
\Section{title}
\end{document}

Probably there is a more beautiful approach using lua(la)tex, I could look into that if someone is interested.

edit2: To substitute the old \section command, one could do:

\documentclass{article}
\usepackage{hyperref}

\ifdefined\texorpdfstring\else\newcommand{\texorpdfstring}[2]{#1}\fi
\let\oldsection\section

\makeatletter
\renewcommand{\section}{\@ifstar{\@sSection}{\@Section}}
\newcommand{\@sSection}[2][]{%
  \oldsection*{\texorpdfstring{\MakeUppercase{#2}}{#2}}}
\newcommand{\@Section}[2][]{%
  \def\shortsec{#1}%
  \ifx\shortsec\empty%
    \oldsection{\texorpdfstring{\MakeUppercase{#2}}{#2}}
  \else%
    \oldsection[\texorpdfstring{\MakeUppercase{#1}}{#1}]{\texorpdfstring{\MakeUppercase{#2}}{#2}}
  \fi}
\makeatother

\begin{document}
\tableofcontents
\section{title}
\end{document}

edit3: In case you want to keep the old \section* behavior, e.g., to keep the old title of toc rather than an uppercase version, you can replace

\renewcommand{\section}{\@ifstar{\@sSection}{\@Section}}

with

\renewcommand{\section}{\@ifstar{\oldsection*}{\@Section}}
6
  • could you make code for \Section{SectionName} without second input -> \Section{SectionName}{SectionName} because data between this two {}{} should always be the same. Thank you :) – Beginner Jul 17 '18 at 9:49
  • @Beginner Sorry, I do not understand this request, you can easily use \Section{SectionName} as usual. The only thing missing is currently a starred version \Section*, if you want I can add that. – nox Jul 17 '18 at 9:54
  • sorry, I was confused with [t] inside of this code \Section[t]{title}. If I remove [t], data in toc and section will be the same. :) – Beginner Jul 17 '18 at 9:59
  • @Beginner Indeed, this is just the short version of the section title. I wanted to show it's working with that as well. Usually you use this optional argument in case you have a (very) long section title that should appear shortened in the toc. – nox Jul 17 '18 at 10:02
  • 1
    @Beginner Sure, you are using the hyperref package, I'll update the code to make it compatible. – nox Jul 17 '18 at 10:47

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.