TransWikia.com

Automatic Capitalization of sectioning headings?

TeX - LaTeX Asked on October 4, 2021

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}

3 Answers

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}%
             {normalfontLargescshapebfseries}}

renewcommand{subsection}{@startsection{subsection}{2}{z@}%
             {-3.25ex@plus -1ex @minus -.2ex}%
             {1.5ex @plus .2ex}%
             {normalfontlargescshapebfseries}}

renewcommand{subsubsection}{@startsection{subsubsection}{2}{z@}%
             {-3.25ex@plus -1ex @minus -.2ex}%
             {1.5ex @plus .2ex}%
             {normalfontnormalsizescshapebfseries}}
makeatother

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

Correct answer by MBR on October 4, 2021

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}

Answered by Werner on October 4, 2021

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
letoldsectionsection
RenewDocumentCommand{section}{ s o m }{ % Capitalize section headings
    % text_uppercase will replace tl_upper_case in later l3token version
    tl_set:Nxsection_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@oldcontentslinecontentsline
defcontentsline#1#2{%
    expandafterifxcsname l@#1endcsnamel@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}

Answered by TakingItCasual on October 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP