23

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?

\begin{equation}\label{eq:appendrow}
\newcommand\x{\times}
\newcommand\y{\colorbox{mygreen}{$1$}}
  \left(\begin{array}{cccc}
    \x  & \x  & \x & \x \\
    0   & \x  & \x & \x \\
    0   & 0   & \x & \x \\
    0   & 0   & 0  & \x \\
    \y  & \y  & \y & \y \\
  \end{array}\right)
\end{equation}

The output is:

enter image description here

29

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}

\begin{equation}\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)
\end{equation}

\begin{equation}
  \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{equation}

\end{document}

enter image description here

  • 1
    Muchisimas gracias! :) – SkyWalker Sep 1 '12 at 19:43
15

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}

\begin{equation}\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)
\end{equation}

\begin{equation}\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{equation}

\end{document}

Result:

enter image description here

  • @Sebastiano: with TeXLive 2019 I have no compilation errors nor warning messages. – Claudio Fiandrino Jul 30 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. – Sebastiano Jul 30 at 14:49
5

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}

enter image description here

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}

enter image description here

  • 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. – Zarko 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. – Zarko Feb 17 '18 at 22:08
  • no, i didn't. i will give more attention to this problem tomorrow. – Zarko 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:-( – Zarko Feb 17 '18 at 23:27
  • about this problem i ask new question: tex.stackexchange.com/questions/415877/… – Zarko Feb 17 '18 at 23:47
3

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:

enter image description here

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:

enter image description here

1

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:

    \begin{equation}\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)
\end{equation}

\begin{equation}\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)
\end{equation}

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

Highlight invasion between 2 matrix using tkiz

SOLUTION

\begin{equation}\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)
\end{equation}

\begin{equation}\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)
\end{equation}

And this is the final solution:

enter image description here

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

  • 1
    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. – Paul Gessler Mar 10 '15 at 21:38

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.