Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

and the output is:

enter image description here

share|improve this question

2 Answers

up vote 5 down vote accepted

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

share|improve this answer
Muchisimas gracias! :) – Giovanni Azua Sep 1 '12 at 19:43

Another approach could be using the hf-tikz package.

Example:

\documentclass{article}
\usepackage{amsmath}

\newcommand\x{\times}

\usepackage[customcolors]{hf-tikz}
\hfsetfillcolor{green!50}
\hfsetbordercolor{white}

\begin{document}

\begin{equation}\label{eq:appendrow}
  \left(\begin{array}{cccc}
    \x  & \x  & \x & \x \\
    0   & \x  & \x & \x \\
    0   & 0   & \x & \x \\
    0   & 0   & 0  & \x \\
    \tikzmarkin{color this row} a  &  b  &  c &  d \tikzmarkend{color this row}\\
  \end{array}\right)
\end{equation}

\begin{equation}\label{eq:appendcol}
  \left(\begin{array}{cccc}
    \x  & \x  & \tikzmarkin{color this column}(0.15,-0.15)(-0.1,0.3) \x & \x \\
    0   & \x  & \x & \x \\
    0   & 0   & \x & \x \\
    0   & 0   & 0  & \x \\
    a  &  b  &  c  \tikzmarkend{color this column} &  d \\
  \end{array}\right)
\end{equation}

\end{document}

Result:

enter image description here

Explanation

The approach is based on the famous \tikzmark macro by Andrew Stacey and Peter Grill. One should just mark the starting and ending position of the area to be highlighted by means of the commands \tikzmarkin and \tikzmarkend. This area could be customized by extend markers passing to the \tikzmarkin the coordinates that represent the shift of markers from the default value. For example:

\tikzmarkin{color this column}(0.15,-0.15)(-0.1,0.3)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.