7

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}
5

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.

6

For this kind of things you need \begingroup .. \endgroup. If you use {..} LaTeX treats it as a \mathord{..}. So this should do

\begingroup\color{ocre}-b\endgroup

May be you prefer a macro; I don't know if this has any big disadvantages, though.

\newcommand\mathcolor[2]{\begingroup\color{#1}#2\endgroup}
..
a \mathcolor{ocre}{- b}
  • I'm just curious, for the pro people, what's the reason why \textcolor is {\color{..}..} rather than \begingroup\color{..}..\endgroup? – Manuel Jul 11 '15 at 12:52
  • 1
    i hadn't registered this difference between the \begingroup ... \endgroup and { ... } forms of input. (thanks for pointing it out.) many people find the verbosity of the commands disrupting in input like this, and many editors brace-match, but don't match the commands, making a difference in the complexity of debugging. so they prefer the braces for that reason. (i'm in that category.) there's a simple workaround for that, though, as i've shown in my answer. – barbara beeton Jul 11 '15 at 15:21
  • Another workaround is put everything into a group, and then change whenever you want: {a \color{ocre} - b}. That way you have exactly the same input, just the first { displaced enough to the left. – Manuel Jul 11 '15 at 16:29

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.