0

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?

11
  • Of course it inserts a blank page since you're inserting two \newpages with a \phantom{-} inbetween. To remove the blank page, just insert \newpage and then the ToC entry.
    – Werner
    Sep 25, 2014 at 17:33
  • @Werner I put those \newpage commands on purpose - otherwise if a few \appends are placed next to each other they all end with the same page number in ToC.
    – Pawel
    Sep 25, 2014 at 17:47
  • So what should the output be in the ToC as the current output is as expected?
    – Werner
    Sep 25, 2014 at 17:50
  • I can't really get to know what's the goal. With 'the number of pages' you mean telling how many pages that 'Extra' appendix has? Or just telling where it starts? On the other hand, are you going to include the content of those appendices in the document, or do you just want to have it placed in the ToC?
    – umarcor
    Sep 25, 2014 at 18:05
  • @Werner Output in terms of ToC it is perfectly what I need. I want to keep it, but simultaneously I do not want those blank pages. Removing \newpages results in: i.imgur.com/shV6iBl.jpg .
    – Pawel
    Sep 25, 2014 at 18:08

2 Answers 2

1

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}
1

After getting some sleep I was able to come up with an alternative, working as desired. It is somewhat based on U.Martinez-Corral answer.

Backstory: I wanted to create a command that will add an item to the Table of Contents and reserve specified amount of pages.

Here is the code.

\documentclass{report}
\usepackage{afterpage}
\newcounter{count}
\newcounter{add}\setcounter{add}{1}
\newcommand{\append}[2]{%
    \stepcounter{chapter}%
    \setcounter{count}{\thepage}\addtocounter{count}{\theadd}%
    \addtocontents{toc}{\protect\contentsline{chapter}{\protect\numberline{\thechapter}#1}{\thecount}}%
    \addtocounter{add}{#2}%
    \afterpage{\addtocounter{page}{-1}\addtocounter{page}{\theadd}\setcounter{add}{1}}%
    \ignorespaces%
}
\begin{document}
\tableofcontents
\append{Extra 1}{3}
\append{Extra 2}{1}
\chapter{Chapter 1}
\chapter{Chapter 2}
\appendix
\chapter{Appendix A}
Some text
\append{Extra B}{2}
\append{Extra C}{1}
Text out of nowhere!
\append{Extra D}{3}
\chapter{Appendix F}
\append{Extra G}{1}
\append{Extra H}{1}
\end{document}

A few words to explain what is going on.

\setcounter{count}... line calculates the starting page of phantom chapter, taking into account number of pages already reserved.

\addtocontents... adds a custom line to ToC, the line is formatted exactly as any other chapter.

\addtocounter{add}{#2} increments total number of reserved pages. This number will be used either on the next page or in next \append.

\afterpage... is invoked when a new page begins. It adds the number of reserved pages to the page counter, and resets the number to 1.

\ignorespaces is added so the command can be used anywhere and not produce additional whitespace, see "Text out of nowhere!" in the example.

Thanks for your help :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .