[Tex/LaTex] Customize section numbering in TOC

numberingsectioningtable of contents

I'm working with report class and I would like to customize headings of chapters, sections and subsections, abvoe all the look of numbers before their respective headings, both in the text and in the contents.

My Probolem

I've customized \thechapter, \thesection and \thesubsection like that:

    \renewcommand{\thechapter}{\Ordinalstring{chapter}}
    \renewcommand{\thesection}{%
        \textls{%
            \fontencoding{T1}\fontfamily{phv}\fontsize{9pt}{10.8pt}\selectfont%
            \Numberstring{chapter}.\numberstring{section}%
        }%
    }
    \renewcommand{\thesubsection}{%
        \textls[100]{%
            \fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont%
            \arabic{chapter}.\arabic{section}.\arabic{subsection}%
        }%
    }

Now, for table of contents I would like to have a different look for these counters. That's to say, I would like my index looks like this:

Chapter 1
  Title of Chapter                                              thepage
  1.1
    Title of Section .......................................... thepage
    1.1.1
      Title of Subsection ..................................... thepage

For chapters I've changed the addcontentsline declaration in \@chapter definition as follows:

    \addcontentsline{toc}{chapter}{\protect\numberline{Chapter \arabic{chapter}}\newline\hspace*{.7em} #1} % space is not exact for the time being 

and so in toc I have ''Chapter 1'', ''Chapter 2'' and so on.

I would like to make the same thing for sections and subsections.

Using titlesec and titletoc packages

After some research I've found titlesec and titletoc packages and using them my code is this:

    \documentclass[a4paper]{report}
    \usepackage[english]{babel}
    \usepackage[latin1]{inputenc}
    \usepackage[scaled=0.92]{helvet}
    \usepackage{mathptmx}
    \usepackage[T1]{fontenc}
    \usepackage{lipsum}
    \usepackage[tracking=true]{microtype}
    \usepackage{fmtcount}
    %
    \renewcommand{\thechapter}{\Ordinalstring{chapter}}
    \renewcommand{\thesection}{%
        \textls{%
            \fontencoding{T1}\fontfamily{phv}\fontsize{9pt}{10.8pt}\selectfont%
            \Numberstring{chapter}.\numberstring{section}%
        }%
    }
    \renewcommand{\thesubsection}{%
        \textls[100]{%
            \fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont%
            \arabic{chapter}.\arabic{section}.\arabic{subsection}%
        }%
    }
    \usepackage[rigidchapters,nobottomtitles*,pageatnewline]{titlesec}
        \titlespacing*{\section}
                      {0em}
                      {2em}
                      {0.5em}
        \titlespacing*{\subsection}
                      {0em}
                      {2em}
                      {0.5em}
        \titleformat{\section}
                    [display]
                    {\fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont}
                    {\thesection}
                    {1ex}
                    {}
                    {}
        \titleformat{\subsection}
                    [display]
                    {\fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont}
                    {\thesubsection}
                    {0em}
                    {}
                    {}
    %
    \usepackage{titletoc}
        \titlecontents{chapter}
                      [0em]
                      {\bfseries}
                      {Chapter \thecontentslabel\\*\hspace*{.7em}}
                      {}
                      {\hfill\contentspage}
        \titlecontents{section}
                      [1.5em]
                      {}
                      {\thecontentslabel\\*\hspace*{.7em}}
                      {}
                      {\titlerule*[.5pc]{.}\contentspage}
        \titlecontents*{subsection}
                      [3.8em]
                      {}
                      {\thecontentslabel\\*\hspace*{.7em}}
                      {}
                      {\titlerule*[.5pc]{.}\contentspage}
    %
    \begin{document}
    \chapter{Foo}

    \lipsum[1]

    \section{Bar}

    \lipsum[2]

    \subsection{Qux}

    \lipsum[3]

    \section{Quux}

    \lipsum[4]

    \subsection{Quux1}

    \lipsum[5]

    \chapter{foo}

    \section{bar}

    \lipsum[4]

    \subsection{qux}

    \lipsum[5]

    \tableofcontents
    \end{document}

Here I don't want to change the \thechapter contents for toc, but only for sections and subsectios (anyway I could change chapter counter for toc as I made above in \@chapter definition and so, declaring \titlecontents{}[]{}{}{}{} for chapter as above, \thecontentslabel would contain arabic numbers for chapters).

I've thought to customize \thecontentslabel or the definition of \numberline (at line 433 in titletoc.sty) in \ttl@tocentry.

As I undersand it, \numberline is responsible for printing numbers of sections and subsections in toc, but I don't know how to redefine it…

It would be possible to redefine \thecontentslabel only for sections and subsection?

Please, could I have an explanation about the behavior of \thecontentslabel, expecially its definition?

If I try to declare \thecontentslabel after \begin{document}, why does \LaTeX give me this error message?

! Undefined control sequence.
l.76 \thecontentslabel

Some attempts

I've tried with this:

    \newcommand{\originalthecontentslabel}{}
    \global\let\originalthecontentslabel\thecontentslabel
    \gdef\thecontentslabel#1{\originalcontentslabel{\arabic{#1}}}

but it seems not to be considered by \LaTeX. I think that titletoc overwrites my re-definition (I've tried to put it first beforse titletoc and then after it, but it's not considered).

Then, I've tried with this:

    \def\numberline##1{\global\ttl@labeltrue\gdef\thecontentslabel{\@arabic##1}}%

and this:

    \def\numberline##1{\global\ttl@labeltrue\gdef\thecontentslabel{\arabic{##1}}}%

but they don't work and \LaTeX prints this error message:

! Missing number, treated as zero.
<to be read again>
\let
l.2 ...{\numberline {\Ordinalstringnum {1}}Foo}{1}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

Summary

In summary,

  • how to edit sections and subsection counters in order to have different formats for their numbers in the table of contents without using titletoc?
  • how to make the same thing in the above item with titletoc (or other suitable package…)?

Hope to have been carefull enought!

Best Answer

It's definitely not a good idea to define \thesection with formatting instructions. Also, you want to have \thechapter to expand to \arabic{chapter}, so that you need to change its appearance only when typesetting the chapter head.

Here are some changes to your settings. However, I don't like at all the way section and subsection titles are set, but the document is yours.

  1. No redefinition of \thechapter, \thesection and \thesubsection

  2. The \@makechapterhead is patched to print \Ordinalstring{chapter} instead of \thechapter.

  3. Some tidying of the code.

\documentclass[a4paper]{report}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[scaled=0.92]{helvet}
\usepackage{mathptmx}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage[tracking=true]{microtype}
\usepackage{fmtcount}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@makechapterhead}
  {\thechapter}
  {\Ordinalstring{chapter}}
  {}{}
\makeatother

\usepackage[rigidchapters,nobottomtitles*,pageatnewline]{titlesec}

\titlespacing*{\section}
              {0em}
              {2em}
              {0.5em}
\titlespacing*{\subsection}
              {0em}
              {2em}
              {0.5em}
\titleformat{\section}
            [display]
            {\fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont}
            {\thesection}
            {1ex}
            {}
            {}
\titleformat{\subsection}
            [display]
            {\fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont}
            {\thesubsection}
            {0em}
            {}
            {}
%
\usepackage{titletoc}
\titlecontents{chapter}
              [0em]
              {\bfseries}
              {Chapter \thecontentslabel\\*\hspace*{.7em}}
              {}
              {\hfill\contentspage}
\titlecontents{section}
              [1.5em]
              {}
              {{\sffamily\thecontentslabel}\\*\hspace*{.7em}}
              {}
              {\titlerule*[.5pc]{.}\contentspage}
\titlecontents*{subsection}
              [3.8em]
              {}
              {{\sffamily\thecontentslabel}\\*\hspace*{.7em}}
              {}
              {\titlerule*[.5pc]{.}\contentspage}
%

\begin{document}
\chapter{Foo}

\lipsum[1]

\section{Bar}

\lipsum[2]

\subsection{Qux}

\lipsum[3]

\section{Quux}

\lipsum[4]

\subsection{Quux1}

\lipsum[5]

\chapter{foo}

\section{bar}

\lipsum[4]

\subsection{qux}

\lipsum[5]

\tableofcontents

\end{document}

enter image description here

enter image description here

Related Question