这是我的文件a.tex
:
\begin{tikzpicture}
\node[] (v) {hi};
\end{tikzpicture}
\endinput
这是main.tex
:
\documentclass{article}
\usepackage{tikz}
\NewDocumentEnvironment{foo}{b}{
% here I process the incoming #1
% here I must revert the effect of \obeylines and \obeyspaces
\input{a.tex}
}{}
\AddToHook{env/foo/begin}{\obeylines\obeyspaces}
\begin{document}
\begin{foo}
first
second
\end{foo}
\end{document}
当我编译时main.tex
,出现以下错误:
! 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.3 \node[]
(v0) {hi};
我怀疑我必须以某种方式恢复环境的\obeylines
影响。怎么做?\obeyspaces
foo
答案1
正常的 catcode 是 10 和 5。
\documentclass{article}
\usepackage{tikz}
\NewDocumentEnvironment{foo}{b}{
#1
% here I must revert the effect of \obeylines and \obeyspaces
\catcode`\ =10 %
\catcode`\^^M=5 %
\input{a.tex}%
}{}
\AddToHook{env/foo/before}{\bgroup\obeylines\obeyspaces}
\AddToHook{env/foo/after}{\egroup}
\begin{document}
\begin{tikzpicture}
\node (v) {hi};
\end{tikzpicture}
\begin{foo}
first
second
\end{foo}
aaa
bbb
\end{document}