\AddToHook{begindocument/end}{⟨code⟩}\begin{document} 不等同于 \begin{document}⟨code⟩?

\AddToHook{begindocument/end}{⟨code⟩}\begin{document} 不等同于 \begin{document}⟨code⟩?

(这是这个问题关于与类相关的段落缩进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}

也是可能的。

相关内容