Unclean .aux file causes "file ended while scanning use of \@newl@bel" error. Why is it not purged?

With the help of When is the aux file read and written? and Barbara Beetons kind assistance I was able to determine what goes wrong and how to prevent it.

This is the problem:

Normally I then don't work with the TeX engine on the console, but simply correct my error and restart.

This is what I should have done on the console:

  • Type ?+Return to get an idea of what can be done.
  • Simply hit Return repeatedly until Tex is finished.
  • Type X+Return to finish the run immediately.
  • Type E+Return to have the cursor jump to the point where the error occurred.

What happens is, that TeX buffers the \write commands to the .aux file in two stages. First stage, it waits until a page is shipped out to get the page numbers right. Second stage, it waits until it sees fit to issue a write() system call, to reduce the number of (potentially slow) system calls. This second buffer seemingly does not care, if the chunks it writes to file are self-contained legal TeX since it will complete the file before the TeX run is finished anyway.

In the environment of TeXShop however it is tempting to let TeX hang unfinished after an error. Thus the second output puffer is not flushed and the .aux file possibly left unfinished and in an illegal state.

My personal conclusion from this: Always let TeX finish.


The assumption that TeX will have a chance to finish it's run after it stopped for an error was very reasonable before windowed environments, where it became easy and normal to leave processes hanging in the background.

Things that could be done on software side to facilitate this user behavior:

  • Change Tex's second buffer behavior to issue a write() system call in case of an error or to break only into TeX-legal chunks.
  • Adjust the environment (TexShop) to either clean the .aux file after a process abortion or provide default input to always correctly finish the process.

Things that the user can do, other than always letting TeX finish:

  • Use \include{...}. That way the .aux files of the sections before are closed an finished by the time the error occurs. Chances are the .aux file of your current section isn't large enough yet and no TeX-illegal parts were written.
  • Hack something involving \immediate\closeout\@mainaux followed by an \openout\@mainaux and a copy of the contents so they don't get overwritten. This will effectively flush the output buffer. Does not seem worth the effort though.

I Just experienced this same problem and couldn't find any offending code in my tex file. It turns out that the offending text was actually in a citation I had just added to BibText, which I was importing using the biblatex package. There must have been some sort of strange character in the abstract that was included when I exported the citation from PubMed. I removed the entire abstract (since it wasn't going to be displayed in the references anyway), deleted the temp files, and rebuilt my PDF without error.


I find it much faster to kill compilation and simply delete the last line of the corrupted .aux file where the truncated buffer write results in incomplete/illegal TeX, particularly when working with large documents and/or debugging persistent/recurring .aux corruption. This way you can retain the valid portion of the .aux file and don't have to wait for compilation to finish.

In most editors it only takes a few keystrokes to switch focus to the .aux file, skip to the end, and delete the last line, so this can be done quite quickly.

If you prefer to force compilation to finish so all buffered \write's are flushed and the .aux isn't corrupted, you can instruct the compiler to proceed through errors without pausing for user input with the -interaction=nonstopmode option, that way you don't have to keystroke past each of them.