# How to highlight a single element in a matrix?

I am trying to write out the LU decomposition of a matrix A. I am able to highlight the pivot column with elements below the pivot element except when I get to the last column. Does anyone know how I can highlight the last element, 2, in the same format? Or do you have suggestions of other ways that I could do this?

Here's what I'm trying to do.

 \documentclass[12pt,letterpaper]{report}

\usepackage{amsmath}
\usepackage{color}
\usepackage{arydshln}
\usepackage{tikz}
\usetikzlibrary{arrows,matrix,positioning}
\usetikzlibrary{fit}
\tikzset{%
highlight/.style={rectangle,rounded corners,fill=blue!30,draw,
fill opacity=0.2,thick,inner sep=0pt}
}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,
baseline=(#1.base)] \node (#1) {#2};}
%
\newcommand{\Highlight}[1][submatrix]{%
\tikz[overlay,remember picture]{
\node[highlight,fit=(left.north west) (right.south east)] (#1) {};}
}

\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{#1}}
\makeatother

\begin{document}

\begin{align*}
A =
\left[
\begin{array} {*4{c}}
\tikzmark{left}{2}& 1 & -1 & 4 \\
-4 & -3 & 6 & -3 \\
2 & -3 & 20 & 23 \\
\tikzmark{right}{4} & 2 & 13 & 7
\end{array}
\right]
\Highlight[first]
\sim
\begin{matrix}
\\
\mbox{$R_2+2R_1$} \\
\mbox{$R_3-R_1$} \\
\mbox{$R_4-2R_2$}
\end{matrix}
&\left[
2 & 1 & -1 & 4 \\
0 & \tikzmark{left}{-1} & 4 & 5 \\
0 & -4 & 21 & 19 \\
0 & \tikzmark{right}{0} & 15 & -1
\end{matrix}
\right]
\Highlight[first] \\
\sim
\begin{matrix}
\\
\\
\mbox{$R_3-4R_2$} \\
\mbox{ }
\end{matrix}
&\left[
\begin{matrix}
2 & 1 & -1 & 4 \\
0 & -1 & 4 & 5 \\
0 & 0 & \tikzmark{left}{5} & -1 \\
0 & 0 & \tikzmark{right}{15} & -1
\end{matrix}
\right]
\Highlight[first]\\
\sim
\begin{matrix}
\\
\\
\\
\mbox{$R_4-3R_3$}
\end{matrix}
&\left[
\begin{matrix}
2 & 1 & -1 & 4 \\
0 & -1 & 4 & 5 \\
0 & 0 & 5 & -1 \\
0 & 0 & 0 & 2
\end{matrix}
\right]
=U
\end{align*}

\end{document}


I need the last element, 2, to be highlighted just like the other pivot columns.

I am new user so I am not allowed to post an image but I hope someone can help with this.

• Welcome to TeX.SE. Thanks for posting a complete document, but you need to: 1. Eliminate unnecessary packages, and 2. Make it compilable, so that those trying to help can have something to start from. – Peter Grill Jun 20 '12 at 17:50
• Thank you for the tip. I have made the suggested changes. Hopefully, it is easier now. – Suganthi Selvaraj Jun 20 '12 at 18:03
• Still does not compile for me: LaTeX Error: \begin{align*} on input line 34 ended by \end{matrix}. – Peter Grill Jun 20 '12 at 20:59

update

\documentclass{article}
\usepackage{tikz,amsmath}

\begin{document}​
\newcommand\hlight[1]{\tikz[overlay, remember picture,baseline=-\the\dimexpr\fontdimen22\textfont2\relax]\node[rectangle,fill=blue!50,rounded corners,fill opacity = 0.2,draw,thick,text opacity =1] {$#1$};}

\begin{equation*}
\begin{pmatrix}
c &  -a    &   0    & \dots  & \dots  & \dots  &  0 \\
-b &   \hlight{a}   &  -a    & \ddots &        &        & \vdots \\
0 &  -b    &   c    & \ddots & \ddots &        & \vdots \\
\vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \\
\vdots &        & \ddots & \ddots &   c    &  -a    &  0 \\
\vdots &        &        & \ddots &  -b    &   c    & -a \\
0 & \dots  & \dots  & \dots  &   0    &  -b    &  c
\end{pmatrix}
\end{equation*}
\end{document}​


• That's nice! Is there a way I can get the rounded edges just like adjust the opacity like the other columns highlighted? – Suganthi Selvaraj Jun 20 '12 at 18:30
• yres add rounded corners in the node. Be careful I made a mistake in the macro : c instead of #1 – Alain Matthes Jun 20 '12 at 18:34
• \newcommand\hlight[1]{\tikz[overlay, remember picture,baseline=-\the\dimexpr\fontdimen22\textfont2\relax]\node[rectangle,fill=blue!30,rounded corners,fill opacity = 0.2,draw,thick] {#1};} – Suganthi Selvaraj Jun 20 '12 at 18:43
• That's my new command but 2 isn't really visible, i.e. it looks faded compared to the other elements. Do you have any suggestions? – Suganthi Selvaraj Jun 20 '12 at 18:44
• Yes your problem here is that you mix blue!30 very light blue and opacity=.2 very light opacity then you can add text opacity=1. I updated my answer and don't forget $#1$ – Alain Matthes Jun 20 '12 at 18:49

Here is a tikz-free version using xcolor that should get you started:

\documentclass[12pt,letterpaper]{report}

%\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{\highlight}[1]{%
\ooalign{\hss\makebox[0pt]{\fcolorbox{green!30}{red!40}{$#1$}}\hss\cr\phantom{$#1$}}%
}

\begin{document}

$A = \left[\begin{array} {*4{c}} \highlight{2} & 1 & -1 & 4 \\ -4 & -3 & 6 & -3 \\ 2 & \highlight{-3} & 20 & 23 \\ 4 & 2 & \highlight{13} & 7 \end{array}\right]$

\end{document}​


For a quick course in \ooalign, see \subseteq + \circ as a single symbol (“open subset”). Of course, other improvements like colour selection (through optional arguments, say) are all possible.

• yes your answer is a tikz-free version but you change the form of the matrix. Tikz version is form-free but I'm sure that you can do the same only with TeX :) – Alain Matthes Jun 20 '12 at 18:28
• @Altermundus: The box is set in a zero-width box which shouldn't change the horizontal shape of the matrix. Any vertical adjustment could be overcome by \smash, if needed. In the example presented by the OP, this didn't seem to be problematic, so I left it out. – Werner Jun 20 '12 at 18:31

Here is another solution based on TikZ just for matrices.

Basically, using the property of TikZ matrices, it is possible to select a single element and it will be highlighted in by means of the background library. With this approach you don't have to place markers inside the matrices, but you have to put the code to highlight elements inside the pgfonlayer environment. Notice that the matrix it's vertically aligned inside the math environment thanks to [baseline=-\the\dimexpr\fontdimen22\textfont2\relax ] (see use of tikzpicture matrix in align or gather environment).

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,backgrounds}
\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}

\tikzset{mycolor/.style = {rounded corners,line width=1bp,color=#1}}%
\tikzset{myfillcolor/.style = {rounded corners,draw,fill=#1}}%

\NewDocumentCommand{\highlight}{O{blue!40} m m}{%
\draw[mycolor=#1] (#2.north west)rectangle (#3.south east);
}

\NewDocumentCommand{\fhighlight}{O{blue!40} m m}{%
\draw[myfillcolor=#1] (#2.north west)rectangle (#3.south east);
}

\begin{document}

$P= \begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax ] \matrix (m)[matrix of math nodes,left delimiter=(,right delimiter=)] { 2 & 1 & -1 & 4 \\ 0 & -1 & 4 & 5 \\ 0 & 0 & 5 & -1 \\ 0 & 0 & 15 & -1\\ }; \begin{pgfonlayer}{myback} \highlight{m-2-2}{m-2-2} \fhighlight{m-3-3}{m-3-3} \fhighlight[orange]{m-4-4}{m-4-4} \end{pgfonlayer} \end{tikzpicture}$
\end{document}


Result:

The same result could be achieved by means of the fit library using, again, a background layer.

The code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,backgrounds,fit}
\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}

\begin{document}

$P= \begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax ] \matrix (m)[matrix of math nodes,left delimiter=(,right delimiter=)] { 2 & 1 & -1 & 4 \\ 0 & -1 & 4 & 5 \\ 0 & 0 & 5 & -1 \\ 0 & 0 & 15 & -1\\ }; \begin{pgfonlayer}{myback} \node[fit=(m-2-2),rounded corners,draw=blue,scale=0.6]{}; \node[fit=(m-3-3),rounded corners,draw,fill=blue!40,scale=0.6]{}; \node[fit=(m-4-4),rounded corners,draw,fill=orange,scale=0.6]{}; \end{pgfonlayer} \end{tikzpicture}$
\end{document}