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?

Jesse
  • 29,686
Pawel
  • 515
  • 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 '14 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 '14 at 17:47
  • So what should the output be in the ToC as the current output is as expected? – Werner Sep 25 '14 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 '14 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: http://i.imgur.com/shV6iBl.jpg . – Pawel Sep 25 '14 at 18:08
  • @Paul: So \append actually also includes some other content into your document, or how do \append insert the "appendices, produced by external software"? – Werner Sep 25 '14 at 18:14
  • @U.Martinez-Corral lets say I want to attach 2 pages when the document is already printed. \append should reserve these 2 pages. So if I use \append on page 10, I want further content to be placed on page 13, but avoid the blank page inbetween 10 and 13. – Pawel Sep 25 '14 at 18:15
  • That's quite a different issue! Is there any reason for doing so and not including those pages into the latex document (even inserting full pdf pages)? – umarcor Sep 25 '14 at 18:20
  • Those pages are big format cad drawings, that have to be plotted separately. I mean it is not life-and-death issue, it would just be nice to make it work that way. – Pawel Sep 25 '14 at 18:28
  • In that case I think you could take a different approach and put those lines in a more direct way. I mean explicitly having a different \addcontentsline which will take the number of the page as a parameter. If not, in order to display different page numbers, I'm afraid you'll have to add extra blank pages. You may add the information you provided in the comments to the main question, to help other people provide the correct answer. – umarcor Sep 25 '14 at 18:58
  • The tocloft package provides a command to do so \cftaddtitleline{toc}{chapter}{<text>}{<page>}, as you can see in the answer to this or this question. – umarcor Sep 25 '14 at 19:05

2 Answers2

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}
umarcor
  • 1,454
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 :)

Pawel
  • 515