math mode coloring

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


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