[Tex/LaTex] titlesec ToC formatting — capitalization, subsection numbering for sections, title formatting

formattingtable of contentstitlesec

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} 

Compiled 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

Best 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:

enter image description here

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.