0

Thanks to this answer, I wrote this code:

\documentclass{article}
\NewDocumentCommand\foo{v}{\texttt{#1}}
\catcode`\|\active
\AtBeginDocument{\catcode`\|\active\def|{\foo|}}
\catcode`\| 12 %
\begin{document}
$a_|b|$
\end{document}

I'm getting:

! Missing { inserted.
<to be read again>
                   \tex_let:D
l.15 $a_|
         b|$.

However, $a |b|$ works just fine. Also $a_{|b|}$ also works. Is it possible to fix this and make it possible to use $a_|b|$ (without the curled brackets around |b|)?

5
  • why support that syntax? subscripts should always be braced. Commented Nov 15, 2022 at 22:21
  • @DavidCarlisle I can do $a_\text{hello}$, but can't do $a_\foo{hello}$
    – yegor256
    Commented Nov 15, 2022 at 22:24
  • It is really unfortunate, undocumented and accidental that \text does not error in that context Commented Nov 15, 2022 at 22:26
  • If your aim is to use a_|b| to get the subscript in \mathtt, you're using the wrong approach. Do you really need verbatim subscripts? With special characters such as #{}?
    – egreg
    Commented Nov 15, 2022 at 22:37
  • yes as egreg said I have been wondering why verbatim, none of the examples in your recent questions have needed that Commented Nov 15, 2022 at 22:45

1 Answer 1

2

The fact that some commands, as an accidental sequence of undocumented expansions, do not give errors on horrible input constructions such as x_\text{foo} or x^\frac12 does not mean that such things need copying elsewhere. The documented latex syntax always braces subscripts.

That said, assuming you want to ignore this advice...

\documentclass{article}
\NewDocumentCommand\foox{v}{\texttt{#1}\egroup}
\catcode`\|\active
\def\foo{\bgroup\foox}
\AtBeginDocument{\catcode`\|\active\def|{\bgroup\foox|}}
\catcode`\| 12 %
\begin{document}
$a_|b|$
\end{document}

You must log in to answer this question.

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