# titlesec ToC formatting — capitalization, subsection numbering for sections, title formatting

I'm a newer user of the detailed formatting capabilities in Latex... I'm having a really hard time figuring out titlesec documentation to do the necessary formatting that I require. Unfortunately, the online examples are not helping me much...

• I'm working in an article class.
• I need uppercase section labels, with the section number including the subsection (e.g. 1.0 INTRODUCTION . . . . . . pg#)
• Subsections and subsubsections should also be included, indented respectively, with nothing beyond the default behavior
• Appendices are not to be uppercase and should be labeled "APPENDIX A - Appendix title . . . pg#)
• Finally, the Table of Contents header should be bold, centered, and uppercase.

Here's the code I have now:

\documentclass[12pt]{article}
\usepackage{blindtext}

\usepackage{titlesec}
\usepackage{titletoc}
\titleformat{\section} % command
[hang]                 % shape
{\normalsize\bfseries} % format
{\thesubsection}       % label
{1em}                  % Spacing between label and title
{\MakeUppercase}       % before code
\titlespacing{\section}% Set the spacing around the title
{0pt}                  % Left
{0pt}                  % Above
{0pt}                  % Below
\titlecontents{section}[2em]{\vspace{12pt}}{\normalfont\normalsize \contentslabel{2em}}{\hspace*{-2em} \MakeUppercase}{}{}

\titleformat{\subsection} % command
[hang]                 % shape
{\bfseries\normalsize} % format
{\thesubsection}       % label
{1em}                  % Spacing between label and title
{}                     % before code
\titlespacing{\subsection}% Set the spacing around the title
{0pt}                  % Left
{0pt}                  % Above
{0pt}                  % Below

\titleformat{\subsubsection} % command
[runin]                % shape
{\bfseries\normalsize} % format
{\thesubsubsection}    % label
{1em}                % Spacing between label and title
{}                     % before code
[.]                    % after code

\begin{document}
\tableofcontents

% Put this after TOC to keep single spacing in TOC:
\setlength{\parskip}{2ex} %--skip lines between paragraphs
\setlength{\parindent}{0pt} %--don't indent paragraphs

\vspace{4em} % Would normally be \newpage

\blinddocument

\end{document}


Sorry... I've spent five hours with minimal progress. I think I'm still learning a lot of the basics that is convoluting this for me... Thanks, Mike

• It's not clear what the numbering for section should be. Like in my answer below? Please explain. – Gonzalo Medina Sep 27 '15 at 1:32

## 1 Answer

Here's one way to do it:

\documentclass[12pt]{article}
\usepackage{blindtext}
\usepackage{etoolbox}
\usepackage{titlesec}
\usepackage{titletoc}

\titleformat{\section} % command
[hang]                 % shape
{\normalsize\bfseries} % format
{\thesubsection}       % label
{1em}                  % Spacing between label and title
{\MakeUppercase}       % before code
\titlespacing{\section}% Set the spacing around the title
{0pt}                  % Left
{0pt}                  % Above
{0pt}                  % Below

\newcommand\RegToCEntries{
\titlecontents{section}[2em]
{\vspace{12pt}}
{\normalfont\normalsize\contentslabel[\thecontentslabel.0]{2em}\uppercase}
{\hspace*{-2em}\uppercase}
{\titlerule*[.75em]{.}\contentspage}
}
\newcommand\AppToCEntries{
\titlecontents{section}[0em]
{\vspace{12pt}}
{\normalfont\normalsize\contentspush{\MakeUppercase{\appendixname}}%
\contentslabel{-1em}\hspace{2em}\makebox[1em][l]{--}%
}
{\hspace*{-2em}}
{\titlerule*[.75em]{.}\contentspage}
}
\AtBeginDocument{\RegToCEntries}
\pretocmd{\appendix}{\AppToCEntries}{}{}
\patchcmd{\tableofcontents}
{\contentsname}
{\hfill\MakeUppercase{\contentsname}\hfill\null}
{}
{}

\titleformat{\subsection} % command
[hang]                 % shape
{\bfseries\normalsize} % format
{\thesubsection}       % label
{1em}                  % Spacing between label and title
{}                     % before code
\titlespacing{\subsection}% Set the spacing around the title
{0pt}                  % Left
{0pt}                  % Above
{0pt}                  % Below

\titleformat{\subsubsection} % command
[runin]                % shape
{\bfseries\normalsize} % format
{\thesubsubsection}    % label
{1em}                % Spacing between label and title
{}                     % before code
[.]                    % after code

\begin{document}

\tableofcontents
\clearpage% just for the example
\blinddocument
\appendix
\section{A test appendix}
\section{Another test appendix}

\end{document}


The result:

# Explanation

1. Center the ToC title.

I used the etoolbox package to patch \tableofcontents:

\patchcmd{\tableofcontents}
{\contentsname}
{\hfill\MakeUppercase{\contentsname}\hfill\null}
{}
{}

2. Formatting for section entries.

I defined two commands \RegToCEntries and \AppToCEntries:

\newcommand\RegToCEntries{
\titlecontents{section}[2em]
{\vspace{12pt}}
{\normalfont\normalsize\contentslabel[\thecontentslabel.0]{2em}\uppercase}
{\hspace*{-2em}\uppercase}
{\titlerule*[.75em]{.}\contentspage}
}
\newcommand\AppToCEntries{
\titlecontents{section}[0em]
{\vspace{12pt}}
{\normalfont\normalsize\contentspush{\MakeUppercase{\appendixname}}%
\contentslabel{-1em}\hspace{2em}\makebox[1em][l]{--}%
}
{\hspace*{-2em}}
{\titlerule*[.75em]{.}\contentspage}
}


The first one gives the desired formatting for regular sections and the seccond one, for sections in appendix; I added Appendix using \contentspush. The first command is used \AtBeginDocument:

\AtBeginDocument{\RegToCEntries}


and the second one is applied automatically after invoking \appendix:

\pretocmd{\appendix}{\AppToCEntries}{}{}


# Suggestion

Your code sets

\titlespacing{\section}% Set the spacing around the title
{0pt}                  % Left
{0pt}                  % Above
{0pt}


which suppresses spacing around section titles; I'd suggest you to use positive values in the last two arguments so section titles will have better vertical spacing with respect to the surrounding text.

• Wow, thanks!! I would not have come up with anything that clean on my own... I seem to have also missed a requirement that I still have the dots and page numbers shown for the sections as well. I think I need \dottedcontents as opposed to \titlecontents, but their syntax appears different, and I'm not sure how to wrap the same behaviors into the \dottedcontents syntax. I also agree with your opinion on the titlespacing, but the format is not up to me... I would change many things, personally. Thanks! – user280607 Sep 27 '15 at 19:13
• I was able to extend your patchcmd's to similarly center and format the List of Tables and Figures headings. Is there a similar way to do that for the References heading or is that defined in the bibiliography style file? – user280607 Sep 27 '15 at 19:54
• @user280607 Please see my updated answer. For the bibliography, if you are not using any special packages, this \patchcmd{\thebibliography} {\refname} {\hfill\MakeUppercase{\refname}\hfill\null} {} {} should be enough; if not, the best thing to do would be to open a fresh new question. – Gonzalo Medina Sep 27 '15 at 23:55
• I need to do the same for the book class. The difference is that appendices are defined as chapters instead of sections? Can you do that? Here's my question: tex.stackexchange.com/q/479343/91816 – Leonardo Castro Mar 13 '19 at 21:12