如何恢复 \obeylines 和 \obeyspaces 对 \input 内容的效果?

如何恢复 \obeylines 和 \obeyspaces 对 \input 内容的效果?

这是我的文件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影响。怎么做?\obeyspacesfoo

答案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}

相关内容