6

I am very surprised that simply adding an optional argument to the definition of my command changes the spacing in formulas in combination with \mathcolor. Why does this happen? How can this be fixed?

MWE:

\documentclass{article}
\usepackage{xcolor}

%MINIMAL EXAMPLE: \newcommand{\optionalSub}[2][a]{_{#2}}

\newcommand{\noOptionalSub}[1]{_{#1}}

%other example: \newcommand{\coloredSubscript}[2][red]{_{\mathcolor{#1}{#2}}}

\begin{document}

%MINIMAL EXAMPLE: $a^b\optionalSub{c}$ %correct spacing $\mathcolor{blue}{a^b}\optionalSub{c}$ % very wrong spacing $\mathcolor{blue}{a^b}\noOptionalSub{c}$ %correct spacing

%other example: $a^b\coloredSubscript{c}$ %correct spacing $\mathcolor{blue}{a^b}\coloredSubscript{c}$ %very wrong spacing

\end{document}

enter image description here

To summarize: \mathcolor alone does not influence the spacing (which I appreciate a lot) and adding optional arguments usually also does not change the spacing (as I appreciate a lot). But somehow using both of them directly next to each other changes the spacing? Why?

Jakob
  • 993
  • 3
    mathcolor has to look ahead to see a following _ or ^ which has to be there via expansion, the lookahead and test for optional argument stops that so \mathcolor does not see the _ – David Carlisle Nov 16 '23 at 00:27
  • 1
    Maybe something like https://tex.stackexchange.com/a/50784/250119 . But have you considered using like \mathcolor{blue}{a}^{\mathcolor{blue}{b}}\coloredSubscript? (tl;dr: What you want isn't supported by \mathcolor, which requires an explicit ^ or _ after it to work. Use something else.) – user202729 Nov 16 '23 at 03:34
  • 2
    @user202729 it doesn't have to be explicit, just reachable by expansion – David Carlisle Nov 16 '23 at 09:32

2 Answers2

3

You can use \NewExpandableDocumentCommand that doesn't suffer from the problem of the check for the optional argument in the legacy \newcommand.

\documentclass{article}
\usepackage{xcolor}

%MINIMAL EXAMPLE: \NewExpandableDocumentCommand{\optionalSub}{O{a}m}{_{#2}}

\NewExpandableDocumentCommand{\noOptionalSub}{m}{_{#1}}

%other example: \NewExpandableDocumentCommand{\coloredSubscript}{O{red}m}{_{\mathcolor{#1}{#2}}}

\begin{document}

%MINIMAL EXAMPLE: $a^b\optionalSub{c}$ %correct spacing $\mathcolor{blue}{a^b}\optionalSub{c}$ % very wrong spacing $\mathcolor{blue}{a^b}\noOptionalSub{c}$ %correct spacing

%other example: $a^b\coloredSubscript{c}$ %correct spacing $\mathcolor{blue}{a^b}\coloredSubscript{c}$ %very wrong spacing

\end{document}

enter image description here

egreg
  • 1,121,712
3

The similar problem is in OpTeX, but not with regards to the next parameter given by an un-expandable macro. Simply

$ {\Blue a^b}_c $ 

gives c after a^b (i.e. with space, wrong position) because the {...} is interpreted not only as a begin/end group but also as a new atom in math list. We have to replace { and } by \begingroup, \endgroup. These TeX primitives only open/close group but they don't construct math atoms. The solution is:

\def\mathcolor#1{\begingroup#1\endgroup}

$\mathcolor{\Blue a^b}_c$

Note that we don't need to do nothing more (no checking of following _ like in LaTeX), because OpTeX sets colors using LuaTeX attributes.

wipet
  • 74,238