Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

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

share|improve this answer
    
Thank you very much for your answer! As a matter of fact, I had thought to use a local redefinition of \thechapter, \thesection and \thesubsection, but I ignored how to do that and where I could look... I've just tried \patchcm for \thesection and \thesubsection and that's all ok, thank you! I'm agree with you about the look of this layout, but it was asked to me about thus. Please, could you explain to me why it's not a good idea define \thesection with formatting instructions? –  Onner Irotsab Dec 15 '12 at 17:35
    
@OnnerIrotsab \thesection, like other \the... commands associated to counters is used to define labels for cross references. So it should only produce the "number" (maybe composite with other numbers) and not intructions on how to print it. Printing the numbers is done in other places: in your case use \titleformat for telling LaTeX how to print the section number. –  egreg Dec 15 '12 at 17:39
    
You've been very clear, thank you very much! –  Onner Irotsab Dec 15 '12 at 18:08
    
@OnnerIrotsab We live not too far from each other (at least as academic work is concerned), did you know? –  egreg Dec 15 '12 at 18:10
    
Yes, I know that! Also, I know about you because of your book on LaTeX programming, that I'm reading, and for your several and very very usefull answers I've found online! –  Onner Irotsab Dec 15 '12 at 18:25

Below there's one possible solution using titlesec and tocloft.

  • The redefinitions for \thechapter, \thesection and \thesubsection are made locally and only for the headings, inside the corresponding \titleformat command.

  • To change the formatting for the ToC entries I used tocloft instead of titletoc since the former interacts in a better way with hyperref.

The code:

\documentclass{book}
\usepackage{fmtcount}
\usepackage{microtype}
\usepackage{tocloft}
\usepackage{titlesec}

\newlength\mylen
\settowidth\mylen{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylen} 

\renewcommand\cftchapaftersnumb{\par\noindent}
\renewcommand\cftchappresnum{\chaptername~}

\renewcommand\cftsecaftersnumb{%
  \par\noindent\hspace*{-0.5\cftsecnumwidth}}
\renewcommand\cftsubsecaftersnumb{%
  \par\noindent\hspace*{-0.5\cftsubsecnumwidth}}

\cftsetindents{subsection}{\dimexpr0.5\cftsecnumwidth+0.5\cftsubsecnumwidth\relax}{1.5em}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{\renewcommand{\thechapter}{\Ordinalstring{chapter}}
\chaptertitlename\ \thechapter}{20pt}{\Huge}

\titleformat{\section}[display]
  {\fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont}{\renewcommand{\thesection}{%
        \textls{%
            \fontencoding{T1}\fontfamily{phv}\fontsize{9pt}{10.8pt}\selectfont%
            \Numberstring{chapter}.\numberstring{section}%
        }%
    }
\thesection}{1em}{}

\titleformat{\subsection}[display]
{\fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont}{\renewcommand{\thesubsection}{%
        \textls[100]{%
            \fontencoding{T1}\fontfamily{phv}\fontsize{11pt}{13.2pt}\selectfont%
            \arabic{chapter}.\arabic{section}.\arabic{subsection}%
        }%
    }
\thesubsection}{1em}{}

\begin{document}

\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One One}
\subsection{Test Subsection One One One}
\chapter{Test Chapter Two}
\section{Test Section Two One}
\subsection{Test Subsection Two One One}
\section{Test Section Two Two}
\subsection{Test Subsection Two Two One}

\end{document}

The ToC:

enter image description here

An inner page showing the modified headings:

enter image description here

share|improve this answer
    
Thank you very much for your answer! I don't believe I'll have to use hyperref, however good to know about tocloft in combination with hyperref! I've tried to put \renewcommand{\thesection}{...} in \tileformat as you have made, but then I've not called \thesecion in \titleformat and I've not deleted the previous redefinitions of \thesection and \thesubsection... I didn't think this could have been the solution and so I've left it go. –  Onner Irotsab Dec 15 '12 at 17:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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