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.

Two problems arise when I add lines to the table of contents (TOC), and to the table of tables (TOT):

  1. The TOC lines are not properly numbered.
  2. The TOT lines don't show on the TOT.

Any idea how to fix that ?

%!TEX TS-program = pdflatex
\documentclass[10pt,a4paper]{book}
\usepackage{array}
%------------ 
\newcommand{\SampleTable}{
\begin{table}
\caption{Title of Sample Table}
\begin{tabular}{ l c r }
  1 & 2 & 3 \\
\end{tabular}
\end{table}
}
%------------ 
\begin{document}
\frontmatter 
\setcounter{secnumdepth}{5}  
\setcounter{tocdepth}{5}    
\tableofcontents
\newpage

\section{section one}
\subsection{subsection one}

\setcounter{subsubsection}{0}
\addcontentsline{toc}{subsubsection}{line added to TOC: subsubsection one}
\addcontentsline{tot}{subsubsection}{line added to TOT: subsubsection one}  

\addcontentsline{toc}{subsubsection}{line added to TOC: subsubsection two}
\addcontentsline{tot}{subsubsection}{line added to TOT: subsubsection two}  

\subsubsection{subsubsection after addcontentsline: three}
\SampleTable
\backmatter
\listoftables

\end{document}
share|improve this question
1  
While the \tableofcontents uses toc for an abbreviation, \listoftables uses lot (not tot). Also, what numbers do you want to add to the ToC/LoT? \addcontentsline only adds what you supply, and you didn't supply any number. –  Werner Mar 8 '13 at 20:37
    
it's "list of tables" (lot, accessed by \listoftables), not "table of tables". –  barbara beeton Mar 8 '13 at 20:40
    
I would just like to get to usual latex numbering: 0.1.1.1 for the first line added, 0.1.1.2 for the second line added, 0.1.1.3 for the following subsubsection. ... and thanks for LOT ! –  SDrolet Mar 8 '13 at 20:45
    
But why are you using \addcontentsline manually you should almost never need to do that \caption should add the entries automatically to the lot and \(sub)section should automatically add entries to the toc. Also "usual" numbering is 1.1.1 not 0.1.1 you are using book so the intention is that all \section are within a \chapter If you want \section to be the top level section unit you should use article –  David Carlisle Mar 8 '13 at 22:45

1 Answer 1

up vote 5 down vote accepted

Here are the two answers, and the resulting code:

  1. As Werner mentioned, \addcontentsline doesn't include the numbering of the line added, according to the level where it was added (ex.: subsubsection). So, we have to add the numbers (value of the chapter, value of the section, value of the subsection, value of the subsubsection), with the help of counters (see http://en.wikibooks.org/wiki/LaTeX/Counters).

        \addtocounter{subsubsection}{1}
        \addcontentsline{toc}{subsubsection}{\arabic{chapter}.\arabic{section}.\arabic{subsection}.\arabic{subsubsection}~~ line added to TOC: subsubsection one}
    

A better option is pointed out by Werner (comment below): \protect\numberline{\thesubsubsection}

        \addcontentsline{toc}{subsubsection}{\protect\numberline{\thesubsubsection} line added to TOC: subsubsection one}
  1. Indeed, TOT doesn't exist, but "lot" does.

    %!TEX TS-program = pdflatex
    \documentclass[10pt,a4paper]{book}
    \usepackage{array}
    %------------ 
    \newcommand{\SampleTable}{
    \begin{table}
    \caption{Title of Sample Table}
    \begin{tabular}{ l c r }
      1 & 2 & 3 \\
    \end{tabular}
    \end{table}
    }
    %------------ 
    \begin{document}
    \frontmatter 
    \setcounter{secnumdepth}{5}  
    \setcounter{tocdepth}{5}    
    \tableofcontents
    \newpage
    
    \part{part one}
    \chapter{chapter one}
    \section{section one}
    \subsection{subsection one}
    
    \addtocounter{subsubsection}{1}
        \addcontentsline{toc}{subsubsection}{\arabic{chapter}.\arabic{section}.\arabic{subsection}.\arabic{subsubsection}~~ line added to TOC: subsubsection one}
    
    \addtocounter{table}{1}
    \addcontentsline{lot}{section}{\arabic{table}~~~~ line added to TOT: subsubsection one}  
    
    \addtocounter{subsubsection}{1}
    \addcontentsline{toc}{subsubsection}{\arabic{chapter}.\arabic{section}.\arabic{subsection}.\arabic{subsubsection}~~ line added to TOC: subsubsection two}
    \addtocounter{table}{1}
    \addcontentsline{lot}{section}{\arabic{table}~~~~ line added to TOT: subsubsection two}  
    
    \subsubsection{subsubsection after addcontentsline: three}
    \SampleTable
    \backmatter
    \listoftables
    
    \end{document}
    
    %% Reference on counters: http://en.wikibooks.org/wiki/LaTeX/Counters
    

And the code with Werner's better option:

    %!TEX TS-program = pdflatex
    \documentclass[10pt,a4paper]{book}
    \usepackage{array}
    %------------ 
    \newcommand{\SampleTable}{
    \begin{table}
    \caption{Title of Sample Table}
    \begin{tabular}{ l c r }
      1 & 2 & 3 \\
    \end{tabular}
    \end{table}
    }
    %------------ 
    \begin{document}
    \frontmatter 
    \setcounter{secnumdepth}{5}  
    \setcounter{tocdepth}{5}    
    \tableofcontents
    \newpage

    \part{part one}
    \chapter{chapter one}
    \section{section one}
    \subsection{subsection one}

        \addtocounter{subsubsection}{1}
        \addcontentsline{toc}{subsubsection}{\protect\numberline{\thesubsubsection} line added to TOC: subsubsection one}

    \addtocounter{table}{1}
    \addcontentsline{lot}{section}{\arabic{table}~~~~ line added to TOT: subsubsection one}  

    \addtocounter{subsubsection}{1}
    \addcontentsline{toc}{subsubsection}{\protect\numberline{\thesubsubsection} line added to TOC: subsubsection two}
    \addtocounter{table}{1}
    \addcontentsline{lot}{section}{\arabic{table}~~~~ line added to TOT: subsubsection two}  

    \subsubsection{subsubsection after addcontentsline: three}
    \SampleTable
    \backmatter
    \listoftables

    \end{document}

    %% Reference on counters: http://en.wikibooks.org/wiki/LaTeX/Counters
share|improve this answer
2  
I truly don't understand what is going on here... but instead of using \arabic{chapter}.\arabic{section}.\arabic{subsection}.\arabic{subsubsection}, you can just use \protect\numberline{\thesubsection}. Compare that to adding \subsubsection{test} somewhere, and look at the indentation/spacing. \numberline is the macro that sets the sectional unit number in a box of specific width, making things line up properly. –  Werner Mar 9 '13 at 0:01
1  
It may seem weird to try to number TOC lines with \addcontentsline, whereas that command is intended to produce unnumbered TOC lines. In fact, in my book, sometimes in the text, I don't want the "title" of the subsubsection to be shown because it takes space, and I need that space for the image I put afterwards. But, I want the image to be indicated in the TOC at the subsubsection level, well numbered. ... Thanks for the other better option for numbering ! –  SDrolet Mar 9 '13 at 0:19

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.