[Tex/LaTex] Text alignment issue in custom Table of Contents

horizontal alignmenttable of contents

As you can see from the image below, I have an issue with the text alignment of extra long chapter names. How can I fix it so that the text will not go further left than the start of the chapter name? That is, I don't want any text to be underneath the "Appendix A" part.

I added that red line just to show how I'd like it aligned, if possible.

enter image description here

Here's my MWE:



\renewcommand*\familydefault{\sfdefault} % Set default font

%%% Table of Contents ----------------------------------------------------------
\setcounter{tocdepth}{1} % Show only Chapters and Sections

%%% Change font/color/layout of TOC


%%% Chapter Header ----------------------------------------------------------
  {\thispagestyle{empty}    % Remove page number on new chapters
  \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=cyan] (0,-1) rectangle
        \draw[fill=cyan] (0,-24) rectangle
        \ifttl@label% <---------------------- Added condition on \ifttl@label
              {\color{white}\LARGE \MakeUppercase{\@chapapp} \Huge\thechapter};
        \fi% <------------------------------- end condition on \ifttl@label


\titleformat{\section}{\color{cyan}\itshape\LARGE}{\llap{\thesection} #1}{1em}{}[\titleline{\color{cyan}\titlerule[1pt]}]
\titleformat{\subsection}{\color{cyan}\normalfont\large}{\; #1}{1em}{}

  \chapter{Turing Machines}
    \section{The Turing Machine}
    \section{Variations of the Turing Machine}
      %\subsection{Non-deterministic Turing Machines}
    \section{The Church-Turing Thesis}
    \section{The Universal Turing Machine}


Best Answer

Let me for brevity indicate here only the region of the code that I modified:

%%% Table of Contents ----------------------------------------------------------
\setcounter{tocdepth}{1} % Show only Chapters and Sections

% this stuff moved here for clarity

%%% Change font/color/layout of TOC


\contentsmargin{1.5cm} % global right margin, perhaps also global left

\titlecontents{chapter}[3cm] % <-- seems to set some specific left margin
{} %     ^^^ pretendously zero width box puts its contents in the left margin
{\hfill\makebox[-3cm]{\thecontentspage}}  % 3cm = twice 1.5cm

\titlecontents{section}[3cm] % <-- again this left (additional?) margin
{\makebox[0cm][r]{\thecontentslabel\hspace{.75cm}}} % box pushed to the left
{\hfill\makebox[-3cm]{\thecontentspage}}  % 3cm = twice 1.5cm



and also, to add hyphenation points:


Explanations: not being familiar with titletoc I went to see its manual. I see there that there is a command \contentslabel{<length>} and another command without parameter \contentspage. It is a bit hard to understand from the documentation only what exactly they do, so I first experimented a bit with varying the lengths in the OP's code. Reading further the manual of titletoc I discover \thecontentslabel and \thecontentspage which do not do mysterious things so in the end I decided to use them in boxes designed on purpose.

It seems that the left margins are set as (mandatory?) arguments within brackets, to each of the sectioning level, whereas the right margin is set globally with the command \contentsmargin.

So I set \contentsmargin to a given value, here 1.5cm, and put the page numbers centered in boxes twice at big, but with a negative width so that the \hfill pushes them into the margin and the page numbers get exactly one underneath the other.

I tried to avoid using lengths given in terms of em as the font size is not the same for chapter and section, and precise alignment is more difficult then. So I opted for the metric system instead from the French revolution.

The basic idea of the layout is inspired by a figure in the \titletoc manual where one understands that the name is typeset in a rectangular block delimited by the left and right margins mentioned above. On the start of the first line I put a box pretendously of width zero, and aligned to its right, so as to put the Chaper x thing into the left margin at a very precise distance. idem for the section number. Then at the end of the last line of the rectangular block I push the page number as explained before into the right margin.


I didn't polish the final looks, and @BarbaraBeeton's comment about with is well-founded. Here is how to address it:




%%%%% additional hyphenation point

Note that I used the German hyphenation pro-blem (this is a problem indeed to do this or prob-lem...). Also, I do not know exactly how titletoc typesets the section or chapter name.


Not knowing enough titletoc I have given a try to etoc. Here is the relevant code in the preamble. If I knew better enumerate I would use it here in the etoc specifications for chapter and section. Here, the code is a bit more basic, one has to know a bit about the basic paragraph relevant parameters in TeX.

I also modified a bit the method to put the page numbers on a specific location in the right margin.

%%% Change font/color/layout of TOC (with package etoc.)
% \RequirePackage{titletoc}

% these margins will used additionally to the page margins
% set-up by package geometry

\makeatletter % for using \@chapapp of class book

 \rightskip\the\tocrightmargin plus 1fil



No need now for the \- in the long appendix name, as no hyphenation is needed! I tried to achieve the same with titletoc but did not succeed in finding the correct way (surely there is one).


  1. after updating my installation, I realized that the latest version of etoc on CTAN (version 1.07) re-sets the styles at begin document to be the default ones from the document class. I will make a bug report as this means one has to use the command \etocsetstyle after \begin{document}. In the mean-time a work-around is to put the code for chapter and section as argument to a AtBeginDocument command.

  2. etoc uses package xspace. It seems this causes \etocnumber\hspace{.75cm} to get this extra space. One can get rid of it either using \etocnumber\unskip\hspace{.75cm} or {\etocnumber}\hspace{.75cm}.

  3. In the section style, it would probably be better to use \textup{\etocnumber} and \textup{\etocpage}, as the italic shape for labels and page numbers is in contrast with what is done for chapter.

toc done with etoc