[Tex/LaTex] How to add custom lines with specified page number to the table of contents

appendicestable of contents

I want to add custom lines for my appendices, produced in external software, to the table of contents. Furthermore it should be possible to specify the number of pages of any particular appendix. How can I achieve this?

As MWE I will use my best try.

\documentclass{report}
\newcommand{\append}[2]{%
  \stepcounter{chapter}%
  \newpage\thispagestyle{empty}\phantom{-}%
  \addcontentsline{toc}{chapter}{\protect\numberline{\Alph{chapter}}{#1}}%
  \newpage\addtocounter{page}{-1}\addtocounter{page}{#2}%
}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
Bla bla
\chapter{Chapter 2}
Bla bla bla
\appendix
\chapter{Appendix 1}
Blah
\append{Extra 1}{2}
\append{Extra 2}{1}
\end{document}

Generated TOC:

MWE

Unfortunately to work it needs to insert a blank page for every single appendix.

How can I get rid of those extra blank pages, while keeping other traits of the \input command?

Best Answer

Since you don't want any blank page at all to be included, because you are going to plot some big format cad drawings separately, I looked at how \cftaddtitleline{toc}{chapter}{<text>}{<page>} is defined in the documentation of the tocloft package, page 49:

\newcommand{\cftaddtitleline}[4]{
 \addtocontents{#1}{%
  \protect\contentsline{#2}{#3}{#4}
 }
}

Thus, you just have to use \addtocontents instead of \addcontentsline. In order to get it displayed the same as the other chapter entries, you should define the title as \protect\numberline{\Alph{chapter}}#1}, which will produce this entry in your *.toc file:

\contentsline {chapter}{\numberline {2}Chapter 2}{3} %automatically done with \chapter{}
\contentsline {chapter}{\numberline {A}Appendix 1}{11} %added

However, you cannot use \thepage and increment it in the following line, because all the sections will get the same number:

  \addtocontents{toc}{\protect\contentsline{chapter}{\protect\numberline{\Alph{chapter}}#1}{\thecnt}}
  \addtocounter{page}{#2}

I added a counter which is set with \thepage before calling \append, and it is modified not to affect the number of the previous chapter (Appendix 1).

\documentclass{report}

\newcounter{cnt}

\newcommand{\append}[2]{%
  \stepcounter{chapter}
  \addtocontents{toc}{\contentsline{chapter}{\protect\numberline{\Alph{chapter}}#1}{\thecnt}}
  \addtocounter{cnt}{#2}
}

\begin{document}
\tableofcontents
\chapter{Chapter 1}
Bla bla
\chapter{Chapter 2}
Bla bla bla
\appendix
\chapter{Appendix 1}
Blah

\setcounter{cnt}{\thepage}
\stepcounter{cnt}
\append{Extra 1}{7}
\append{Extra 2}{8}

% In case you want to add other 'normal' appendices
%\clearpage
%\setcounter{page}{\thecnt}
%\chapter{Appendix \thechapter}

\end{document}
Related Question