在我的回答中这个问题,发生了一些我不理解的有关钩子系统的事情。如果添加钩子的代码直接位于主 tex 文件中(\tcolorboxenvironment
在此示例中),它会按我预期的方式工作:
\documentclass{article}
\usepackage[skins]{tcolorbox}
\newenvironment{myitemize}
{\begin{itemize}}
{\end{itemize}}
\tcolorboxenvironment{myitemize}{blanker,
before skip=6pt,
after skip=6pt,
borderline west={3mm}{0pt}{red}}
\begin{document}
\begin{myitemize}
\item text
\item more text
\end{myitemize}
\RemoveFromHook{env/myitemize/before}
\RemoveFromHook{env/myitemize/after}
\begin{myitemize}
\item text
\item more text
\end{myitemize}
\end{document}
生成一个带有红色边框的列表,一个不带有红色边框的列表。请注意 的定义\tcolorboxenvironment
:
\newcommand{\tcolorboxenvironment}[2]{%
\AddToHook{env/#1/before}{%
\begin{tcolorbox}[savedelimiter={#1},#2,wrap@environment,%
code={\def\tcb@end@tcolorboxenvironment{\end{tcolorbox}}}]%
}%
\AddToHook{env/#1/after}{%
\tcb@end@tcolorboxenvironment%
}%
}
但是如果我将此代码移到mytest.sty
:
\ProvidesPackage{mytest}
\RequirePackage[skins]{tcolorbox}
\newenvironment{myitemize}
{\begin{itemize}}
{\end{itemize}}
\tcolorboxenvironment{myitemize}{blanker,
before skip=6pt,
after skip=6pt,
borderline west={3mm}{0pt}{red}}
然后尝试
\documentclass{article}
\usepackage{mytest}
\begin{document}
\begin{myitemize}
\item text
\item more text
\end{myitemize}
\RemoveFromHook{env/myitemize/before}
\RemoveFromHook{env/myitemize/after}
\begin{myitemize}
\item text
\item more text
\end{myitemize}
\end{document}
我得到两个列表都有红色边框。如果代码在包中或直接在序言中,会有什么区别?
答案1
您需要包裹标签,因为默认标签在mytest
包裹内部,但top-level
在外部。
\documentclass{article}
\usepackage{mytest}
\begin{document}
\begin{myitemize}
\item text
\item more text
\end{myitemize}
\RemoveFromHook{env/myitemize/before}[mytest]
\RemoveFromHook{env/myitemize/after}[mytest]
\begin{myitemize}
\item text
\item more text
\end{myitemize}
\end{document}