General\addcontentsline \input

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
fpires
Posts: 15
Joined: Wed Dec 23, 2015 7:10 pm

\addcontentsline \input

Post by fpires »

Hi, everyone!

I have tried to use the \input command inside the \addcontentsline one, but it does not seem to work. Hereunder is the MWE:

Code: Select all

\documentclass{report}
\begin{document}
		\tableofcontents	
		\addcontentsline{toc}{section}{\input{Test}}
\end{document}
The file "Test" has just one charachter, which is the number 2. If I write any text inside the last curly braces of \addcontentsline, it works, and if I use \input{Test} outside the \addcontentsline command, it also works normally, printing 2. What am I doing wrong? Thanks in advance!
Last edited by Stefan Kottwitz on Wed Dec 23, 2015 8:32 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

\addcontentsline \input

Post by Johannes_B »

You are dealing with a fragile thing here, you have to \protect it.
Though i am not sure what you are trying to achieve. Maybe we can find a better solution?
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
fpires
Posts: 15
Joined: Wed Dec 23, 2015 7:10 pm

\addcontentsline \input

Post by fpires »

Hello, Johannes,

first of all thank you for your answer. I apologize for the late reply, but I was out of work for some days.

Well, what I would like to achieve in my code is: the amount of pages of each appendix must be literally written in the table of contents. For example, if appendix A has 5 pages, the table of contents should look like that:

......
Appendix A - 5 pages ....... page 89
......

The question is that I would like to do it automatically, i. e., not counting manually the amount of pages of each appendix and writting it in the table of contents. In order to achieve it, I made a file - called Test in my example - that should be fed with the total number of pages of a particular appendix in this way:

Code: Select all

\section*{Appendix A --- Title of Appendix A}

\setcounter{paginaini}{\value{page}}
\addtocounter{paginaini}{-1}

Text of Appendix A. 

\setcounter{paginas}{\value{page}}
\addtocounter{paginas}{-\value{paginaini}}

\newwrite\temporario
\immediate\openout\temporario=Test
\immediate\write\temporario{\thepaginas}
\immediate\closeout\temporario

\addcontentsline{toc}{section}{\input{Test} pages}



So I should run the code twice to get the correct amount of pages written in the table of contents. Would you have a suggestion for my case?

Thank you very much!
fpires
Posts: 15
Joined: Wed Dec 23, 2015 7:10 pm

\addcontentsline \input

Post by fpires »

Thank you, Johannes, your suggestion about \protect worked perfectly!
Post Reply