LaTeX forum ⇒ GeneralHow can I write to and read from an AUX file?

LaTeX specific issues not fitting into one of the other forums of this category.
nderby
Posts: 3
Joined: Thu Jan 31, 2008 2:57 am

How can I write to and read from an AUX file?

Postby nderby » Tue Mar 11, 2008 8:14 pm

Hello all,

How can I write to and read from an AUX file? I'm using LaTeX from NiKTeX 2.5, and I've tried this simple example to start, which gives me an error message:


\documentclass{article}

\begin{document}

\makeatletter
\immediate\write\@auxout{foo}

\end{document}


Can someone help me out?

Alternatively, is there some online tutorial somewhere about how to write to and read from an AUX file? I can't seem to find one.


Thanks so much,

Nate

User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Wed Mar 12, 2008 1:42 am

You may try to read the documented code of LaTeX, i.e. the file source2e.pdf, to figure out how it works. In TeX Live, this file is at texmf-dist/doc/latex/base. You may also read The TeXbook, by D. Knuth, or TeX by Topic, by Victor Eijkhout, that you can download here. I also provide you a simple example. It does not consider the aux file, but a a custom file. Anyway, it may be heplpful:
\documentclass{article}

\begin{document}

% This writes the file myfile.tmp. 
% It will contain three lines

\newwrite\outputstream
\immediate\openout\outputstream=myfile.tmp
\immediate\write\outputstream{foo 1}
\immediate\write\outputstream{foo 2}
\immediate\write\outputstream{\string\textbf{foo 3}}
\immediate\closeout\outputstream

% This reads myfile.tmp and writes its contents

\newread\inputstream
\immediate\openin\inputstream=myfile.tmp
\immediate\read\inputstream to \auxcommand

This is the first line of the file: \auxcommand

\immediate\read\inputstream to \auxcommand

This is the second line of the file: \auxcommand

\immediate\read\inputstream to \auxcommand

This is the third line of the file: \auxcommand

\immediate\closein\inputstream

\end{document}


You can change the names of \outputstream, \inputstream and \auxcommand by those of your choice.

jpmrst
Posts: 1
Joined: Tue Jan 12, 2010 7:05 pm

Postby jpmrst » Tue Jan 12, 2010 7:11 pm

Nate,

This works for me for simple things:
    \protected@write\@auxout{}{%
      % What's to be written here.
    }

Note that you'll want to use the \string macro to defer expansion until when the aux-file is loaded, e.g.
    \protected@write\@auxout{}{%
      \string\@mycommand{arg}{arg}
    }

Hope this helps!
-John


Return to “General”

Who is online

Users browsing this forum: No registered users and 14 guests