# Strange error generated after another error and related to aux files (Xe(La)TeX)

With short files, if I get an error, I correct it, retypeset, and that's it. If the document becomes long enough (I don't know how many lines are required at least to get it, but I think 1000 should do it), when I retypeset, no matter what, I always get an error like:

./app_An_2.tex:441: File ended while scanning use of \@writefile.
<inserted text>
\par
l. 441 \begin{document}


where app_An_2.tex is the name of my file. If I trash the aux files, the error vanishes. Why does that happen? Part of the answer lies in the fact that below a certain number of lines the .aux file is left empty throughout the typesetting till the Undefined control sequence is found, whereas above that number (which seems to be around 4 hundred and sixty-something) after typesetting you find the .aux file has something in it. Usually the last line is an incomplete \@writefile{toc}, even removing the \tableofcontents command from the file and not letting there BE ANY .toc file. So why does all that happen above 460-something lines and not below that, and why ON EARTH does it \@writefile{toc} when there is neither any \tableofcontents in the .tex file nor any .toc file in the folder of the .tex file?

-
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). Also, you can use backticks ` to mark your inline code –  karlkoeller Dec 7 '13 at 16:55
This often happens when a fragile command is used in a section title. –  egreg Dec 7 '13 at 16:55
And thanks for you tips @karlkoeller. –  MickG Dec 7 '13 at 21:20
Though the tip of the code button doesn't solve the problem of posting that example. –  MickG Dec 7 '13 at 21:26