TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When compiling this code:

\documentclass{article}

\usepackage{testPackage}
\usepackage{xparse}

\NewDocumentCommand{\foo}{mm}{#1_{#2}}

\begin{document}
$\foo A i$, $\fooT A i$
\end{document}

where testPackage includes the definition:

\usepackage{xparse}
...
\ExplSyntaxOn
...
\NewDocumentCommand{\fooT}{mm}{#1_{#2}}
...
\ExplSyntaxOff

I get the output:

enter image description here

Why is the subscript not typesetted correctly in the second version, even though both commands do the same?

share|improve this question
    
??? \fooT and \foo look the same for me? And the output is the same – Christian Hupfer Jan 19 at 12:12
    
What is the category code is _ in testPackage? – Joseph Wright Jan 19 at 12:15
    
Oh, I'm sorry for being unclear. But somehow you read my mind. – Stefan Perko Jan 19 at 12:25
up vote 5 down vote accepted

I assume that the O.P. has \ExplSyntaxOn...\ExplSyntaxOff as a wrapper around the definition of \fooT. This will change the catcodes of course and _ is interpreted differently.

If none of the expl3 syntax (e.g. think of \cs_new:N etc.) is needed, don't use \ExplSyntax.... The \NewDocumentCommand is sufficient for this!

\documentclass{article}

%\usepackage{testPackage}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\fooT}{mm}{#1_{#2}}
\ExplSyntaxOff
\NewDocumentCommand{\fooother}{mm}{#1_{#2}}
\NewDocumentCommand{\foo}{mm}{#1_{#2}}

\begin{document}
$\foo{A}{i}$, $\fooT{A}{i}$, $\fooother{A}{i}$
\end{document}

enter image description here

share|improve this answer

The conventions holding between \ExplSyntaxOn and \ExplSyntaxOff are, essentially:

  1. spaces and end-of-lines are ignored (but not in the middle of a control sequence name, of cours)

  2. _ and : can (and should) be part of control sequence names, so they have category code 11 (letter)

  3. ~ is the same as an ordinary space token

A consequence of this is that _ cannot be used to introduce subscripts in math mode. The reason is the usual one: if you do

\ExplSyntaxOn
\NewDocumentCommand{\fooT}{mm}{#1_{#2}}
\ExplSyntaxOff

the replacement text of \fooT is tokenized when _ is a letter, so it will be such every time \fooT is called.

In order to solve this problem, the expl3 kernel provides

\c_math_subscript_token

so you should use it when you need _8 in the body of a definition, either for introducing a subscript or for checking with \peek_... tests.

Using \sb is also possible, which might be more attractive in the code above; so

\ExplSyntaxOn
\NewDocumentCommand{\fooA}{mm}{#1\c_math_subscript_token{#2}}
\ExplSyntaxOff

or

\ExplSyntaxOn
\NewDocumentCommand{\fooA}{mm}{#1\sb{#2}}
\ExplSyntaxOff

are equivalent. The former syntax should preferably be used in a pure expl3 context.

share|improve this answer
    
The former syntax should preferably be used in a pure expl3 context.” I don't think the use of that _token is this one so may be not even in “pure expl3 context” would be necessary. When a math package is released may be something like \math_subscript:n {#2} could be preferred. – Manuel Jan 19 at 15:43
    
@Manuel There's quite a big difference between _ (or \c_math_subscript_token) and a macro, because _ looks for a <filler>. – egreg Jan 19 at 16:08
    
I didn't mean it was the same. I meant that \c_math_subscript_token wasn't designed for this (IMO), i.e., to be used as a subscript; altough it's expl3 it doesn't make it fit for this. If there was a \math_subscript:n then that “should preferably be used in a pure expl3 context”. I was just nitpicking that I don't think using _token as a subscript in this case is preferred in expl3 at all. – Manuel Jan 19 at 17:51

If, for some reason, you need to use a subscript inside expl3 syntax, you can always use \sb

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\fooT}{mm}{#1\sb{#2}}
\ExplSyntaxOff
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.