為什麼 \AtEndDocument 不會在文件的*結尾*執行?

為什麼 \AtEndDocument 不會在文件的*結尾*執行?

我一定做了一些極其愚蠢的事情,但我已經盡可能地把它歸結為沒有運氣了。我正在處理最近提交中出現的錯誤pagectrl @ca7dc3

我有以下最小示例:

\documentclass{minimal}

\AtEndDocument{\typeout{wat}}

\begin{document}
hello, world
\end{document}

與以下日誌

This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) (preloaded format=pdflatex 2014.12.7)  9 DEC 2014 22:03
entering extended mode
 restricted \write18 enabled.
 %&-line parsing enabled.
**\input success-6.tex
(./success-6.tex (/usr/local/texlive/2014/texmf-dist/tex/latex/base/minimal.cls
Document Class: minimal 2001/05/25 Standard LaTeX minimal class
)
No file success-6.aux.
\openout1 = `success-6.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
wat
[1{/usr/local/texlive/2014/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./success-6.aux) ) 
Here is how much of TeX's memory you used:
 17 strings out of 493109
 369 string characters out of 6135004
 52967 words of memory out of 5000000
 3574 multiletter control sequences out of 15000+600000
 3640 words of font info for 14 fonts, out of 8000000 for 9000
 1141 hyphenation exceptions out of 8191
 14i,4n,10p,120b,92s stack positions out of 5000i,500n,10000p,200000b,80000s
</usr/local/texlive/2014/texmf-dist/fonts/type1/public/amsfo
nts/cm/cmr10.pfb>
Output written on success-6.pdf (1 page, 11394 bytes).
PDF statistics:
 12 PDF objects out of 1000 (max. 8388607)
 7 compressed objects within 1 object stream
 0 named destinations out of 1000 (max. 500000)
 1 words of extra memory for PDF output out of 10000 (max. 10000000)

日誌檔案的這一部分讓我感到非常悲傷:

wat
[1{/usr/local/texlive/2014/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]

我將此解釋為wat在文件開始之前就已經輸入了 ( [)。我究竟做錯了什麼?

答案1

人們認為該\AtEndDocument鉤子主要用於在文件、地址或註釋等末尾列印的材料。

該包atveryend還提供了兩個鉤子,其名稱不言自明:

\documentclass{minimal}
\usepackage{atveryend}

\AtEndDocument{\typeout{atend}}
\AfterLastShipout{\typeout{afterlastshipout}}
\AtVeryEndDocument{\typeout{atveryend}}

\begin{document}
hello, world
\end{document}

這是終端上的輸出:

This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./veryend.tex
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2014/texmf-dist/tex/latex/base/minimal.cls
Document Class: minimal 2001/05/25 Standard LaTeX minimal class
) (/usr/local/texlive/2014/texmf-dist/tex/latex/oberdiek/atveryend.sty)
(./veryend.aux)
atend
[1{/usr/local/texlive/2014/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
afterlastshipout
(./veryend.aux)
atveryend
 )</usr/local/texlive/2014/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on veryend.pdf (1 page, 11397 bytes).
Transcript written on veryend.log.

請注意,atveryend輸入的是文件.aux已被讀入。

答案2

\AtEndDocument\@enddocumenthook巨集內的更新\enddocument。下面顯示了它的位置\enddocument(來自latex.ltx):

\def\enddocument{%
   \let\AtEndDocument\@firstofone
   \@enddocumenthook% <---- Hook for \AtEndDocument
   \@checkend{document}%
   \clearpage% <---- Starts the final page builder
   \begingroup
     \if@filesw
       \immediate\closeout\@mainaux
       \let\@setckpt\@gobbletwo
       \let\@newl@bel\@testdef
       \@tempswafalse
       \makeatletter \@@input\jobname.aux
     \fi
     \@dofilelist
     \ifdim \font@submax >\fontsubfuzz\relax
       \@font@warning{Size substitutions with differences\MessageBreak
                  up to \font@submax\space have occurred.\@gobbletwo}%
     \fi
     \@defaultsubs
     \@refundefined
     \if@filesw
       \ifx \@multiplelabels \relax
         \if@tempswa
           \@latex@warning@no@line{Label(s) may have changed.
               Rerun to get cross-references right}%
         \fi
       \else
         \@multiplelabels
       \fi
     \fi
   \endgroup
   \deadcycles\z@\@@end}

注意\@enddocumenthook執行的是 \clearpage,這會啟動頁面建立器。因此,wat輸出是頁面建置開始。讓它輸出\clearpage,您可以修補(\enddocument或遵循中的一些準則我可以掛在文件的末端嗎?\clearpage):

\documentclass{article}
\usepackage{etoolbox}
\patchcmd{\enddocument}% <cmd>
  {\clearpage}% <search>
  {\clearpage\typeout{wat}}% <replace>
  {}{}% <success><failure>
\begin{document}
hello, world
\end{document}

報告.log

[1{/usr/local/texlive/2014/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
wat

答案3

\clearpage您可以在不使用\patchcmdfrom套件的情況下將巨集放在最終文件中etoolbox。第一種方法可用於非常簡單的情況:

\AtEndDocument{\clearpage\typeout{wat}}

這是有效的,因為第二個\clearpage(來自 LaTeX 內部)是在主垂直清單僅包含可丟棄項目時處理的,即沒有建立空白頁面。但是,當另一個套件使用\AtEndDocument並且它們認為它們的巨集將在 之前處理時,此方法可能會失敗\clearpage。然後你可以使用更穩健的方法。定義在內部控制序列\afterclearpage之後移動其參數的巨集:\clearpage

\documentclass{article}
\def\afterclearpage#1#2\clearpage{#2\clearpage#1}
\AtEndDocument{\afterclearpage{\typeout{wat}}}

\begin{document}
hello, world
\end{document}

相關內容