Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Just like http://programming.itags.org/tex/137196/ I have noticed that everything after \end{document} is ignored. What is the point of \end{document}? It seems that the compiler could just read until the end of the file instead of until \end{document}. So is \end{document} just for completeness in that it closes the document environment or is there an actual use for placing things after \end{document}?

share|improve this question
That is where I curse on TeX, it never reads it! –  mafp Jan 17 '13 at 20:19
add comment

6 Answers

up vote 56 down vote accepted

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Compilier ignores everything you put after \end{document}. It is up to you what you place there. Note also that same role plays \endinput in files included into main source file with the use of \input or \include declarations.

As to me, I put after \endinput big fragments of text somehow related to working material which could be difficult to comment out using % char.

share|improve this answer
There is a slight difference between \end{document} and \endinput: you must not put anything in the same line as \endinput as this will confuse TeX. Say for example \endinput \undefined on one line and TeX will complain. –  topskip Sep 15 '11 at 7:27
\endinput is also useful to indicate the end of a .sty file, so that comments can be placed there without the need for %. not as good as separate documentation, but a lot quicker. –  barbara beeton Sep 15 '11 at 12:26
add comment

I regularly use the space after the \end{document} as a scratch pad to play with text that doesn't have a proper home in the main document.

share|improve this answer
add comment

Some editors (emacs at least) can put meta info after \end{document}

I have this (generated by emacs) in my main document (mymain.tex) and all \included documents:


%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "mymain"
%%% End: 

If I ask emacs to compile an \included document, it automatically knows what to do.

share|improve this answer
And you can have other 'minor' modes there if you like (e.g., 'flyspell'). –  jon Feb 9 '12 at 3:57
So these will not be recognized by Emacs if placed in any other place than the end? –  N.N. Feb 9 '12 at 6:17
@Jon: Yes it would be nice to have flyspell. Actually I would like to have flyspell on by default, when in latex mode, but I do not know how!? –  Hans-Peter E. Kristiansen Feb 12 '12 at 4:13
@N.N.: It works everywhere - the \end{document} is after all only in the main document. - But it is still an example of something that makes sense after the \end{document}. –  Hans-Peter E. Kristiansen Feb 12 '12 at 4:17
@Hans-PeterE.Kristiansen -- I don't do it anymore myself, but you should be able to add the following line to your .emacs file: (add-hook 'LaTeX-mode-hook 'turn-on-flyspell) (maybe add a comment that says what you are trying to do for future reference). It probably makes sense to put this line near/following whereever you are loading reftex and plugging it into auctex. If you don't use auctex's LaTeX-mode but emac's latex-mode, switch accordingly. –  jon Feb 20 '12 at 21:52
show 1 more comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.