# Tweaking TOC format with amsbook class

I am quite satisfied with the default formatting of the TOC items in the amsbook class, except for one little problem:

If you have more than 10 sections, then the extra digit will push the section name of the double digit sections further to the right, thus failing to align neatly with others.

How can I get rid of this effect, without using \parindent that I had to set to 0 ? Here is the compulsory MWE

\documentclass{amsbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\setlength\parindent{0cm}

\setcounter{tocdepth}{2}

\begin{document}

\tableofcontents

\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{NOT Ok}
Not OK

\end{document}

• A MWE would be fine ;-) – user31729 Jun 27 '15 at 18:32
• section is no environment ;-) – user31729 Jun 27 '15 at 18:48
• @ChristianHupfer Oupss .. Just wanted to get rid of the MWE ... too quickly. – brunoh Jun 27 '15 at 18:55
• How should subsection be typeset? Do you plan to have more than nine subsections for one section? These details are not minor. – egreg Jun 27 '15 at 19:49
• @egreg I want to use the default settings of the amsbook class except for this problem of indentation. And no I do not intend to have more than nine subsections per section. – brunoh Jun 27 '15 at 19:57

You can box the numbers so they will occupy a fixed space.

\documentclass{amsbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{calc}

\makeatletter
\renewcommand{\tocsection}[3]{%
\renewcommand{\tocsubsection}[3]{%
\makeatother

\setcounter{tocdepth}{2}

\begin{document}

\tableofcontents

\chapter{Title}

\section*{Whatever}

\section{Ok}
\subsection{Again}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{NOT Ok}
\subsection{Again}
Not OK

\end{document}


Here is a hack for aligning at the periods instead:

\documentclass{amsbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\makeatletter
\renewcommand{\tocsection}[3]{%
\renewcommand{\tocsubsection}[3]{%
\def\indent@subsec@num#1{%
\ifx#1\@secnumber
\@secnumber
\else
\expandafter\indent@subsec@num@aux\expandafter#1%
\fi
}
\def\indent@subsec@num@aux#1.#2.{\makebox[1em][r]{#1}.#2.}
\makeatother

\setcounter{tocdepth}{2}

\begin{document}

\tableofcontents

\chapter{Title}

\section*{Whatever}

\section{Ok}
\subsection{Again}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{Ok}
OK
\section{NOT Ok}
\subsection{Again}
Not OK

\end{document}


• +1 Almost there ! But as I explained in my last comment in answer to your questions I want the number to be flushleft to their first digit. The indentation part of your hack works fine. Thank you. – brunoh Jun 27 '15 at 20:45
• @brunoh I can't understand what you mean. – egreg Jun 27 '15 at 20:45
• When the section number is above 10, the numbers are all vertically aligned with the dot. I want them to be aligned vertically on their first digit. Then the left side of the TOC is a vertical line. – brunoh Jun 27 '15 at 20:48
• @brunoh I added what I think is your wish. – egreg Jun 27 '15 at 20:55
• Perfect (with the use of the package calc). It works even well here with more than 10 subsections ... which is quite an intellectual relief. – brunoh Jun 27 '15 at 21:05

The amsbook class settings for the ToC can't be changed with tocloft (at least I've not found a way so far).

The settings are a little bit peculiar. The easiest way is to change the justification of the section number and use a further indentation in the \l@section command, changing the 3rd. argument from 1.2pc to 1.5pc, but this can be adapted of course to the personal taste ;-)

The \tocsection command is written to the ToC implicitly by class code, but the changes must be done in this command, in my point of view

\documentclass{amsbook}

\usepackage{pgffor}
\makeatletter

\renewcommand{\tocsection}[3]{%
}

\def\l@section{\@tocline{1}{0pt}{1.5pc}{}{}}
\makeatother
\begin{document}

\tableofcontents
\chapter{First}
\foreach \x in {1,...,20} {%
\section{Section \x}
}
\end{document}


• Thank you for your clear answer ! Unfortunately, a new effect I do not like appears with your method : the numbers are vertically aligned on the dot next to them, and so when there are a lot of subsections they are not aligned at all on the left margin. I do not like that. I would very much like to keep this default behavior, in addition to a kind of indentation for the section name. – brunoh Jun 27 '15 at 19:28
• @brunoh: I'll see what I can do about this, but amsbook is weird in this case, in my point of view – user31729 Jun 27 '15 at 19:33
• Yes I do agree with you. I upvoted your answer but did not accept it as such because of the new issue. Thank you for looking into it ! – brunoh Jun 27 '15 at 19:36
• try this: change the second line of the \tocsection redefinition to \indentlabel{\@ifnotempty{#2}{\llap{\hbox to1.5pc{\ignorespaces#2.\hfill}}}#3 -- this will shift the section number to the left of the space left for it. the width of the lapped box must be the same as the space specified in the \l@section.\@tocline. (#1 can be omitted since it won't be present.) note: this will not work if \subsections and lower levels are included in the toc. – barbara beeton Jun 27 '15 at 20:13
• @barbarabeeton Your suggestion works quite well for the section level. Unfortunately I have lots of subsections, and their numbers are now aligned to the indentation of the names of the section. The effect is not what I expected but nevertheless quite nice. It makes the distinction between the two levels quite clearer. – brunoh Jun 27 '15 at 20:59