I'm trying to redefine a \tableofcontents-like command.
I proceed this way : the displayed elements are written to a file, and the file is input when I want the display.
If I know the toc will be called at the beginning (resp. the end) of the document, it's fine : all I have to do is to add the \openout (resp \closeout) commands to the toc command.
However, I'd prefer not to work under that assumption (mostly because I won't be the only one using that code, and I want to avoid obscure side effects).
So I want the \openout and \closeout commands to be automatically issued at \begin{document} and \end{document}. The problem is that in that case, I can't \input the auxiliary file in the middle of the document. I've been trying to store the content of the file, before the \begin{document}, in a macro, but I am facing various problems which I guess are related to expansion.
After browsing some related questions, I managed to write the following code :
\documentclass{article}
\newwrite\sommaire
\AtBeginDocument{%
\immediate\openout\sommaire=\jobname.som%
}
\AtEndDocument{%
\immediate\closeout\sommaire}
\newcommand{\sommaireline}[1]{%
\immediate\write\sommaire{#1}
}
\makeatletter
\IfFileExists{\jobname.som}%
{\edef\mysommaire{\@@input \jobname.som }}
{\edef\mysommaire{\message{No file \jobname.som}}}
\makeatother
\begin{document}
\sommaireline{aaaaa}
\mysommaire
\sommaireline{bbbbb}
\end{document}
But it still doesn't work, and now I don't understand why.
More precisely, the first compilation goes fine, and the .som file is written as expected, but the second compilation fails with :
ERROR: File ended while scanning definition of \mysommaire.
...
ERROR: Too many }'s
both on the line of the first \edef.
\inputa generic (text) file into a macro or token list: rather it must be read line by line with an input stream, see my answer. If someone has another method and proves me wrong and that one can use in one go the\inputrather than\readline by line I am very interested to hear about it! – Dec 07 '12 at 18:09\inputto fill a token list or a macro, but the file has to end with an\endinput. – Dec 07 '12 at 20:24\CatchFileDeffrom thecatchfilepackage. – egreg Dec 07 '12 at 20:28\endinput. This was made much too quickly, whereas what I really know for having practiced it is the method of using\readas explained in my initial answer. Sorry folks for the rather stupid things I then added and took away. – Dec 07 '12 at 20:46catchfile. I am so ignorant that I don't even see the reasons why this task is so complicated. Any pointer? – Dec 07 '12 at 21:02