14

Is there anyway to make LaTeX automatically capitalize sectioning headings?

That is, I would like this to generate a capitalized headline:

\documentclass{article}
\begin{document}
\section{This should be in title case.}
But it isn't.
\end{document}
lockstep
  • 250,273
vy32
  • 4,780

3 Answers3

16

sectsty provides easy hooks into sectional units. \MakeUppercase turns the title into UPPERCASE, while \titlecap (from titlecaps) turns it into Title Case.

enter image description here

\documentclass{article}
\usepackage{sectsty}% http://ctan.org/pkg/sectsty
\usepackage{titlecaps}% http://ctan.org/pkg/titlecaps
\begin{document}
\tableofcontents
\sectionfont{\MakeUppercase}
\section{This should be in upper case}
It is!
\sectionfont{\titlecap}
\section{This should be in title case}
It is!
\end{document}
Werner
  • 603,163
3

Another solution, using any additional package, it to modify the \section, \subsection etc. macros. It can be done fairly easily with the following

\makeatletter
\renewcommand{\section}{\@startsection {section}{1}{\z@}%
             {-3.5ex \@plus -1ex \@minus -.2ex}%
             {2.3ex \@plus .2ex}%
             {\normalfont\Large\scshape\bfseries}}

\renewcommand{\subsection}{\@startsection{subsection}{2}{\z@}%
             {-3.25ex\@plus -1ex \@minus -.2ex}%
             {1.5ex \@plus .2ex}%
             {\normalfont\large\scshape\bfseries}}

\renewcommand{\subsubsection}{\@startsection{subsubsection}{2}{\z@}%
             {-3.25ex\@plus -1ex \@minus -.2ex}%
             {1.5ex \@plus .2ex}%
             {\normalfont\normalsize\scshape\bfseries}}
\makeatother

The title of the section, subection and subsubsection will then be in small caps.

MBR
  • 1,347
  • I don't think this is a particularly good solution as you'd have to change the hardcoded dimensions almost any time you're using a different document class/document layout. –  Aug 23 '13 at 13:19
  • 1
    In fact, this is the proper way to do it, since it doesn't require packages not part of the standard distribution. Thanks again. – vy32 Aug 23 '13 at 15:25
  • This doesn't work for me. – David Manheim Nov 25 '19 at 12:25
  • Would you have a MWE? – MBR Nov 26 '19 at 14:01
  • @NBR, where should I put a dot if I want a dot following the section title? Thanks in advance... – Saba Mar 04 '21 at 14:21
  • You'd need to dive into the internals of TeX and modify section count format (\@seccntformat) command, as explained in the TeX FAQ. The first example is actually exactly what you want to do. – MBR Mar 05 '21 at 11:00
3

If you're using hyperref, \MakeUppercase will break some ToC hyperrefs. You can use latex3 features as an alternative to \MakeUppercase, you'll just need to redefine \section. MWE:

\documentclass{article}

\usepackage{xparse} \usepackage{hyperref}

\ExplSyntaxOn \let\oldsection\section \RenewDocumentCommand{\section}{ s o m }{ % Capitalize section headings % \text_uppercase will replace \tl_upper_case in later l3token version \tl_set:Nx\section_upper{\cs_if_exist:NTF{\text_uppercase:n} {\text_uppercase:n{#3}}{\tl_upper_case:n{#3}}} \IfBooleanTF{#1} {\oldsection*{\section_upper}} {\IfNoValueTF{#2} {\oldsection{\section_upper}} {\oldsection[#2]{\section_upper}}}} \ExplSyntaxOff

\makeatletter % Always capitalize section headings in ToC (tex.stackexchange.com/a/156917) \let@oldcontentsline\contentsline \def\contentsline#1#2{% \expandafter\ifx\csname l@#1\endcsname\l@section \expandafter@firstoftwo \else \expandafter@secondoftwo \fi {@oldcontentsline{#1}{\MakeUppercase{#2}}}% {@oldcontentsline{#1}{#2}}% } \makeatother

\begin{document} \tableofcontents \section{this should be in upper case} It is! \end{document}