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:
  1. \documentclass{article}
  2.  
  3. \begin{document}
  4.  
  5. % This writes the file myfile.tmp.
  6. % It will contain three lines
  7.  
  8. \newwrite\outputstream
  9. \immediate\openout\outputstream=myfile.tmp
  10. \immediate\write\outputstream{foo 1}
  11. \immediate\write\outputstream{foo 2}
  12. \immediate\write\outputstream{\string\textbf{foo 3}}
  13. \immediate\closeout\outputstream
  14.  
  15. % This reads myfile.tmp and writes its contents
  16.  
  17. \newread\inputstream
  18. \immediate\openin\inputstream=myfile.tmp
  19. \immediate\read\inputstream to \auxcommand
  20.  
  21. This is the first line of the file: \auxcommand
  22.  
  23. \immediate\read\inputstream to \auxcommand
  24.  
  25. This is the second line of the file: \auxcommand
  26.  
  27. \immediate\read\inputstream to \auxcommand
  28.  
  29. This is the third line of the file: \auxcommand
  30.  
  31. \immediate\closein\inputstream
  32.  
  33. \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:
  1. \protected@write\@auxout{}{%
  2. % What's to be written here.
  3. }

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

Hope this helps!
-John


Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests