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}
I, too, think that the interactive mode was important when the edit-compile-test cycle was very long and one couldn't afford to start over for each mistake.
However, today we are more accustomed to the Unix style of error reporting, where an utility would print the error and exit immediately. Luckily, there is a command line switch combination that does precisely that:
pdflatex -interaction=nonstopmode -halt-on-error
With this command line, only the first error is printed on the terminal, and control is returned to the shell. The only disadvantage is that you can't type H anymore to read the error help — but if you have at least some experience with LaTeX, you will not need the error help text very often.
Best Answer
Try out xii.tex. It's a plainTeX file, you can find it on CTAN.
And Enrico “egreg” Gregorio produced a similar code, even shorter. See in What is the most bizarre thing you have seen done with TeX, where it is called
xcix.tex
.