4

I would like to output a single line of text at the end of all the documents generated using a particular coustom package file. I assumed \AtEndDocument would do the trick as I have used it before to do things like closing environments that were automatically opened at the start of the document, etc. Si I tried this:

\AtEndDocument{%
    \textit{my note here}
}

But this seems to output after a final \clearpage or similar has been run on the document and the line appears on a page of it's own. This is now what I want.

To my surprise trying to search for this, several questions around this site and the net seem to be asking for just the opposite: how to issue commands after the clear-page. It seems the behaviour I'm seing is what other people want and vise versa.

How can I add a snippet to the end of a document that ends on the same page with the rest of the content?

Caleb
  • 1,988

2 Answers2

5

The problem has nothing to do with \AtEndDocument itself, the problem is a package that has messed with that hook.

In my case I was using the package lastpage. Moving the \usepackage{lastpage} declaration to the end of my package file after I had declared what I wanted to have in that hook caused my snippet to appear on the proper page.

Caleb
  • 1,988
  • Thanks, that's my issue too. The same happens with the newer (?) pageslts as well. Since I can't (in good conscience) move the package load to the end of my preamble (I have modular documents), I opted for a manual \AtEndDocument{\label{LastPage}} -- now it's fine. (There may be corner cases in which the label does not end up on the last page -- I guess I'll deal with that when I encounter it.) – Raphael Nov 18 '14 at 12:00
0

At https://ctan.org/pkg/lastpage the manual for the lastpage package is available.

The version at the time of this post is v2.0a, its manual reads:

Subsection 3.1 \AtEndDocument

\AtEndDocument is not used by the lastpagemodern.sty version of the lastpage package, requiring LaTeX-format 2022-11-01 or newer. Instead \AddToHook{enddocument/afterlastpage} is used and the problem does not arise.

lastpageclassic.sty uses \AtEndDocument and lastpage209.sty redefines \enddocument. The last two cases are problematic: The output of a LaTeX2e run is not independent of the order in which the packages are loaded. It is often the case that the same formats for which one must put tables and figure at the end, are the ones in which endnotes are also required. If one wants to use \AtEndDocument here as well (as done for \pageref{LastPage}), then it is easy to get to three separate uses of \AtEndDocument (assuming one uses this for the endnotes as well). Clearly it is not safe for any package writer or user to assume that no material will follow what they put into \AtEndDocument. Therefore a message, which begins with AED, is included in every usage of \AtEndDocument. lastpage uses \AtEndDocument{...\clearpage...}, thus \usepackage{lastpage}...\AtEndDocument{something} will place something after the \clearpage. To place it earlier, use \AtEndDocument{something}...\usepackage{lastpage}. If the something is not known before \usepackage{lastpage}, you can use for example

...
\def\beforeLastpageClearpage{\relax}
\AtEndDocument{\beforeLastpageClearpage}
\usepackage{lastpage}
\begin{document}
...
\def\beforeLastpageClearpage{\textit{something}}%
...
\end{document}

(might need a protected and/or expanded \def). When \clearpage leads to some output, \clearpage\textit{something} instead of \textit{something} might be wanted.

tl;dr

Either use LaTeX-format 2022-11-01 or newer (\usepackage{lastpage} automatically loads the modern style which does not even use \AtEndDocument but \AddToHook{enddocument/afterlastpage})

or pay attention where/when to \AtEndDocument your stuff.

Stephen
  • 14,890