[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:

\documentclass{book}
\let\cleardoublepage\clearpage

\RequirePackage{titletoc}
\RequirePackage{tikz}
\RequirePackage[explicit]{titlesec}
\RequirePackage{DejaVuSansCondensed}
\RequirePackage[normalem]{ulem}
\RequirePackage[
  left=2.25in,
  right=0.75in,
  top=1.25in,
  bottom=1.25in,
  marginparwidth=1.75in,
  marginparsep=.25in,
  asymmetric]{geometry}

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

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

%%% Change font/color/layout of TOC
\makeatletter
\titlecontents{chapter}[-2em]
{\color{cyan}\bfseries\LARGE\addvspace{3mm}}
{\makebox[4.4em][r]{\@chapapp}\contentslabel{-0.25em}\hspace{1.9em}}
{}
{\hspace{3em}\contentspage}
\g@addto@macro{\appendix}{%
  \addtocontents{toc}{\protect\renewcommand*{\protect\@chapapp}{\protect\appendixname}}%
}
\makeatother

\titlecontents{section}[5.3em]
{\color{cyan}\large\itshape\addvspace{3mm}}
{\contentslabel{0em}\hspace{3.2em}}
{}
{\hspace{3em}\contentspage}
[\addvspace{0mm}]

%%% Chapter Header ----------------------------------------------------------
\makeatletter
\titleformat{\chapter}
  {\normalfont\sffamily\Huge\scshape}
  {}{0pt}
  {\thispagestyle{empty}    % Remove page number on new chapters
  %\setcounter{definition}{0}
  \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=cyan] (0,-1) rectangle
          (25cm,3cm);
        \draw[fill=cyan] (0,-24) rectangle
          (25cm,-25cm);
        \ifttl@label% <---------------------- Added condition on \ifttl@label
          \node[anchor=west,xshift=.21\paperwidth,yshift=-.01\paperheight,rectangle]
              {\color{white}\LARGE \MakeUppercase{\@chapapp} \Huge\thechapter};
        \fi% <------------------------------- end condition on \ifttl@label
       \end{tikzpicture}
      };
   \end{tikzpicture}\endgraf
  \vskip-.7cm
  \color{cyan}\Huge\raggedright\leftskip-1cm
   \noindent\MakeUppercase{#1}\endgraf
  }
\makeatother

\titlespacing*{\chapter}{0pt}{10pt}{0pt}{}

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

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

  \appendix
  \chapter{ON COMPUTABLE NUMBERS, WITH AN APPLICATION TO THE ENTSCHEIDUNGSPROBLEM}
\end{document}

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
\makeatletter
\g@addto@macro{\appendix}{%
  \addtocontents{toc}{\protect\renewcommand*{\protect\@chapapp}{\protect\appendixname}}}

%%% Change font/color/layout of TOC

% START OF MODIFIED CODE

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

\titlecontents{chapter}[3cm] % <-- seems to set some specific left margin
{\color{cyan}\bfseries\LARGE\addvspace{3mm}}
{\makebox[0cm][r]{\@chapapp\hspace{.5em}\thecontentslabel\hspace{.75cm}}}
{} %     ^^^ 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
{\color{cyan}\large\itshape\addvspace{3mm}}
{\makebox[0cm][r]{\thecontentslabel\hspace{.75cm}}} % box pushed to the left
{}
{\hfill\makebox[-3cm]{\thecontentspage}}  % 3cm = twice 1.5cm
[\addvspace{0mm}]

% END OF MODIFICATIONS

\makeatother

and also, to add hyphenation points:

  \appendix
  \chapter{ON COMPUTABLE NUMBERS, WITH AN APPLICATION TO THE ENT\-SCHEI\-DUNGS\-PROBLEM}

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.

toc

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

\contentsmargin{3cm}

\titlecontents{chapter}[3cm]
{\color{cyan}\bfseries\LARGE\addvspace{3mm}}
{\makebox[0cm][r]{\@chapapp\hspace{.5em}\thecontentslabel\hspace{.75cm}}%
 }
{}
{\hfill\makebox[-6cm]{\thecontentspage}}

\titlecontents{section}[3cm]
{\color{cyan}\large\itshape\addvspace{3mm}}
{\makebox[0cm][r]{\thecontentslabel\hspace{.75cm}}}
{}
{\hfill\makebox[-6cm]{\thecontentspage}}
[\addvspace{0mm}]

%%%%% additional hyphenation point
\chapter{ON COMPUTABLE NUMBERS, WITH AN APPLICATION TO THE ENT\-SCHEI\-DUNGS\-PRO\-BLEM}

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.

toc

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}
\usepackage{etoc}

% these margins will used additionally to the page margins
% set-up by package geometry
\newlength{\tocleftmargin}
\setlength{\tocleftmargin}{3.5cm}
\newlength{\tocrightmargin}
\setlength{\tocrightmargin}{1cm}

\makeatletter % for using \@chapapp of class book

\etocsetstyle{chapter}
{\parindent0pt
 \leftskip\tocleftmargin
 \rightskip\the\tocrightmargin plus 1fil
 \parfillskip0pt
 \color{cyan}}
{\addvspace{3mm}\leavevmode\bfseries\LARGE\upshape}
{\llap{\@chapapp\hspace{.5em}\etocnumber\hspace{.75cm}}\etocname
 \hfill\makebox[-\tocrightmargin][l]{\makebox[0pt]{\etocpage}}\par}
{}

\etocsetstyle{section}
{}
{\addvspace{3mm}\leavevmode\mdseries\large\itshape}
{\llap{\etocnumber\hspace{.75cm}}\etocname
 \hfill\makebox[-\tocrightmargin][l]{\makebox[0pt]{\etocpage}}\par}
{}

\makeatother

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).

Remarks:

  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

Related Question