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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In typical LaTeX documents, the document content is typed inside the document environment, which is usually the last thing in the file:


    Content goes here.

However, a custom class of mine takes care of everything:


% This macro is provided by the class. It does everything needed to output the document
% and is always the last macro to be carried out.
% It uses \begin{document}…\end{document} under the hood.

Would it be possible to run that macro automatically at the end of the file that issued the \documentclass{myniceclass}, so that I didn't have to type it in all the documents that follow the class?

The general question is thus: How to run code automatically at the end of a specific file or at the end of input?

LuaTeX specific tricks (callbacks, maybe?) are allowed.

share|improve this question
See here, though that's for a LaTeX rather than LuaTeX solution. – Arun Debray Jan 20 at 21:20
Then that's no a “class”. Cleaner would be just \input{mycustomdocument} rather than \documentclass{mycustomclass}. – Manuel Jan 20 at 21:21
@Manuel The class is used to handle correspondence. Information about the correspondence (who maintain the correspondence, the locations from and to which letters are sent, which letters have been sent,…) is stored in a Lua script. The document instructs the class about the location of this script and, then, the class takes care of everything. – Kalrish Jan 20 at 21:37
isn't easy \documentclass{mycustomclass}\input{fileofcontents}\outputdocument? – touhami Jan 20 at 21:48
this answer would be helpful tex.stackexchange.com/a/13289/71471 – touhami Jan 20 at 22:44
up vote 8 down vote accepted

Your question isn't that clear but there is an \everyeof hook (added by etex)

so if your class goes



Then \foo will define itself to do nothing (in case more than one file is input) but the first time will do


and could obviously do more...

Another variant:





note that is inputting packages in the implied preamble.

zmble.cls :


\begin{document} hello \end{document}%


This produces an output of

enter image description here

share|improve this answer
Thanks a lot! Would it be possible to run \foo at the end of the last file (instead of at the end of the first file)? – Kalrish Jan 20 at 22:10
You can of course arrange that the main file is the first eof by using \AtBeginDocument{\usepackage{whatever} instead of \usepackage{whatever} then essentially all processing is deferred until this kicks in, or if you want to ignore any packages that are input early you could make the macro do nothing if \@currnamestack is not empty which means you are in a nested package context or ... – David Carlisle Jan 20 at 22:21
Would you mind posting an example with \@currnamestack? I have tried \everyeof{\ifx\@currnamestack\@empty\foo\fi} but it keeps giving me an error that I don't understand. – Kalrish Jan 20 at 22:37
@Kalrish example added – David Carlisle Jan 20 at 22:48

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.