[Tex/LaTex] To fix alignment in text added to Table of Contents

horizontal alignmenttable of contentstocbibindtocloft

My school requires tables of contents formatted like this — please note the little headings, in red:
enter image description here

I have it almost ready, except for a pesky misalignment in the second page of the table of contents. "Chapter" gets thrown to the right.

Chapter thrown to right

As I prepare a minimal working example, it seems that a previous addtocontents line triggers the problem.

\documentclass[12pt,letterpaper,oneside]{book}
\usepackage{tocloft}

\usepackage[toc,page]{appendix}
\usepackage{titlesec}
\usepackage[nottoc]{tocbibind}
\usepackage{afterpage}
\usepackage{blindtext}
\usepackage{indentfirst}
\usepackage{fancyhdr}

\begin{document}

\tableofcontents

\addtocontents{toc}{\hfill Page \par}
%  Is this line above the culprit? if commented, Chapter below aligns!

\chapter{Blah blah1}
\section{Basics}
\subsection{Margins}
\blindtext
\section{Blah in section}
\Blindtext
\subsection{More blah for a subsection}
\blindtext 
\begin{figure}
\caption{picture}
\end{figure}
\chapter{Some Blah}
\section{One section}
\blindtext
\chapter{All}
\blindtext
\chapter{the}
\blindtext
\section{Good enough}
\blindtext
\subsection{Better}
\blindtext

\addtocontents{toc}{\protect\afterpage{Chapter\hfill Page \par\medskip}}
% This is how I add those 'headings' to the toc.
% Not well aligned!

\chapter{way}
\section{Please}
\section{Go}
\subsection{Ahead}
\chapter{to}
\chapter{the}
\chapter{very}
\chapter{end}
\chapter{keep}
\section{keep uno}
\section{kep dos}
\chapter{going}
\chapter{more}

\end{document}

This minimal code will produce this:

enter image description here

Is there a way to fix that addtocontents line? or a more sensible way to do it?

Please advise. Thank you in advance!


If it helps, these are some lines from the .toc file:

\contentsline {subsection}{OM as city block distance}{28}
\afterpage {Chapter\hfill Page \par \vspace \medskipamount }
\contentsline {subsection}{Euclideanity of OM and Euclidean transformations}{29}
\contentsline {subsubsection}{Raw and transformed OM as Euclidean}{29}
\contentsline {subsubsection}{Low dimensional Euclidean via MDS}{32}
\contentsline {section}{Distribution of sequences and inter-sequence distances}{34}
\contentsline {subsection}{Distribution of true inter-triple and OM distances}{34}

And this is how it looks like:

enter image description here


Aw, I just added a

\addtocontents{toc}{Chapter\hfill Page \par}
\section{Distribution of sequences and inter-sequence distances}

What a dirty thing.

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.

\documentclass[11pt]{report}
\usepackage[T1]{fontenc}
\usepackage[nottoc]{tocbibind}
\usepackage{atbegshi}
\usepackage{blindtext}

\AtBeginDocument{%
  \addtocontents{toc}{%
    \contentsline{chapter}{}{Page}%
    \protect\AtBeginShipoutNext{\contentsline{chapter}{\chaptername}{Page}\vspace{\baselineskip}}%
  }%
  \addtocontents{lof}{\contentsline{chapter}{\figurename}{Page}}%
  \addtocontents{lot}{\contentsline{chapter}{\tablename}{Page}}%
}

\begin{document}
  \tableofcontents
  \listoffigures
  \listoftables
  \addtocontents{toc}{\contentsline{chapter}{\chaptername}{}}

  \blinddocument
  \blinddocument
  \blinddocument
  \blinddocument
\end{document}

enter image description hereenter image description here enter image description hereenter image description here

Related Question