无法添加到分段命令的“after”钩子

无法添加到分段命令的“after”钩子

似乎不能添加到分段命令的“after”钩子中。例如文档

\documentclass{article}
\AddToHook{cmd/section/after}{\bfseries}
\begin{document}
abc
\section{some title}
abc
\end{document}

日志读取失败

! Missing \endcsname inserted.
<to be read again> 
                   \scan_stop: 
l.5 \section
            {hhd}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.

! Argument of \hook_use:n has an extra }.
<inserted text> 
                \par 
l.5 \section
            {hhd}
I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

Runaway argument?
! Paragraph ended before \hook_use:n was complete.
<to be read again> 
                   \par 
l.5 \section
            {hhd}
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

如果用、等\section替换,错误是相同的。当然,这个例子没有显示用法;我的实际用例是向用from制作的自定义目录中添加一行\chapter\subsectionSection <num>\newlistoftocloft示例中展示了一种简单的解决方法,即在命令前进行挂钩并将计数器加一。

\documentclass{article}
\usepackage{tocloft,lipsum}

\newlistof[section]{answer}{ans}{List of Answers}

\newcommand{\answer}[1]{%
    \refstepcounter{answer}
    \par\noindent\textbf{Answer \theanswer. #1}
    \addcontentsline{ans}{answer}{\protect\numberline{\theanswer}#1}\par
    }

%%% this doesn't work
% \AddToHook{cmd/section/after}{
%   \addcontentsline{ans}{section}{Section \thesection}
%   }

%%% workaround
\newcounter{sectionplusone}
\setcounter{sectionplusone}{1}
\AddToHook{cmd/section/before}{
    \addcontentsline{ans}{section}{Section \thesectionplusone}
    \stepcounter{sectionplusone}
    }

\begin{document}
\section{some section}
\answer{Title} \lipsum[1][1-2]
\section{another section}
\answer{another title} \lipsum[1][3-4]
\listofanswer
\end{document}

在此处输入图片描述

这应该是错误吗?如果是这样,建议在不重新定义分段命令的情况下“在”分段命令之后进行修补的方法是什么?

答案1

这在章节中有记录2. 限制和操作细节,具体例子cmd/section/after如下:

在此处输入图片描述

这归结为每个命令的实现方式,正如文档所说,要么你知道,要么你发现(困难的方式:)。\section特别是,关于,有一个逐步解释其工作原理这里,这也许能帮助你理解为什么会失败。

钩子代码无法检测到这种情况,并且它会以难以预测的方式失败。它如何失败还取决于命令的定义方式。

如果您使用的是带有标准类的 LaTeX,并且没有用于格式化分段命令的包,则可以将钩子添加到\@xsect,它会在每个分段命令之后运行,但在这里您不再知道它是 还是\section\subsection其他。另一个选择是挂接到\sectionmark\subsectionmark等:它们更像是“用户级”,挂接到它们不会有问题:

\documentclass{article}
\usepackage{tocloft,lipsum}

\newlistof[section]{answer}{ans}{List of Answers}

\newcommand{\answer}[1]{%
    \refstepcounter{answer}
    \par\noindent\textbf{Answer \theanswer. #1}
    \addcontentsline{ans}{answer}{\protect\numberline{\theanswer}#1}\par
    }

\AddToHook{cmd/sectionmark/after}
  {\addcontentsline{ans}{section}{Section \thesection}}

\begin{document}
\section{some section}
\answer{Title} \lipsum[1][1-2]
\section{another section}
\answer{another title} \lipsum[1][3-4]
\listofanswer
\end{document}

相关内容