0

This is my file a.tex:

\begin{tikzpicture}
\node[] (v) {hi};
\end{tikzpicture}
\endinput

This is 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}

When I compile main.tex, it fails with this:

! 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};

I suspect that I must revert the effect of \obeylines and \obeyspaces somehow inside foo environment. How?

5
  • You seem to be asking several questions that look like you have an 'X-Y' problem: you are asking lots of things that I think suggest your starting point is wrong. For example, it you are trying to read examples verbatim then 'do stuff', you should be looking at a solution for that from the start (e.g. using listings).
    – Joseph Wright
    Commented Oct 9, 2022 at 11:33
  • @JosephWright you are right, but this seems to be the last question for me (in this particular journey)
    – yegor256
    Commented Oct 9, 2022 at 13:09
  • 1
    this is identical to your previous question. you should delete this and I'll answer there when I am at a computer or someone else will Commented Oct 9, 2022 at 14:23
  • The previous question has already been answered, but this case is different (I believe)
    – yegor256
    Commented Oct 9, 2022 at 14:46
  • @yegor256 reading your past dozen or so questions makes we wonder if maybe the listing side text option from tcolorbox could be useful? Commented Oct 9, 2022 at 14:58

1 Answer 1

2

The normal catcodes are 10 and 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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .