1

This is the code:

\documentclass{article}
\NewDocumentCommand\foo{v}{\texttt{#1}}
\AtBeginDocument{\catcode`\|\active\def|{\foo|}}
\begin{document}
Hello, \foo{world}, |world|!
\end{document}

It doesn't compile:

! Missing control sequence inserted.
<inserted text>
                \inaccessible
l.12 \begin{document}

How to fix this? I want to be able to call either \foo{world} or |world| and make them arrive to the same \foo command.

4
  • The catcode context of \def| is wrong. I've also shown how to make the catcode context correct here.
    – cabohah
    Nov 15, 2022 at 10:10
  • 1
    your question does not seem clear and the code is unrelated to the question as \foo{world} and |world| both read the argument verbatim. The error is from \AtBeginDocument{\catcode`\|\active\def|{\foo|}} as you need to make | active outside the macro, as always. Nov 15, 2022 at 10:10
  • Maybe you just searching for package shortvrb.
    – cabohah
    Nov 15, 2022 at 10:14
  • 1
    I might have said this before, but if you haven't you can read TeXbook/TeX by Topic to understand exactly how the TeX parser/catcode changes work. Would take a while though.
    – user202729
    Nov 15, 2022 at 13:13

1 Answer 1

1

With your setup you have already tokenized | as an 'other' character. You need to ensure that \def| happens when | is active. There are a few ways, for example

\documentclass{article}
\NewDocumentCommand\foo{v}{\texttt{#1}}
\catcode`\|\active
\AtBeginDocument{\catcode`\|\active\def|{\foo|}}
\catcode`\| 12 %
\begin{document}
Hello, \foo{world}, |world|!
\end{document}

You must log in to answer this question.

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