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.
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:





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,


User avatar
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}
  3. \begin{document}
  5. % This writes the file myfile.tmp.
  6. % It will contain three lines
  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
  15. % This reads myfile.tmp and writes its contents
  17. \newread\inputstream
  18. \immediate\openin\inputstream=myfile.tmp
  19. \immediate\read\inputstream to \auxcommand
  21. This is the first line of the file: \auxcommand
  23. \immediate\read\inputstream to \auxcommand
  25. This is the second line of the file: \auxcommand
  27. \immediate\read\inputstream to \auxcommand
  29. This is the third line of the file: \auxcommand
  31. \immediate\closein\inputstream
  33. \end{document}

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

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

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


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!

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests