0

This is a macro for producing vector length symbols. The mandatory argument m receives vector notations, which the optional argument o receives whatever should be added as subscripts if they are needed. However, the subscript notation does not seem to work, as the MWE below prints subscripts as if they were normal characters in the equation. Is the underscore character recognised as its own character and not an operator in xparse commands?

\documentclass[10pt]{article}
\usepackage{amsmath,xparse}
\newcommand{\abs}[1]{\ensuremath{\lvert #1 \rvert}}
\ExplSyntaxOn
\NewDocumentCommand \len { m o } {
    \IfNoValueTF{#2} {
        \ensuremath{\abs{\vec{#1}}}
    } {
        \ensuremath{\abs{\vec{#1}_{#2}}}
    }
}
\ExplSyntaxOff

\begin{document}
\[ \len{f}[\text{s.max}]=\mu_s\len{F}[N] \]
\end{document}

I'd rather not use the t_ argument. I'm not going to use the optional argument for anything other than the subscript anyways, and adding _ every time I write a subscript is just way too many unnecessary _s.

Paul Kim
  • 1,078
  • The subscripts work properly for me. Btw, you could also use \NewDocumentCommand \len { m O{} }{\ensuremath{\abs{\vec{#1}_{#2}}}}. Actually, I would define \NewDocumentCommand\len{ m O{} }{\ensuremath{\lvert\vec{#1}_{#2}rvert}} so that you don't have multiple \ensuremath's coming from \abs and \len. –  Jul 02 '19 at 04:59
  • 2
    @daleif but _ has no special meaning in this example - expl3 syntax has not been activated. – Ulrike Fischer Jul 02 '19 at 06:14
  • FWIW, the MWE typesets everything properly too on my computer... – frougon Jul 02 '19 at 06:30
  • Thank you everyone for the feedback. My goal is to achieve what everyone seems to be achieving without changing anything. – Paul Kim Jul 02 '19 at 06:44
  • Did you run the example exactly as posted? For me, it works as I'd expect. I wonder if your real case looks different – Joseph Wright Jul 02 '19 at 06:47
  • If your example as posted doesn't work, show the log-file. – Ulrike Fischer Jul 02 '19 at 07:52
  • Ah, my apologies everyone. My MWE didn't contain the \ExplSyntaxOn...\ExplSyntaxOff around the NewDocumentCommand...{} part. This would activate the expl3 syntax that provides special meaning for the underscore. Using \sb{...} would be the correct solution here, as a deleted comment had pointed out before. – Paul Kim Jul 02 '19 at 13:24
  • As an afterthought, it seems as though the ^ symbol is working fine as a superscript operator. Would that mean that said symbol can be used as a superscript even in expl3 syntax? – Paul Kim Jul 02 '19 at 13:26
  • Yes, ^ will keep it's normal meaning also when expl3 syntax is enabled. Btw, if you have found a solution that fixes your problem, you can also add an answer yourself and accept it. Otherwise I'd vote to close this question, as it seems to have been solved in the comments. – siracusa Jul 04 '19 at 01:18

1 Answers1

0

As a comment above states, expl3 syntax applies special meaning to the underscore symbol _, therefore the usage of macro \sb{...} is required.

Paul Kim
  • 1,078
  • As Ulrike mentions in her comment (I deleted mine as it was wrong), your answer is not correct, as least not for this mwe. _ has its usual meaning as long as you are not activating expl3 syntax. You did with your edit, but in this mwe it is not necessary at all. – daleif Jul 20 '19 at 17:27
  • I've heard that the \ExplSyntaxOn...\ExplSyntaxOff prevents the functioning of end-of-line ^^Ms that can mess with the spacing. Since it was either that or adding %s to every line (which is way too much work for a lazy person like me) I used expl3 syntax instead. – Paul Kim Jul 21 '19 at 00:41
  • I think that is a very high price for such a little effect, especially since this is a math command where spaces are ignored anyways. Note that those ensuremath are irrelevant – daleif Jul 21 '19 at 02:26