There is a user friendly LaTeX package: newfile
. You can use it to read and write files easily. It provides normal file IO functions, and also verbatim file IO functions. It is more suitable for your example, than those low-level macros. The package document has some good examples.
A naive example (similar to table of contents):
\documentclass{article}
\usepackage{newfile}
\newoutputstream{comment}
\openoutputfile{\jobname.comment}{comment}
\begin{document}
\section{Test}
text one
\addtostream{comment}{\noexpand\item comment one at page \thepage}
text two
\addtostream{comment}{\noexpand\item comment two}
\section{Comments}
\closeoutputstream{comment}
\begin{enumerate}
\input{\jobname.comment}
\end{enumerate}
\end{document}
Macros will expand when write to file. It is very important to control the expansion. Some macros should not be expanded, like \item
here; while some macros should be expanded, like \thepage
and \thesection
here.
In TeX, there are \newwrite
, \openout
, \immediate
, \write
, \closeout
, \ifeof
etc. A good resource is chapter 30 of TeX by Topic.
The same example using TeX primitives:
\documentclass{article}
\newwrite\commentfile
\openout\commentfile=\jobname.comment
\begin{document}
\section{Test}
text one
\write\commentfile{\noexpand\item comment one at page \thepage}
text two
\write\commentfile{\noexpand\item comment two in section \thesection}
\section{Comments}
\closeout\commentfile
\begin{enumerate}
\input{\jobname.comment}
\end{enumerate}
\end{document}
In LaTeX kernel, which you can refer source2e
, you can learn from \@starttoc
, \@wirtefile
, \addcontentsline
(for contents) etc.
The same example (works like standard TOC):
\documentclass{article}
\makeatletter
\newcommand\addcommentitem[1]{%
\write\@auxout{\noexpand\@writefile{comment}{\noexpand\item #1}}}
\newcommand\printcomment{%
\section*{Comments}
\begin{enumerate}
\item[] Here are some comments:
\@starttoc{comment}
\end{enumerate}}
\makeatother
\begin{document}
\section{Test}
text one
\addcommentitem{comment one at page \thepage}
text two
\addcommentitem{comment two in section \thesection}
\printcomment
\end{document}
You might not care about these files, but pdflatex
does quite a bit. These files hold information collected during the first run(s) and are needed to build the final PDF with correct ToC, references, PDF bookmarks, etc.
Your can delete these files afterwards, e.g. manually or using a front-end tool like latexmk
(-c
option). However, future compilations of the PDF would then need again several compiler runs.
You can define an output directory for all files using the -output-directory
command line argument of pdflatex
. After compilation you can then move the PDF in the current directory.
With MiKTeX, you can specify a directory in which all the auxiliary files are put (but not the PDF output) by using the -aux-directory
command line argument. You can even combine -output-directory
and -aux-directory
.
Best Answer
You can write to the aux file with
or
or
Depending on requirements.
\immediate\write
writes to the specified file at that point, expanding the supplied tokens (like\edef
) so fragile commands will do the wrong thing.\write
does not write at that point it puts a write node into the current vertical or horizontal list and if that list is shipped out to make a page then the write happens. This is needed to get page numbers correct. (If the write is inside a box and that box is never used on the main page then nothing is written to the file.)\protected@write
is a LaTeX-defined macro that uses\write
but arranges that\protect
works as required in LaTeX to protect fragile commands. The extra argument unused above allows you to locally insert extra definitions to make more commands be safe or have special definition in the write, see for example the definition of\index
or\addtocontents
.It is safe to write to the aux file, however you have to be aware that the file will be read back at least at the begin and end of the document, so you need to write lines that are safe in that context.
If you want to write to your own file then you just need to do
in the preamble and then replace
\@auxout
by\myfile
when writing.Have a look at the way
\tableofcontents
or\listoftables
or\listoffigures
work inlatex.ltx
or documented in source2e. They basically all use