# How to color only the coefficients of a system of equations?

How to color only the coefficients of an aligned system of equation? I'm trying to do something like this:

And so far, I've tried using these packages and defining these colors:

\usepackage{amsmath,systeme}
\usepackage{xcolor}
\definecolor{V}{HTML}{00a2e8}
\definecolor{R}{HTML}{fbc200}
\definecolor{B}{HTML}{000000}


The problem I have is that apparently the systeme environment won't allow me to color the minus sign of the coefficients (I guess is using them to identify every term in the equations) nor the equal symbol. So this is what I've tried:

\begin{equation*}\mathcolor{R}{
\systeme[I_{1}I_{2}I_{3}I_{4}]{
6\color{B}{I_{1}}-3\color{B}{I_{2}} -1\color{B}{I_{3}}=\color{V}{5},
-3\color{B}{I_{1}} +7\color{B}{I_{2}} -2\color{B}{I_{4}}=\color{V}{-23},
-1\color{B}{I_{1}}+3\color{B}{I_{3}} -1\color{B}{I_{4}}=\color{V}{5},
-2\color{B}{I_{2}} -1\color{B}{I_{3}} +4\color{B}{I_{4}}=\color{V}{3}
}}
\end{equation*}


On the other hand, when I try this:

    $$\systeme*{ \color{R}{6}\color{B}{I_{1}} -\color{R}{3}\color{B}{I_{2}}-\color{R}{1}\color{B}{I_{3}}=\color{V}{5}, \color{R}-3\color{B}{I_{1}}+\color{R}{7}\color{B}{I_{2}} -\color{R}{2}\color{B}{I_{4}}=\color{V}{-23}, \color{R}{-1}\color{B}{I_{1}}+\color{R}{3}\color{B}{I_{3}}-\color{R}{1}\color{B}{I_{4}}=\color{V}{5}, \color{R}-\color{R}{2}\color{B}{I_{2}}-\color{R}{1}\color{B}{I_{3}} +\color{R}{4}\color{B}{I_{4}}=\color{V}{3} }$$


I get this output:

I know technically, the minus signs, as I wrote the equations, are not part of the coefficients, but rather a shortened way of saying + (-k) where k is a real number. But, I thought that there might be a way to get the LaTeX formatted version of the first image I uploaded. Hence, if you have any idea of what that way is, I'll be very grateful to you :)

PS:Just in case somebody is wondering, that's the system I get from an electric circuit, so that's where the name of the variables come from.

Here, I build it from scratch as a TABstack. I use tokcycle only insofar as employing it's token-list building macro \addcytoks to build the \cytoks token register.

I use listofitems to parse the coefficients and to build up the token register, which in the end gets passed to the TABstack.

Syntax is

\mysystem{<color1>}{<color2>}{<Variable>}{<coefficients>}


The MWE:

\documentclass{article}
\usepackage{listofitems,tokcycle,xcolor,tabstackengine}
\TABstackMath
\newcommand\mysystem[4]{%
\begingroup
\setsepchar{\\/&}%
\foreachitem\z\in\zlist[]{%
\foreachitem\zz\in\zlist[\zcnt]{%
\ifnum\zzcnt=\listlen\zlist[1]\relax
\fi
}%
}
\renewcommand\stackalignment{r}%
\expandafter\tabbedCenterstack\expandafter{\the\cytoks}
\endgroup
}
\setstackgap{L}{1.2\normalbaselineskip}
\begin{document}
$\left\{\mysystem{orange!80!black}{cyan!80!black}{I} {6&-3&-1&&5\\-3&7&&-2&-23\\-1&&3&-1&5\\&-2&-1&4&3}\right.$
\end{document}


If different vertical space is desired between equations, one can alter this line in the preamble,

\setstackgap{L}{1.2\normalbaselineskip}

• Very very good also this answer. Commented Nov 8, 2020 at 19:41
• I think the OP didn't want +-2 ?
– Simd
Commented Nov 8, 2020 at 20:19
• @Anush Thank you, I misunderstood it to the opposite. I have edited my answer. Commented Nov 8, 2020 at 20:35
• @Steven B. Segletes , thank you very much for your reply :O I've never used those packages before, except for xcolor, of course; so It's really nice to see an alternative method that doesn't rely on systeme. I liked it a lot :D Also, @Anush thanks for pointing that out :) Commented Nov 8, 2020 at 21:31

First, I guess from your code that you misunderstood the basic commands of the xcolor package. Its simplest commands that color a text are \color and \textcolor.

• \textcolor takes two mandatory arguments, the first being a valid color, and the second being the input you want to be output in that color. I think this is how you intended to use \color, but that's not how it works.

• \color takes only one mandatory argument which is a defined color. When you call \color{R}, you ask LaTeX to write everything (in the current group) that follows in the color R.

So for example when LaTeX reads \color{R}{I_{1}}, it switches to the color R, and then the reading continues from {I_{1}}, which is not taken as an argument of \color.

Also, I'm not totally sure how \systeme works, but as far as I know, it aligns the system of equations using the signs and the variables, so it seems probable to be that using color commands will mess up the alignment.

Hence, I think I would recommend you to use something else to make the system of equations. The following example uses an array environment.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{xcolor}
\definecolor{V}{HTML}{00a2e8}
\definecolor{R}{HTML}{fbc200}
\definecolor{B}{HTML}{000000}
\newcommand*{\I}[1]{\textcolor{B}{I_{#1}}}
\newcommand*{\p}{\textcolor{B}{+}}
\newenvironment{systemeq}{%
\renewcommand{\arraycolsep}{0pt}
\left\{
\begin{array}{>{\color{R}}r @{\mkern\medmuskip}%
>{\color{R}}c @{\mkern\medmuskip}%
>{\color{R}}r @{\mkern\medmuskip}%
>{\color{R}}c @{\mkern\medmuskip}%
>{\color{R}}r @{\mkern\medmuskip}%
>{\color{R}}c @{\mkern\medmuskip}%
>{\color{R}}r%
@{\mkern\thickmuskip{=}\mkern\thickmuskip}%
>{\color{V}}l}%
}{%
\end{array}
\right.
}
\begin{document}
$\begin{systemeq} 6\I{1} & - & 3\I{2} & - & 1\I{3} & & & 5 \\ -3\I{1} & \p & 7\I{2} & & & - & 2\I{4} & -23 \\ -1\I{1} & & & \p & 3\I{3} & - & 1\I{4} & 5 \\ & - & 2\I{2} & - & 1\I{3} & \p & 4\I{4} & 3 \end{systemeq}$
\end{document}


This approach works well, but it is of course less automatic than the usual usage of the \systeme command.

• Very good...I appreciate your answer....:-) It is done very well. Commented Nov 8, 2020 at 19:40
• @Vincent Thank you very much. I thought there was no hope for formatting it using systeme, so I'm greatful you showed the way :) Also, thanks for clarifying how to use the command. I don´t know why exactly, but when I use \color in Texmaker, it just colors everything it finds in the same line, unless I use it to switch back to the original color. That doesn't happen on Overleaf, for example; but I couldn't use systeme there, so I needed an editor to which I could import that package. And that's the reason why I used \color so many times hehe Commented Nov 8, 2020 at 20:23