Does anyone place anything after \end{document}?

I frequently have stuff beyond the \end{document}! It is invaluable for debugging. Sometimes I have some complicated bit of TeX code that's going badly wrong but I can't figure out exactly where the error is (for example, beamer tends to report errors at the end of the frame - since that's when it encounters them - but the actual error is buried deep within it). Then putting \end{document} at a judicious step helps me find the likely cause of the error by locating it in the source file.

Another use (should these be in different answers?) is when testing code. This is a particular use when I'm answering stuff here. If there's some code that I want to save (perhaps the questioner's original code) but don't want to process (maybe I'm working on an alternative) then it's quick and easy to shove it beyond the \end{document}. It's better than commenting it since it keeps the different pieces separate and my editor still syntax-highlights it.

Before I discovered version control, I used to use the patch after \end{document} as a place to save bits of an article that might still fit in somewhere but I wasn't sure where. Now that I use version control, I don't do that as I know I can always get it back from an older copy. To underline how daft this is, I don't think that I have ever actually resurrected code, but it gave me that safety net and so meant that I was happier to just remove vast swathes of an article that needed to be removed without fear of losing something valuable.


TeX reads file until it reads a primitive called \end, at which point it finalises typesetting. So any TeX file which simply 'runs out of instructions' means that TeX will present the * prompt and wait for more input. Thus you have to 'finish off' correctly.

In most cases, you do not want to call \end directly. In LaTeX, the primitive \end is renamed to \@@end, and the name \end is used for environments, so you can't easily use the primitive directly in any case. At the end of a LaTeX document, there is actually quite a lot to tidy up, for example finalising the .aux file. This is all wrapped up inside \end{document}, which as it's last act fires the \end primitive to finish the TeX run.

(Normally, there is filler to add to the last page as well, so even in plain TeX it's usual to include some additional instructions along with \end. In plain, this is done by the \bye macro.)


The \end{document} command may call necessary cleanup macros, which would be impossible to realize in LaTeX (because LaTeX is not compiled directly, but rather written in TeX and interpreted) if the file just ended. So, \end{document} has a very specific purpose, even though any text after it is ignored and thus nothing but a glorified comment.

Tags:

Compiling