60

Forgive the stupid simplicity of this question, and please feel free to refer me to an older answer. I'm not quite familiar enough with LaTeX terminology to have found it.

All I want to do is set a different default format for my section titles. Instead of just being bolded and slightly larger than the main text, I'd like them to be of normal weight, the same size as the main text, and all caps.

I'm using the article class.

  • 4
    do any of your section titles contain math? if so, then small caps isn't a good choice, since much of the math may then be taller than the text. (this is why the ams document classes use all real uppercase for running heads, even though it would be more traditional, and proper, to use small caps; the inter-letter spacing in small caps is better, and if designed properly, the shapes are also subtly different from regular caps. but in some situations, math rules.) – barbara beeton Nov 30 '11 at 14:07
66

You can use the titlesec package:

\documentclass{article}
\usepackage{titlesec}
\usepackage{lipsum}

\titleformat{\section}
  {\normalfont\scshape}{\thesection}{1em}{}

\begin{document}

\section{Test Section}
\lipsum[1]

\end{document}

to use small caps for the section titles:

enter image description here

or

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\titleformat{\section}
  {\normalfont}{\thesection}{1em}{\MakeUppercase{#1}}

\begin{document}

\section{Test Section}
\lipsum[1]

\end{document}

to use upper case for the section titles:

enter image description here

If you don't want to use the titlesec package, you can redefine the \section command, as implemented in article.cls; here's an example of such a redefinition to obtain section titles using small caps:

\documentclass{article}

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

\begin{document}

\section{Test Section}

\end{document}
  • That is a perfect explanation but I am curious about what do the commands inside the fourth, fifth and sixth braces do exactly. I referred to the manual but got confused more. – Vesnog Mar 24 '14 at 0:29
  • I'm trying the titlesec package with \MakeUppercase. Assigning <explicit> option to <titlesec> and "#1" obliterates all other section titles (subsection, subsubsection). – Pwdr Oct 16 '15 at 11:55
  • @Pwdr If you are redefining those sectional units, you need to use #1 in the last argument, since explicit is being used. – Gonzalo Medina Oct 16 '15 at 13:31
  • @Gonzalo - I was adding {#1} to the last argument of the section which utilised {\MakeUppercase} but I didn't realise I had to add {#1} to the last argument on ALL sectional units, even if they don't use {\MakeUppercase}. All sorted now! – Pwdr Oct 16 '15 at 16:30
17

I'd like to take a crack at this question, because 5 years ago I landed here and thought titlesec was the best way to do this. That may or may not be true.

The class settings are perhaps obvious to experts, but rather obscure to new users of LaTeX.

$(kpsewhich -var-value TEXMFDIST)/tex/latex/base/article.cls

For the 2016 version, this would result in /usr/local/texlive/2016/texmf-dist/tex/latex/base/article.cls on a Unix-based system.

The relevant parts of the article class extracted from the file.

% Do you want numbering?
% Setup Counters using TeX or LaTeX
\newcounter {part}
\newcounter {section}
\newcounter {subsection}[section]
\newcounter {subsubsection}[subsection]
\newcounter {paragraph}[subsubsection]
\newcounter {subparagraph}[paragraph]

% Format Output of Counters
\renewcommand\thepart{\@Roman\c@part}
\renewcommand\thesection{\@arabic\c@section}
\renewcommand\thesubsection{\thesection.\@arabic\c@subsection}
\renewcommand\thesubsubsection{\thesubsection.\@arabic\c@subsubsection}
\renewcommand\theparagraph{\thesubsubsection.\@arabic\c@paragraph}
\renewcommand\thesubparagraph{\theparagraph.\@arabic\c@subparagraph}

% Define Sectioning Commands (\part is the oddball)
\newcommand\part{%
   \if@noskipsec \leavevmode \fi
   \par
   \addvspace{4ex}%
   \@afterindentfalse
   \secdef\@part\@spart}

\def\@part[#1]#2{%
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{part}%
      \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
    \fi
    {\parindent \z@ \raggedright
     \interlinepenalty \@M
     \normalfont
     \ifnum \c@secnumdepth >\m@ne
       \Large\bfseries \partname\nobreakspace\thepart
       \par\nobreak
     \fi
     \huge \bfseries #2%
     \markboth{}{}\par}%
    \nobreak
    \vskip 3ex
    \@afterheading}
\def\@spart#1{%
    {\parindent \z@ \raggedright
     \interlinepenalty \@M
     \normalfont
     \huge \bfseries #1\par}%
     \nobreak
     \vskip 3ex
     \@afterheading}
\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}
\newcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\large\bfseries}}
\newcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\normalsize\bfseries}}
\newcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                                    {3.25ex \@plus1ex \@minus.2ex}%
                                    {-1em}%
                                    {\normalfont\normalsize\bfseries}}
\newcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
                                       {3.25ex \@plus1ex \@minus .2ex}%
                                       {-1em}%
                                      {\normalfont\normalsize\bfseries}}

% For something like Appendix where sections should appear as letters,
% reset counters to 0 and use \@Alph to make \c@section appear as A.
\newcommand\appendix{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\@Alph\c@section}}

% How should it appear in the table of contents?
% Usually the ToC title is formatted like the sections.
\newcommand\tableofcontents{%
    \section*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{toc}%
    }
  • \z@ = 0, see What does \z@ do?
  • \c@countername = the TeX register that actually holds the integer which is created by the LaTeX macro \newcounter (normally only for backend, hence the @) and which is made globally accessible, see What are the differences between TeX counts and LaTeX counters?
  • \m@ne = register with counter equal to -1, see What do \m@ne‎ and \@M do in the definition of \@makechapterhead?
  • \@M = \mathchardef = \mathchar"2710 = 10000, see above.
  • \leavevmode = leave vertical mode [and implicitly enter horizontal mode], see Function and usage of \leavevmode

    Use \leavevmode for all macros which could be used at the begin of the paragraph and add horizontal boxes by themselves (e.g. in form of text).

  • \@afterindentfalse opposed to \@afterindenttrue LaTeX kernel macro that is part of \if@afterindent. It is setting the @afterindent to false after a sectioning command such that paragraphs directly following headings are not intended. Subsequent paragraphs will be indented when the expansion of this becomes \@afterindenttrue, see http://mirror.easyname.at/ctan/macros/latex/required/tools/indentfirst.pdf
  • \secdef in e.g. \secdef\@part\@spart is effectively a switcher between the starred version \@spart (s for starred part) and the non-starred version \@part. This enables * to influence which macro is expanded, see What does \secdef do?
  • \@plus LaTeX macro = TeX macro \plus, \show\@plus = =macro: ->plus., see What is glue stretching?
  • \@minus LaTeX macro = TeX macro \minus, see above.
  • \ifnum \c@secnumdepth >\m@ne just says if the depth of the section counter is anything greater than -1, then add this heading to the table of contents else...
  • \parindent = amount of space indented
  • \raggedright = A LaTeX kernel macro. It tells TeX to left-align something with disregard to the TeX typographical point system that makes the proper inter-word space adjustments for a pretty, squared paragraph shape (full justification). = \let\\\@centercr\@rightskip\@flushglue \rightskip \@rightskip\leftskip\z@skip\parindent\z@, see Combining leftskip, rightskip and raggedright
  • \@afterheading LaTeX macro that deals with post-heading stuff e.g. preventing a page break between the heading and next line (usually a club line of a paragraph), or that line (club) with its next line. The first is done using \if@nobreak logic, the latter with \clubpenalty=10000=maximum penalty.

    \@nobreaktrue \everypar{\if@nobreak \@nobreakfalse \clubpenalty\@M \if@afterindent \else {\setbox\z@\lastbox} \fi \else \clubpenalty\@clubpenalty \everypar{} \fi} }\@fb@afterHHook

Editing Sections

Just looking around at the article.cls can be very helpful, but there are many commands using the @ sign, which helps ensure that user-level commands will not overlap with "kernel" commands i.e. you will unlikely put \newcommand\@Alph{} in your preamble (if you did, you would get an error, because TeX expects all characters in macro names/command sequence names to have category code 11), see What do \makeatletter and \makeatother do? and What are category codes? for details.

You could redefine the sections to your specifications by using \renewcommand. Note that you need to temporarily change the category code of @ to access the kernel commands. Either surround your definition with

\makeatletter
% insert macro with @ here
\makeatother

OR

\catcode`\@11\relax 
% insert macro with @ here
\catcode`\@12\relax

Grandfather \@startsection

Note that the grandfather of all sectioning commands/headings, except part, is \@startsection. Understanding it can be tricky, but \show\@startsection reveals its definition:

\if@noskipsec \leavevmode \fi \par \@tempskipa #4\relax \@afterindenttrue \ifdim \@tempskipa <\z@ \@tempskipa -\@tempskipa \@afterindentfalse \fi \if@nobreak \everypar {}\else \addpenalty \@secpenalty \addvspace \@tempskipa \fi \@ifstar {\@ssect {#3}{#4}{#5}{#6}}{\@dblarg {\@sect {#1}{#2}{#3}{#4}{#5}{#6}}}.

\@startsection{<name>}{<level>}{<indent>}{<beforeskip>}{<afterskip>}{<style>}*[<altheading>]{<heading>}

To use it to create new headings, you need define the following:

  • A counter with the same name
  • Format the counter output
  • ToC entry line format
  • The \@startsection command itself
  • The header/footer "mark"

Note that when using etoc, it is important to also provide a level for the new command separately from setting it in \@startsection.

For details on \@startsection, see see Where can I find help files or documentation for commands like \@startsection for LaTeX?.

Full Example

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\makeatletter

% Change existing
% Article Class already defined a counter for this heading
\renewcommand\section{\@startsection{section}{1}{\z@}
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\MakeUppercase}}% look at \contentsname

% Add new
\newcounter{mysection}% add counter
\renewcommand\themysection{\@arabic\c@mysection}% set typesetting format of counter to arabic numerals
\newcommand*\l@mysection{\@dottedtocline{1}{0em}{1.5em}} % define table of contents line format
% Article Class \l@section
%\newcommand*\l@section[2]{%
%  \ifnum \c@tocdepth >\z@
%    \addpenalty\@secpenalty
%    \addvspace{1.0em \@plus\p@}%
%    \setlength\@tempdima{1.5em}%
%    \begingroup
%      \parindent \z@ \rightskip \@pnumwidth
%      \parfillskip -\@pnumwidth
%      \leavevmode \bfseries
%      \advance\leftskip\@tempdima
%      \hskip -\leftskip
%      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
%    \endgroup
%  \fi}
\newcommand\mysectionmark[1]{} % define header/footer                            
\newcommand\mysection{\@startsection{mysection}{1}{\z@}
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\bfseries\Large\normalfont\noindent\textcolor{orange}}} % use \textcolor{color}{arg consumed later automatically} instead of \color{color} to avoid a whatsit and disrupt the page breaking mechanism. Add \noindent when using \textcolor here. Indentation can still be achieved by replacing arg 3 of \@startsection.       
\makeatother

\begin{document}
\tableofcontents
---ENDTOC---

\section{Animals}
Lions, tigers, and bears, oh my!

\mysection{Vegetables}
Carrots, parsnips, and broccoli, oh my!

\end{document}

enter image description here

  • 3
    Very thorough indeed! – Mico Oct 30 '16 at 9:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.