![\AddToHook{begindocument/end}{⟨code⟩}\begin{document} 不等同于 \begin{document}⟨code⟩?](https://linux22.com/image/471718/%5CAddToHook%7Bbegindocument%2Fend%7D%7B%E2%9F%A8code%E2%9F%A9%7D%5Cbegin%7Bdocument%7D%20%E4%B8%8D%E7%AD%89%E5%90%8C%E4%BA%8E%20%5Cbegin%7Bdocument%7D%E2%9F%A8code%E2%9F%A9%EF%BC%9F.png)
(这是这个问题关于与类相关的段落缩进standalone
。
以下 MCE 给出了预期结果(缩进段落):
\documentclass[varwidth]{standalone}
\newlength\keptparindent
\keptparindent=\parindent
\begin{document}
\parindent=\keptparindent
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
magna. Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et ne
tus et malesuada fames ac turpis egestas.
\end{document}
但是,如果:
\begin{document}
\parindent=\keptparindent
被(假设的)等效前导指令取代:
\AddToHook{begindocument/end}{%
\parindent=\keptparindent
}
\begin{document}
段落不再缩进:
\documentclass[varwidth]{standalone}
\newlength\keptparindent
\keptparindent=\parindent
\AddToHook{begindocument/end}{%
\parindent=\keptparindent
}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
magna. Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et ne
tus et malesuada fames ac turpis egestas.
\end{document}
为什么?是否可以仅使用序言指令来缩进段落?
编辑
这是我的文件列表
standalone.cls 2022/10/10 v1.3b Class to compile TeX sub-files standalone
shellesc.sty 2023/07/08 v1.0d unified shell escape interface for LaTeX
ifluatex.sty 2019/10/25 v1.5 ifluatex legacy package. Use iftex instead.
iftex.sty 2022/02/03 v1.0f TeX engine tests
xkeyval.sty 2022/06/16 v2.9 package option processing (HA)
xkeyval.tex 2014/12/03 v2.7a key=value parser (HA)
standalone.cfg 2022/10/10 v1.3b Default configuration file for 'standalone'
class
article.cls 2023/05/17 v1.4n Standard LaTeX document class
size10.clo 2023/05/17 v1.4n Standard LaTeX file (size option)
varwidth.sty 2009/03/30 ver 0.92; Variable-width minipages
l3backend-pdftex.def 2023-11-09 L3 backend support: PDF output (pdfTeX)
***********
答案1
不幸的是,standalone
没有使用钩子,而是附加\sa@cls@afterbegindocument
到\document
。这\sa@cls@afterbegindocument
用于执行\standalone
并因此激活varwidth
。结果中的代码\document
是:
\UseOneTimeHook {begindocument/end}
\ignorespaces
\sa@cls@afterbegindocument
因此begindocument/end
钩子被执行前与缺失缩进相关的代码。
作为一种解决方法,您可以使用:
\listfiles
\documentclass[varwidth]{standalone}
\newlength\keptparindent
\keptparindent=\parindent
\makeatletter
\AddToHook{begindocument/end}{%
\csname sa@cls@afterbegindocument\endcsname
\let\sa@cls@afterbegindocument\relax
\parindent=\keptparindent
}
\makeatother
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
magna. Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et ne
tus et malesuada fames ac turpis egestas.
\end{document}
要得到
笔记:
\listfiles
\documentclass[varwidth]{standalone}
\newlength\keptparindent
\keptparindent=\parindent
\AddToHook{begindocument/end}{%
\standalone
\NewCommandCopy\STANDALONE\standalone
\def\standalone{\RenewCommandCopy\standalone\STANDALONE}%
\parindent=\keptparindent
}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
magna. Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et ne
tus et malesuada fames ac turpis egestas.
\end{document}
也是可能的。