TransWikia.com

Spaces around Boolean argument to IfValueT gives -NoValue-

TeX - LaTeX Asked on July 26, 2020

This code compiles correctly as long as I don’t put spaces around arguments #3 and #4 in the definition of vec. I can’t find anything about this in the xparse documentation, and I have been under the impression that spaces in such places are the preferred style in LaTeX3 so I’d like to know why this happens. I have a feeling it may be trivial, but I’m not seeing it.

My MWE (contains code originally by @egreg):

documentclass{article}
usepackage{amsmath}
usepackage{xparse}
usepackage{esvect}

% A smarter vec command.
% Why doesn't it work when I put spaces around #3 or #4?
RenewDocumentCommand{vec}{ s m e{_^} }{%
  ensuremath{%
    IfBooleanTF {#1}
      {vv{#2}}
      {boldsymbol{#2}}
    _{IfValueT{#3}{#3}vphantom{smash[b]{|}}}
    ^{IfValueT{#4}{#4}vphantom{smash[t]{big|}}}
  }%
}%

begin{document}
[
  vec{E}_{text{ball}}
  qquad
  vec{E}_{_{text{ball}}}
]
[
  vec{p}_{text{ball}}
  qquad
  vec{p}_{_{text{ball}}}
  qquad
]
[
  vec{F}
  qquad
  vec{F}_{text{proton}}
  qquad
  vec{F}^{text{elec}}
  qquad
  vec{F}^{text{elec}}_{text{proton}}
  qquad
  vec{F}_{text{proton}}^{text{elec}}
]
[
  vec{F}
  qquad
  vec{F}_{21}
  qquad
  vec{F}_{21}^{text{contact}}
  qquad
  vec{F}_{text{table on ball}}^{text{contact}}
  qquad
  vec{F}^{text{contact}}_{text{table on ball}}
]
end{document}

Here is the result without the spaces in question:

Output without spaces around #3 and #4

And here is the result WITH the spaces in question:

Output with spaces around #3 and #4

One Answer

(continue from this comment)

An ugly workaround:

  • tokenize e{^_} before ExplSyntaxOn, in order to frozen the catcode of _, and
  • replace _ (required) and ^ (just for consistent) by their command form sb (sub-script) and sp (super-script), respectively.
documentclass{article}
usepackage{amsmath}
usepackage{xparse}
usepackage{esvect}

% A smarter vec command.
% Why doesn't it work when I put spaces around #3 or #4?

deftemp{ RenewDocumentCommand{vec}{ s m e{_^} } }

ExplSyntaxOn
temp{%
  ensuremath{%
    IfBooleanTF {#1}
      {vv{#2}}
      {boldsymbol{#2}}
    sb{IfValueT{ #3 }{#3}vphantom{smash[b]{|}}}
    sp{IfValueT{ #4 }{#4}vphantom{smash[t]{big|}}}
  }%
}%
ExplSyntaxOff

begin{document}
[
  vec{E}_{text{ball}}
  qquad
  vec{E}_{_{text{ball}}}
]
[
  vec{p}_{text{ball}}
  qquad
  vec{p}_{_{text{ball}}}
  qquad
]
[
  vec{F}
  qquad
  vec{F}_{text{proton}}
  qquad
  vec{F}^{text{elec}}
  qquad
  vec{F}^{text{elec}}_{text{proton}}
  qquad
  vec{F}_{text{proton}}^{text{elec}}
]
[
  vec{F}
  qquad
  vec{F}_{21}
  qquad
  vec{F}_{21}^{text{contact}}
  qquad
  vec{F}_{text{table on ball}}^{text{contact}}
  qquad
  vec{F}^{text{contact}}_{text{table on ball}}
]
end{document}

Correct answer by muzimuzhi Z on July 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP