[Tex/LaTex] Changing the color of a “-” symbol within the align environment

aligncolorieeetran

I need to highlight a "-" sign together with the term next to it in an equation which is inside the align environment.

In principle, I need something like this:

enter image description here

but with the minus sign highlighted in the same way as the "b" term.

The problem is that when I try to include the minus in the color-changing command, the spacing is screwed up:

enter image description here

Using IEEEtrantools instead of LaTeX's plain align doesn't help either. Anyone knows of any clean solution to this?

Here's the MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage[retainorgcmds]{IEEEtrantools}
\definecolor{ocre}{RGB}{243,102,25}
\begin{document}

Test:

\begin{align*}
a-b&=a-b\\
a+b&=a+b
\end{align*}

Test with color 1:

\begin{align*}
a{\color{ocre}-b}&=a-b\\
a+b&=a+b
\end{align*}

Test with color 2:

\begin{align*}
a-{\color{ocre}b}&=a-b\\
a+b&=a+b
\end{align*}

Test with IEEE:

\begin{IEEEeqnarray*}{rCl}
a{\color{ocre}-b}&=&a-b\\
a+b&=&a+b
\end{IEEEeqnarray*}
\end{document}

Best Answer

what is happening when -b is put inside braces is that the minus is being interpreted as a unary rather than a binary minus, since it has nothing (as detectable by tex) on its left side. to remedy that, it's sufficient to insert an empty group, {}, to its left:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage[retainorgcmds]{IEEEtrantools}
\definecolor{ocre}{RGB}{243,102,25}
\begin{document}

Test:
\begin{align*}
a-b&=a-b\\
a+b&=a+b
\end{align*}

Test with color 1:
\begin{align*}
a{\color{ocre}{}-b}&=a-b\\
a+b&=a+b
\end{align*}

Test with IEEE:
\begin{IEEEeqnarray*}{rCl}
a{\color{ocre}{}-b}&=&a-b\\
a+b&=&a+b
\end{IEEEeqnarray*}
\end{document}

output of example code

edit: another, even simpler, workaround, is to simply adjust the scope of the group:

{a\color{ocre}-b}&=&a-b

that works for this case, where the colored portion ends at the end of the cell (before the &). if non-colored material follows, that would need to be taken into account. thanks to @Manuel for the suggestion in a comment to his answer.