write and read from a LaTeX temporary file? [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this questionI am writing a document that has a set of "task lists" scattered throughout. I want to have a single list of all of the tasks at the end. It seems to me that I should be able to do this with the same sort of intermediate files 开发者_C百科that are used for the table of contents, list of tables, and list of figures. But I just can't figure out any way to do it --- I've tried adding \addtocontents
with a different extension, but that doesn't seem to work. Does anybody have an idea of how to do this?
You can do:
\newwrite\tempfile
in your preamble to declare a new file writer.
Then, to open a file when you want to, you can assign the writer to a file and open it:
\immediate\openout\tempfile=lists.txt
To write to the file:
\immediate\write\tempfile{this is interesting}
Finally, close the file with:
\immediate\closeout\tempfile
To read a file, it could be as simple as \input
, or you can use \newread
, \openin
, \read
and \closein
combination.
Is this what you want to do?
Edit: This "works for me":
\documentclass{minimal}
\newwrite\tempfile
\begin{document}
\immediate\openout\tempfile=lists.tex
\immediate\write\tempfile{this is interesting}
\immediate\write\tempfile{}
\immediate\write\tempfile{this too}
\immediate\closeout\tempfile
\input{lists}
\end{document}
I haven't had much luck with the \addtocontents
mechanism. I have much better luck writing arbitrary LaTeX code to the .aux file. The examples are really too big to post in an SO answer, but you can find one in the noweb in the way "subpage labels" are handled, and you can find something similar in my technical report Teach Technical Writing in Two Hours Per Week, which accumulates some lists of principles and practices. The noweb source is public (and in Debian), and if anyone wants to the the other, send me an email.
For stuff like this, I encourage you strongly to dig under the LaTeX layer and study The TeXbook by Donald Knuth. That's the place where the mechanisms available are really explained.
If your list of tasks is really going to come at the end, you can avoid all the nonsense and just allocate a global tokens register which you accumulated throughought the document. You'll find some helpful ideas and examples in Appendix D of The TeXbook (Dirty tricks).
you would want the todonotes
package for LaTeX:
\usepackage{todonotes}
\begin{document}
\todo{This will be a note typeset in the margin}
\todo[inline]{This will be an in-line todo}
\missingfigure{This will give me a box indicating a pic should go here}
\listoftodos % will give you all of your todos from the document.
http://www.tex.ac.uk/tex-archive/help/Catalogue/entries/todonotes.html
精彩评论