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.
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.
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:
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.
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}
.
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.
Simply add these lines to your preamble
\renewcommand{\cftsecafterpnum}{\hspace*{7.5em}}
\renewcommand{\cftsubsecafterpnum}{\hspace*{7.5em}}
and adjust 7.5em
to your needs.
MWE
\documentclass[12pt]{article}
\usepackage{setspace, tocloft}
%Modifies line spacing of the ToC
\setlength\cftparskip{-1.2pt}
\setlength\cftbeforesecskip{1.3pt}
\setlength\cftaftertoctitleskip{2pt}
\renewcommand{\cftsecafterpnum}{\hspace*{7.5em}}
\renewcommand{\cftsubsecafterpnum}{\hspace*{7.5em}}
%Makes dots after sections/subsections: Sections 1., 2.1., etc
\makeatletter
\renewcommand{\@seccntformat}[1]{\csname the#1\endcsname.\quad}
\makeatother
%Makes the dots (above) appear in ToC
\let \savenumberline \numberline
\def \numberline#1{\savenumberline{#1.}}
\begin{document}
\tableofcontents
\begin{section}{Section 1}
\begin{subsection}{Section 1.1}\end{subsection}
\begin{subsection}{Section 1.2}\end{subsection}
\end{section}
\begin{section}{Section 2}\end{section}
\end{document}
Output
A remark
If you want to add dots after sections and subsections numbers, you can use
\renewcommand{\cftsecaftersnum}{.}
\renewcommand{\cftsubsecaftersnum}{.}
instead of
\let \savenumberline \numberline
\def \numberline#1{\savenumberline{#1.}}
Best Answer
You could try this approach which will certainly need some more modifications in case the ToC is more than two pages long. And perhaps there are some drawbacks which I don't see at the moment.
It uses the »atbegshi« package from the »oberdiek« bundle which allows to add the "headline" quite flexible to the next page of the ToC. If necessary, this can also be applied to the LoF an LoT.