7

It is possible to use \textcolor in math-mode; however, it changes the spacing of subscripts.

\documentclass{article}
\usepackage[usenames]{xcolor}
\begin{document}
$Y_i$ vs $\textcolor{red}{Y}_i$ vs $\mathord{\textcolor{red}{Y}}_i$ vs $\color{red}Y\color{black}_i$ vs $\begingroup\color{red}Y\endgroup_i$
\end{document}

Is there any way of fixing the spacing? This suggestion (the last two examples) also didn't help.

An additional complication is that I intend to use this within a macro. So, I am aiming for something like:

\newcommand{\parameter}{\textcolor{red}{Y}}
We use both $\parameter$ and $\parameter_i$

tex --version outputs TeX 3.1415926 (TeX Live 2013).

  • Have a look at the \mathcolor as defined in Colored symbols. – Peter Grill Jun 11 '14 at 5:56
  • @PeterGrill Thanks, unfortunately though it didn't work. I did find, though, that \protect is needed when using the solutions below in a caption. That link, however, uses it within the macro itself, so it is good to know that that is safe to do. The \leavevmode isn't used in any of these solutions, so I wonder if it resolves an unseen problem (a quick search suggests spacing at beginning of paragraph, but that hasn't seemed to be an issue)? – Joel Bosveld Jun 11 '14 at 6:27
3

May be you need this:

\def\parameter{\color{red}Y\futurelet\next\parameterA}
\def\parameterA{\ifx\next_\expandafter\parameterB\else\color{black}\fi}
\def\parameterB_#1{_{\color{black}#1}\color{black}}

$Y_i, \parameter_i, \parameter$ etc.

Edit: The code above was my first attempt to solve this problem. But two thinks were not treated here. First: color stack (the outer color may be blue, for example) and second: exponent (you can write \parameter_i^2 for example). This is a reason why I suggest a second version of my macros:

\def\parameter{\bgroup\colorlet{outcolor}{.}\color{red}Y\futurelet\next\parameterA}
\def\parameterA{\ifx\next_\expandafter\parameterB\else\egroup\fi}
\def\parameterB_#1{_{{\color{outcolor}#1}}\futurelet\next\parameterC}
\def\parameterC{\ifx\next^\expandafter\parameterD\else\egroup\fi}
\def\parameterD^#1{^{\color{outcolor}#1}\egroup}

The explanation of the problem: The index is positioned without italic correction of base character but exponent with italic correction. The previous sentence is not true if the last object of the base isn't character, i.e it is \pdfliteral or \special in order to return to the outer color. Then the italic correction is applied before such \pdfliteral. The following index (if exists) cannot remove the space from this italic correction after that. The result is: we cannot close the color in the base. We need to return to the outer color in the index manually.

Second problem: we cannot close the group (or to place \pdfliteral) after the index because the exponent (if exists) is positioned after such closing group and it is too far from the base. This is a reason why the exponent is treated similar way in may macros and the \egroup is set after exponent. Watch to the last trick: the double braces {{..}} in the index. The color setting in the xcolor LaTeX package is implemented as TeX-groups dependend by \aftergroup primitive. This places some material after the group where \color command is used. We need to save this material inside the index, no after the index is closed.

  • Thank you for your testing. I've corrected it. I din't test it on the real LaTeX package color, so I dint't realize this trickiness. – wipet Jun 10 '14 at 12:26
  • Thank you. Do you mean that the index ought to be positioned with (not without) italic correction but the exponent is not? When I test with \mathrm{A}_i^i the exponent does not move any closer to the A, rather it juts out. Is this the intended behaviour? That only the index (subscript) has its position adjusted based on the shape of the character? – Joel Bosveld Jun 11 '14 at 2:53
  • Each character has its own italic correction which (rougly speaking) denotes the amount of the project of the glyph from its rectangular box. Italic Y has big italic correction but roman A has zero correction. In text mode, characters are set alongside without correction if \/ isn't used. Compare {\it Yx} (without correction) and {\it Y\/x} (with correction). In math mode, characters are set alongside with italic correction (roughly speaking, there is a special exception). Compare: $Yx$. If you try $Y,$ then there is correction followed by negative kerning between these two chars. – wipet Jun 11 '14 at 15:15
  • There is another problem not solved here: if the colored letter is followed by character constituting kerning pair, then kern isn't generated because there is color swither between the characters. Compare $Y,$ and $\parameter,$. I've improved my code in order to cover this feature too and I've released the definitive version of this code in my www page petr.olsak.net/opmac-tricks.html#colmath . – wipet Jun 12 '14 at 6:26
5

With \textcolor{red}{Y}_{i} you lose the font metric information about the “Y”, so TeX can't kern the subscript. Here's a macro that works in the easiest cases (no superscript and just at the main level (not in subscripts or superscripts). If you need the full set of features, it would be just more complicated.

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newcommand\parameter{%
  \textcolor{red}{Y}%
  \@ifnextchar_{\parameter@subscript}{}%
}
\newcommand\parameter@subscript[2]{%
  % #1 is _, #2 is the actual subscript
  \sbox0{$Y_{#2}$}\sbox2{$Y{}_{#2}$}%
  _{\kern\dimexpr\wd0-\wd2\relax#2}%
}
\makeatother

\begin{document}
$Y_i$

$\parameter_i$
\end{document}

enter image description here

An easier implementation that works also in subscripts.

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newcommand\parameter{%
  \begingroup
  \color{red}Y\@ifnextchar_{\do@parameter}{\endgroup}%
}
\newcommand{\do@parameter}[2]{%
  _{\color{black}#2}\endgroup
}
\makeatother
\begin{document}
$Y_i$

$\parameter_i$

$A_{\parameter_i}$
\end{document}

enter image description here

  • Thank you. What is the more natural way of achieving this? To me, creating extra boxes to calculate how much to nudge things over seems slightly more messing than playing with the groups/colors. Is one way preferred as the more TeX-y approach? – Joel Bosveld Jun 11 '14 at 3:24
  • @JoelBosveld Personally I'd avoid color; probably the second approach is better. – egreg Jun 11 '14 at 8:24
0

I combined @egreg and @wipet's answers as my final solution, which seems to work with all test cases I have tried. It has the added feature of allowing the text to be set through a parameter, so it operates analogously to \textcolor.

\documentclass{article}
\usepackage[usenames]{xcolor}

\makeatletter
\newcommand{\mathcolor}[2]{%
  \begingroup
  \colorlet{out}{.}\color{#1}#2\@ifnextchar_{\do@mathcolorsub}{\endgroup}%
}
\newcommand{\do@mathcolorsub}[2]{%
  _{{\color{out}#2}}\@ifnextchar^{\do@mathcolorsup}{\endgroup}%
}
\newcommand{\do@mathcolorsup}[2]{%
  ^{\color{out}#2}\endgroup
}
\makeatother

It makes it easy to define multiple parameters.

\newcommand{\Y}{\mathcolor{red}{Y}}
\newcommand{\A}{\mathcolor{green}{A}}
\newcommand{\Ar}{\mathcolor{blue}{\mathrm{A}}}
\newcommand{\Oc}{\mathcolor{red}{O}}

And the test cases:

\begin{document}
$Y_i+Y^i+Y_i^i+Y+Z_{Y_i}+Z^{Y_i}$\par
$\color{blue} \Y_i+\Y^i+\Y_i^i+\Y+Z_{\Y_i}+Z^{\Y_i}$\par
$A_i+A^i+A_i^i+A+Z_{A_i}+Z^{A_i}$\par
$\A_i+\A^i+\A_i^i+\A+Z_{\A_i}+Z^{\A_i}$\par
$\mathrm{A}_i+\mathrm{A}^i+\mathrm{A}_i^i+\mathrm{A}+Z_{\mathrm{A}_i}+Z^{\mathrm{A}_i}$\par
$\Ar_i+\Ar^i+\Ar_i^i+\Ar+Z_{\Ar_i}+Z^{\Ar_i}$\par
$A_i^{Y_k}+\A_i^{\Y_k}+O^i+\Oc^i$
\end{document}

Test cases

I presume this is worth posting, but am happy to delete if is considered superfluous.

  • Unfortunately this didn't work for me. Test case: \newcommand{\foo}{\mathcolor{red}{U_0}}, \newcommand{\bax}{\mathcolor{blue}{r_1}} and \newcommand{\qux}{\foo^{\bax}}. When I do $\qux$ the exponent r_1 is too far to the right, and not aligned with the subscript 0 in U_0. – hakoja Oct 9 '18 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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