\NewDocumentEnvironment を使用すると tikz がエラーを出すのはなぜですか?

\NewDocumentEnvironment を使用すると tikz がエラーを出すのはなぜですか?

コードは次のとおりです:

\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}

ここに画像の説明を入力してください

関連情報