4

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

\documentclass{article}

\begin{document}
    Content goes here.
\end{document}

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

\documentclass{mycustomclass}

% 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.
\outputdocument

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.

5
  • 1
    See here, though that's for a LaTeX rather than LuaTeX solution. Jan 20 '16 at 21:20
  • 2
    Then that's no a “class”. Cleaner would be just \input{mycustomdocument} rather than \documentclass{mycustomclass}.
    – Manuel
    Jan 20 '16 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 '16 at 21:37
  • isn't easy \documentclass{mycustomclass}\input{fileofcontents}\outputdocument?
    – touhami
    Jan 20 '16 at 21:48
  • this answer would be helpful tex.stackexchange.com/a/13289/71471
    – touhami
    Jan 20 '16 at 22:44
8

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

so if your class goes

\def\foo{\begin{document}\end{document}}


\everyeof{\global\expandafter\let\expandafter\foo\expandafter\relax\foo}

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

\begin{document}\end{document}

and could obviously do more...


Another variant:

Document:

\documentclass{zmble}

\usepackage{array}

\usepackage{graphicx}

note that is inputting packages in the implied preamble.

zmble.cls :

\LoadClass{article}

\def\foo{\global\let\foo\relax
\begin{document} hello \end{document}%
}


\everyeof{\ifx\@currnamestack\@empty\expandafter\foo\fi}

This produces an output of

enter image description here

4
  • 1
    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 '16 at 22:10
  • 1
    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 ... Jan 20 '16 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 '16 at 22:37
  • @Kalrish example added Jan 20 '16 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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