7

I want to color some of the symbols in math mode, but not their subscripts. However, when I do this, the subscripts are broken and escapes from the text.

I have prepared a minimal example to demonstrate my problem:

\documentclass{article}
\usepackage{xcolor}
\begin{document}

$T_i$

${\color{red}T}_i$

$\textcolor[rgb]{1,0,0}{T}_{i}$

\end{document}

The result is:

image

Note that, T and i are separated when T is colored. Is there a way to color properly without adding extra space on the subscript?

8

Instead of changing colors or closing a group immediately before a subscript, change the color after entering the subscript.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
$T_i$

${\color{red}T_{\color{black}i}}$
\end{document}

enter image description here

5

You can remember the current color and use xparse for grabbing the possible subscript and superscript, which are typeset in the current color.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}

\NewDocumentCommand{\colornucleus}{omme{_^}}{%
  \begingroup\colorlet{currcolor}{.}%
  \IfValueTF{#1}
   {\textcolor[#1]{#2}}
   {\textcolor{#2}}
    {%
     #3% the nucleus
     \IfValueT{#4}{_{\textcolor{currcolor}{#4}}}% subscript
     \IfValueT{#5}{^{\textcolor{currcolor}{#5}}}% superscript
    }%
  \endgroup
}
\begin{document}

$T_i$ $T^j$ $T_i^j$

$\colornucleus{red}{T}_i$
$\colornucleus{red}{T}^j$
$\colornucleus{red}{T}_i^j$

$\colornucleus[rgb]{1,0,0}{T}_{i}$
$\colornucleus[rgb]{0,1,0}{T}^j$
$\colornucleus[rgb]{0,0,1}{T}_{i}^j$

\textcolor{blue!30!red}{Text $\colornucleus{black}{T}_i$ text}

\end{document}

enter image description here

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.