# Matrix change row or column background

I have a matrix shown below. I would like to have the whole row green rather than only the cells.

How can I do that?



The output is:

You can use \rowcolor, \columncolor, and \cellcolor from the colortbl package, loaded in my example through the xcolor package:

\documentclass{article}
\usepackage{amsmath}
\usepackage[table]{xcolor}

\newcommand\x{\times}
\newcommand\y{\cellcolor{green!10}}

\begin{document}

$$\label{eq:appendrow} \left(\begin{array}{cccc} \rowcolor{red!20} \x & \x & \x & \x \\ 0 & \x & \x & \x \\ \rowcolor{blue!20} 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ \y a & b & \y c & d\\ \end{array}\right)$$

$$\left(\begin{array}{>{\columncolor{olive!20}}cc>{\columncolor{yellow!20}}cc} \x & \x & \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a & b & c & d \\ \end{array}\right)$$

\end{document}


• Muchisimas gracias! :) Sep 1 '12 at 19:43
• Is there a way to do this for diagonals as well? Jan 10 '21 at 16:14

Another approach could be using the hf-tikz package.

Example:

\documentclass{article}
\usepackage{amsmath}

\newcommand\x{\times}

% requires version 0.3 of the package
\usepackage[customcolors]{hf-tikz}

\tikzset{style green/.style={
set fill color=green!50!lime!60,
set border color=white,
},
style cyan/.style={
set fill color=cyan!90!blue!60,
set border color=white,
},
style orange/.style={
set fill color=orange!80!red!60,
set border color=white,
},
hor/.style={
above left offset={-0.15,0.31},
below right offset={0.15,-0.125},
#1
},
ver/.style={
above left offset={-0.1,0.3},
below right offset={0.15,-0.15},
#1
}
}

\begin{document}

$$\label{eq:appendrow} \left(\begin{array}{cccc} \x & \x & \x & \x \\ 0 & \tikzmarkin[hor=style orange]{el} \x & \x\tikzmarkend{el} & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ \tikzmarkin[hor=style green]{row} a & b & c & d \tikzmarkend{row}\\ \end{array}\right)$$

$$\label{eq:appendcol} \left(\begin{array}{cccc} \tikzmarkin[ver=style cyan]{col 1}\x & \x & \tikzmarkin[ver=style green]{col 2} \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a \tikzmarkend{col 1} & b & c \tikzmarkend{col 2} & d \\ \end{array}\right)$$

\end{document}


Result:

• @Sebastiano: with TeXLive 2019 I have no compilation errors nor warning messages. Jul 30 '19 at 6:54
• Claudio excuse me very much for my comment that I have deleted. Probabily using Papeeria Live demo online I have setteed TeXLive 2015 as default. Excuse me. My best regards. Jul 30 '19 at 14:49

I wanted to answer this post: how to obtain vertical and horizontal gray bars in a matrix? but it was closed as a duplicate of the one I'm answering now.

However, since here there's no a solution with a TikZ matrix yet:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, fit}
\usetikzlibrary{backgrounds}
\newcommand\x{\times}

\begin{document}
\begin{tikzpicture}
\matrix[
matrix of math nodes,
row sep=.5ex,
column sep=.5ex,
left delimiter=(,right delimiter=),
nodes={text width=.75em, text height=1.75ex, text depth=.5ex, align=center}
] (m)
{
\x & \x & \x & \x \\
0  & \x & \x & \x \\
0  & 0  & \x & \x \\
0  & 0  & 0  & \x \\
1  & 1  & 1  & 1 \\
};
\begin{scope}[on background layer]
\node[fit=(m-2-1)(m-2-4), draw=green!30, fill=green!30, rounded corners] {};
\node[fit=(m-1-3)(m-5-3), draw=green!30, fill=green!30, rounded corners] {};
\node[fit=(m-2-3), fill=green] {};
\end{scope}
\end{tikzpicture}
\end{document}


Alternatively, a solution like Zarko's answer, with rounded corners:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
\newcommand\x{\times}

\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,
nodes={rectangle,
minimum size=1.2em, text depth=0.25ex,
inner sep=0pt, outer sep=0pt,
anchor=center},
column sep=-0.5\pgflinewidth,
row sep=-0.5\pgflinewidth,
inner sep=0pt,
left delimiter=(, right delimiter=),
row 2 column 2/.append style={nodes={draw=cyan,fill=cyan}},
] (m)
{
a_1 & \x  & a_3 \\
a_4 & \x  & a_6 \\
a_7 & \x  & a_9 \\
};
\begin{scope}[on background layer]
\filldraw[cyan!50, rounded corners] (m-2-1.north west) --
(m-2-1.south west) -- (m-2-3.south east)-- (m-2-3.north east)--
cycle;
\filldraw[cyan!50, rounded corners] (m-1-2.north west) --
(m-3-2.south west) -- (m-3-2.south east)-- (m-1-2.north east)--
cycle;
\end{scope}
\end{tikzpicture}
\end{document}


• testing your nice solution (+1), i discover interesting "malfunction" of matrix of math nodes: if i use minimum size=... instead of text width=..., text height=... i receive error: missing $ ... now i wonder why. i will try to investigate this more. Feb 17 '18 at 21:58 • i test simple example based on example in tikz & pgf manual, page 647, third example, and there i don't receive any error. now i will look closely, what is in your code different to my example. Feb 17 '18 at 22:08 • no, i didn't. i will give more attention to this problem tomorrow. Feb 17 '18 at 22:59 • i found cause of problem: if i comment line left delimiter=(, right delimiter=),, than my experiment works, however, with this delimiters are lost:-( Feb 17 '18 at 23:27 • about this problem i ask new question: tex.stackexchange.com/questions/415877/… Feb 17 '18 at 23:47 Within {pNiceMatrix} of nicematrix, you have a command \Block which may be used for highlighting a part of the matrix. \documentclass{article} \usepackage{nicematrix} \begin{document}$\begin{pNiceMatrix}[margin]
a & b & c & d \\
\Block[fill=red!15,rounded-corners]{1-4}{}
a & b & c & d \\
a & b & c & d \\
a & b & \Block[draw,fill=blue!15,rounded-corners]{2-2}{}
c & d \\
a & b & c & d \\
\end{pNiceMatrix}\$

\end{document}


You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

if the rounded border of bars is not essential, than an alternative to CarLaTeX answer, which is used as base in preparing mine, is:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}
\newcommand\x{\times}

\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,
nodes={rectangle, %draw, very thin,
minimum size=1.2em, text depth=0.25ex,
inner sep=0pt, outer sep=0pt,
fill opacity=0.5, text opacity=1,
anchor=center},
column sep=-0.5\pgflinewidth,
row sep=-0.5\pgflinewidth,
column 2/.append style = {nodes={fill=cyan!50}},
row 2/.append style = {nodes={fill=cyan!50}},
row 2 column 2/.append style={nodes={fill=cyan}},
inner sep=0pt,
left delimiter=(, right delimiter=),
]
{
a_1 & \x  & a_3 \\
a_4 & \x  & a_6 \\
a_7 & \x  & a_9 \\
};
\end{tikzpicture}
\end{document}


(well, matrix is not original but from some my experiments), which gives:

addendum: above answer contain some wrong assumptions: (i) how transparency work (on level of nodes it hasn't any sense, so better is not use it on such way); (ii) transparency had to be resolved on row 2 and column style definition (see mwe below) ; (iii) nodes with only fill should not overlap each other for edge lines width, so the sep=-0.5\pgflinewidth, row sep=-0.5\pgflinewidth had to be removed; (iv) from comparison of nodes definition in CarLaTeX answer and mine i recently observe important difference: nodes={text width=.75em, text height=1.75ex, text depth=.5ex, align=center} but i try to use in form nodes={minimum size=1.75ex, text depth=.5ex, align=center} where align=center cause error (removing it remove the error).

for comparison i left first mwe above as it is. corrected one is:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}
\newcommand\x{\times}

\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,
nodes={rectangle,
minimum size=1.5em, text depth=0.25ex,
inner sep=0pt, outer sep=0pt,
anchor=center},
row 2/.append style = {nodes={preaction={fill=cyan!30}}},
column 2/.append style = {nodes={fill=red!60},fill opacity=0.5, text opacity=1},
inner sep=0pt,
left delimiter=(, right delimiter=),
]
{
a_1 & \x  & a_3 \\
a_4 & \x  & a_6 \\
a_7 & \x  & a_9 \\
};
\end{tikzpicture}
\end{document}


which gives:

An alternative solution with tblr environment of tabularray package:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor}

\usepackage{tabularray}
\SetTblrInner{colsep=5pt,rowsep=1pt}

\newcommand\x{\times}

\begin{document}

$$\label{eq:appendrow} \left(\begin{tblr}{ colspec = {cccc}, row{1} = {red9}, row{3} = {blue9}, cell{5}{1,3} = {green9}, } \x & \x & \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a & b & c & d \\ \end{tblr}\right)$$

$$\left(\begin{tblr}{Q[c,olive9]cQ[c,yellow9]c} \x & \x & \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a & b & c & d \\ \end{tblr}\right)$$

\end{document}


Thank you for your answer. I would like to share that tip. First I will explain the problem, and after that, I will post the solution.

## PROBLEM

If I do that:

    $$\label{eq:appendcol} \left(\begin{array}{cccc} \tikzmarkin[ver=style red]{col 1}\x & \x & \tikzmarkin[ver=style red]{col 2} \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a \tikzmarkend{col 1} & b & c \tikzmarkend{col 2} & d \\ \end{array}\right)$$

$$\label{eq:appendcol} \left(\begin{array}{cccc} \tikzmarkin[ver=style red]{col 1}\x & \x & \tikzmarkin[ver=style red]{col 2} \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a \tikzmarkend{col 1} & b & c \tikzmarkend{col 2} & d \\ \end{array}\right)$$


Vertical highlighting continues from first matrix to second, invading the space between them. The result:

## SOLUTION

$$\label{eq:appendcol} \left(\begin{array}{cccc} \tikzmarkin[ver=style red]{col 1}\x & \x & \tikzmarkin[ver=style red]{col 2} \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a \tikzmarkend{col 1} & b & c \tikzmarkend{col 2} & d \\ \end{array}\right)$$

$$\label{eq:appendcol-second} \left(\begin{array}{cccc} \tikzmarkin[ver=style red]{col 1-a}\x & \x & \tikzmarkin[ver=style red]{col 2-a} \x & \x \\ 0 & \x & \x & \x \\ 0 & 0 & \x & \x \\ 0 & 0 & 0 & \x \\ a \tikzmarkend{col 1-a} & b & c \tikzmarkend{col 2-a} & d \\ \end{array}\right)$$


And this is the final solution:

Special thanks to @percusse and @claudio-fiandrino that helped me to solve the problem on that post:

Problems with matrix highlighting in two separate equations using hf-tikz

• This space is reserved for answers. If you have another question that is not answered here, please ask it by clicking the Ask Question button. Follow-up questions like this are more than welcome! You can link to this question to provide background if it helps motivate/clarify your question. Mar 10 '15 at 21:38