# Shorter way to color indices of a matrix?

I've made a bordermatrix with brackets [] instead of (), now there are these indices on the top and left side. In my case on the top they are u_1,v_1,u_2,v_2 and so on. On the left side they are F_1x,F_1y,F_2x,F_2y. Now I would like to have all the "u_i" indices in blue and all the "v_i" indices in green. The same for the left side, all the "F_ix" blue and all the "F_iy" green. My solution looks like this and works, but if the matrix has more rows and lines this will be too much code.

\bordermatrix{~ & \color{blue!80!black}  \bf \overline {u}_1
& \color{green!80!black} \bf \overline {v}_1
& \color{blue!80!black}  \bf \overline {u}_5
& \color{green!80!black} \bf \overline {v}_5 \cr
\color{blue!80!black} \bf \overline {F}_{1x} & 336000 & 0 & -336000 & 0 \cr
\color{green!80!black} \bf \overline {F}_{1y} & 0 & 0 & 0 & 0 \cr
\color{blue!80!black} \bf \overline {F}_{5x} & -336000 & 0 & 336000 & 0 \cr
\color{green!80!black} \bf \overline {F}_{5y} & 0 & 0 & 0 & 0 \cr
}


Is there a shorter more elegant way to achieve the same result?

• I think you will find useful this answer to Border matrix with multiple indices on top and bottom. Jun 21, 2013 at 14:53
• Thank you Claudio for your help, but I've already read the link you gave me before. I'am not that good with programming, maybe the solution is there but for me its not visible. Jun 21, 2013 at 15:05
• I've just edited my answer there with another example and some more explanation. Please, don't hesitate to clarify it is still obscure. Jun 21, 2013 at 15:09
• Sorry to bother you, but I just noticed that you have received some good answers to your questions, but you still haven't accepted (nor up-voted) any. Please consider revisiting your questions and accepting the answers (by clicking on the checkmark to their left) that you consider best solved your problems; in case of doubt, please see How do you accept an answer?. Jun 21, 2013 at 15:21
• Hi Gonzalo, thank you for the hint, I didn't know that its possible to vote the answers ;) Jun 21, 2013 at 15:23

Using some commands can do the job easier; the example also includes an example using the blkarray package:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{blkarray}

\newcommand\Gmat[2]{%
\color{green!80!black}\mathbf{\overline{#1}_{#2}}}
\newcommand\Bmat[2]{%
\color{blue!80!black}\mathbf{\overline{#1}_{#2}}}

\begin{document}

$\bordermatrix{ ~ & \Bmat{u}{1} & \Gmat{v}{1} & \Bmat{u}{5} & \Gmat{v}{5} \cr \Bmat{F}{1x} & 336000 & 0 & -336000 & 0 \cr \Gmat{F}{1y} & 0 & 0 & 0 & 0 \cr \Bmat{F}{5x} & -336000 & 0 & 336000 & 0 \cr \Gmat{F}{5y} & 0 & 0 & 0 & 0 \cr }$

$\begin{blockarray}{ccccc} ~ & \Bmat{u}{1} & \Gmat{v}{1} & \Bmat{u}{5} & \Gmat{v}{5} \\ \begin{block}{c[cccc]} \Bmat{F}{1x} & 336000 & 0 & -336000 & 0 \\ \Gmat{F}{1y} & 0 & 0 & 0 & 0 \\ \Bmat{F}{5x} & -336000 & 0 & 336000 & 0 \\ \Gmat{F}{5y} & 0 & 0 & 0 & 0 \\ \end{block} \end{blockarray}$

\end{document}


• wow this is a great solution! Thank you very much! Jun 21, 2013 at 19:48

Here is a way to do that with the environments of nicematrix.

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{nicematrix}

\begin{document}

\newcommand\mat[2]{\mathbf{\overline{#1}_{#2}}}

\NiceMatrixOptions
{
code-for-first-row = \ifodd \value{jCol} \color{blue} \else \color{green} \fi ,
code-for-first-col = \ifodd \value{iRow} \color{blue} \else \color{green} \fi
}
$\begin{pNiceMatrix}[first-row,first-col] & \mat{u}{1} & \mat{v}{1} & \mat{u}{5} & \mat{v}{5} \\ \mat{F}{1x} & 336000 & 0 & -336000 & 0 \\ \mat{F}{1y} & 0 & 0 & 0 & 0 \\ \mat{F}{5x} & -336000 & 0 & 336000 & 0 \\ \mat{F}{5y} & 0 & 0 & 0 & 0 \\ \end{pNiceMatrix}$

$\begin{bNiceMatrix}[first-row,first-col] & \mat{u}{1} & \mat{v}{1} & \mat{u}{5} & \mat{v}{5} \\ \mat{F}{1x} & 336000 & 0 & -336000 & 0 \\ \mat{F}{1y} & 0 & 0 & 0 & 0 \\ \mat{F}{5x} & -336000 & 0 & 336000 & 0 \\ \mat{F}{5y} & 0 & 0 & 0 & 0 \\ \end{bNiceMatrix}$

\end{document}