这是代码:
\documentclass{article}
\usepackage{tikz}
\NewDocumentEnvironment{foo}{b}{hello}{}
\AddToHook{env/foo/before}{\obeylines\obeyspaces}
\begin{document}
\begin{tikzpicture}
\node (v) {hi};
\end{tikzpicture}
\begin{foo}
first
second
\end{foo}
\begin{tikzpicture}
\node (v) {hi};
\end{tikzpicture}
\end{document}
我收到了这个奇怪的消息:
! Package tikz Error: A node must have a (possibly empty) label text.
See the tikz package documentation for explanation.
Type H <return> for immediate help.
...
l.14 \node
(v) {hi};
怎么了?
答案1
正如所注意到的来自@David Carlisle 的评论,您的 catcode 混乱将持续存在\end{foo}
...texdoc lthooks
说
因此你可能想要使用begin
钩子:
\documentclass{article}
\usepackage{tikz}
\NewDocumentEnvironment{foo}{b}{hello}{}
\AddToHook{env/foo/begin}{\obeylines\obeyspaces}
\begin{document}
\begin{tikzpicture}
\node (v) {hi};
\end{tikzpicture}
\begin{foo}
first
second
\end{foo}
\begin{tikzpicture}
\node (v) {hi};
\end{tikzpicture}
\end{document}