# tikz pgf - Add background color to specific cells of a Matrix

Hi i'm interested in how to add background to specific cells of a matrix.

I already know a bit about how to build a matrix, my given code looks like:

\usepackage{tikz}
\usetikzlibrary{arrows, fit, matrix, positioning, shapes}

\begin{tikzpicture}[>=latex]
\node (B) {$B^\prime=$};
\matrix (M) [matrix of math nodes,left delimiter={[}, right delimiter={]},
matrix anchor=west, right=0.5em of B,
column 1/.style={anchor=base east},
column 2/.style={anchor=base east},
column 3/.style={anchor=base east}]
{
a & b & c\\
b & d & e\\
c & e & a+b+1\\
};
\node  [fit=(M-1-1) (M-1-3), draw, rectangle, blue]{};
\node  [fit=(M-2-2) (M-2-3), draw, rectangle, blue]{};
\node  [fit=(M-3-3) (M-3-3), draw, rectangle, blue]{};
\end{tikzpicture}


As you can see im creating a right-aligned matrix, because my element at cell(3,3) is very long. To point out that my matrix is quadratic, I thought about highlighting row elements 1-1 to 1-3, 2-2 to 2-3, and element 3-3.

I would want to give them a background color, lets say some sort of blue. I didn't manage to fill out the rectangles without covering the matrix elements below.

I would be greatful for any hints or solutions :)

• Does this answer help? tex.stackexchange.com/a/112433/87678 Sep 18, 2018 at 12:34
• Welcome to TeX.SE! You can simply add fills. For instance, if the 1-1 cell should be shaded, replace the first line by of the matrix |[fill=blue!30]|a & b & c\\ . If you want to fill the rectangles you draw with fit, you may want to load the backgrounds library and do the fills/fits in a scope \begin{scope}[on background layer] such that the texts stay in the foreground. You can of course fill a whole column by column 1/.style={anchor=base east,fill=red},, say, and likewise for rows.
– user121799
Sep 18, 2018 at 12:40
• @David Purton Thanks, the |[fill=<color>| option is something i want, but: "Fill" does follow the written letters, not the cells of a matrix. So if I do this operation cell-wise, the filled boxes will overlap, and have some white areas in between. I want boxes with equal height for element 1,1 to 1,3, 2,2 to 2,3 and element 3,3 is that possible? Sep 18, 2018 at 12:54
• You only need to add nodes={minimum height=6mm}, to the options of the matrix. Of course, 6mm is not carved in stone.
– user121799
Sep 18, 2018 at 13:02
• @marmot I think your comment about adding an background layer is more intuitive. Is there a way to draw 3 boxes like this example: tex.stackexchange.com/questions/220646/… for my written cells? I tried \scoped[on background layer] \node[fill=pink!50, fit=(M-1-1)(M-1-3) (M-2-2)(M-2-3) (M-3-3)] {}; Sep 18, 2018 at 13:09

i finally managed to find my desired answer.

\usepackage{tikz}
\usetikzlibrary{arrows, fit, matrix, positioning, shapes, backgrounds,
}

\begin{tikzpicture}[>=latex]
\node (B) {$B^\prime=$};
\matrix (M) [matrix of math nodes,left delimiter={[}, right delimiter={]},
matrix anchor=west, right=0.5em of B,
column 1/.style={anchor=base east},
column 2/.style={anchor=base east},
column 3/.style={anchor=base east}]
{
a & b & c\\
b & d & e\\
c & e & a+b+1\\
};
\scoped[on background layer]
{
\node[fill=pink!50, fit=(M-1-1)(M-1-3) ]   {};
\node[fill=pink!50, fit=(M-2-2)(M-2-3) ]   {};
\node[fill=pink!50, fit=(M-3-3)(M-3-3) ]   {};
}
\end{tikzpicture}


it's not super perfect, but i'll take it. Thanks for your suggestions.