# Highlight elements in the matrix

I have a matrix and need to highlight some elements in the matrix. I have found how to highlight a single element (I want to put the cell in the square):

$$A = \begin{bmatrix} \fbox{0} & \fbox{0} & 0 \\ \fbox{0} & \fbox{0} & 0 \\ 1 & 1 & 1 \\ \end{bmatrix}$$


But how can I outline elements of the submatrix like in the picture below? I need just a frame where some elements are located.

-
Welcome to TeX.sx! Please make sure that all images are uploaded using the official stackexchange interface, i.e. the image icon on top of the text field (shortcut: CTRL+G). This ensures that all images are always accessible and do not expire. As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. – Martin Scharrer Jan 4 '12 at 13:57
– percusse Jan 4 '12 at 13:58
I need put some elements in the square, not highlight. See the picture – Eugene Jan 4 '12 at 15:18

I recommand tikz. The result:

The code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,matrix,positioning}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
{
8 &8 &1 &6 \\
3 &8 &5 &7 \\
4 &8 &9 &5 \\
};
\draw[color=red] (m-1-1.north west) -- (m-1-3.north east) -- (m-2-3.south east) -- (m-2-1.south west) -- (m-1-1.north west);
\draw[color=red,double,implies-](m-1-2.north) -- +(0,0.3);
\end{tikzpicture}
\end{document}

-
Note that, if all the array elements aren't the same width, this box won't be square. It'll be trapezoidal or some such nonsense. Which is disappointing, if understandable. – Parthian Shot Oct 9 '15 at 9:04

You could use TikZ, and

• the fit library for creating nodes fitting the desired area,
• a style for the highlighted node, so separated from the code and easy to change,
• the TikZ options overlay and remember picture, so you can later refer to those nodes, for example for drawing arrows and annotations later.

To demonstrate why it's useful to have nodes for reference, I created an example, which shows transposing a matrix, highlights a submatrix both in the original matrix and the result, connected by an arrow and annotated:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{%
highlight/.style={rectangle,rounded corners,fill=red!15,draw,
fill opacity=0.5,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) {};}
}
\begin{document}
$M = \left(\begin{array}{*5{c}} \tikzmark{left}{1} & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & \tikzmark{right}{13} & 14 & 15 \\ 16 & 17 & 18 & 19 & 20 \end{array}\right) \Highlight[first] \qquad M^T = \left(\begin{array}{*5{c}} \tikzmark{left}{1} & 6 & 11 & 16 \\ 2 & 7 & 12 & 17 \\ 3 & 8 & \tikzmark{right}{13} & 18 \\ 4 & 9 & 14 & 19 \\ 5 & 10 & 15 & 20 \end{array}\right)$
\Highlight[second]
%
\tikz[overlay,remember picture] {
\draw[->,thick,red,dashed] (first) -- (second) node [pos=0.66,above] {Transpose};
\node[above of=first] {$N$};
\node[above of=second] {$N^T$};
}
\end{document}​

-
I certainly like this answer but is it restricted to only numbers? I tried this with a matrix with entries like $\frac{\partial f}{\partial x}$ (within the $) and I got errors thrown for missing$ but putting them in did not fix the problem. – John M Jun 27 '15 at 21:03
edit: nevermind! – John M Jun 27 '15 at 21:29

You can use \tikzmark from this answer by Andrew Stacey to mark the endpoints where you want the box. This allows you to do the matrix in the usual way (outside of tikz). Since this is very similar to Werner's solution that used pstricks, I adapted that code to use \tikzmark:

## Notes:

• This does require two runs: the first to compute the positions of the box, and the second to draw it in the correct spot.
• Since this is using tikz, you automatically get all the flexibility inherent in tikz, such as line styles, line thickness, line color, fill, etc. These can be passed to the \DrawBox macro to customize each instance or provided as default options to maintain consistency.

## Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[1][]{%
\tikz[overlay,remember picture]{
\draw[red,#1]
($(left)+(-0.2em,0.9em)$) rectangle
($(right)+(0.2em,-0.3em)$);}
}

\begin{document}
$M = \left[\begin{array}{*{13}{c}} \tikzmark{left}0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 1 & 0 & 1 & 0 \\ 0 & 1 & 0\tikzmark{right} & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 1 & 1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \end{array}\right]$
\DrawBox[thick]
\end{document}​

-

Here is a solution using Tikz. You may also want to read the solution suggested by percusse in his comment, although I think my solution is simpler to understand for a newcommer to tikz.

comment : my original solution included an align command in the matrix, this actually turned everything in the matrix into text, not math stuff. I removed the align instruction, so now if you compile the code, the stuff is actually in math mode and the minus signs are correct. I did not change the image.

The code is

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}

\matrix[matrix of math nodes,left delimiter = (,right delimiter = ),row sep=10pt,column sep = 10pt] (m) {
1&3&-8\\
2&0&1\\
-7&9&1\\
};

\draw (m-1-1.north west) -- (m-1-1.south west) -- (m-3-3.south west) -- (m-3-3.south east)
-- (m-3-3.north east) -- (m-1-1.north east) -- cycle;
\end{tikzpicture}

\begin{tikzpicture}

\matrix[matrix of math nodes,left delimiter = (,right delimiter = ),row sep=10pt,column sep = 10pt] (m) {
1&3&-8\\
2&0&1\\
-7&9&1\\
};

\draw (m-1-1.north west) -- (m-2-1.south west) -- (m-2-2.south east) -- (m-1-2.north east) -- cycle;
%the previous line can be replaced by \draw (m-1-1.north west) rectangle (m-2-2.south east);
\end{tikzpicture}

\end{document}


The result is

-
Very interesting solution. Is there a way of forcing the numbers of the matrix to be in math mode automatically, so that the minus signs in front of "8" and "7" aren't typeset as short dashes? – Mico Jan 4 '12 at 14:58
@Mico: see change to solution. The "matrix of math nodes" instruction does what you want. – Frédéric Jan 4 '12 at 15:06
That's pretty awesome. – Todd Lehman Jan 4 '12 at 20:01

Here's a method that uses only LaTeX's "built-in" commands \cline and \multicolumn to create the frame around the upper-left 3x3 submatrix.

By the way, because your matrix has more than 10 columns, I believe it's necessary to either use the array environment instead of the bmatrix environment (bmatrix is essentially a "wrapper" around the array construct) or to increase the counter MaxMatrixCols in order to avoid unwelcome surprises while setting up a 13-column matrix via bmatrix.

Separately, for the case at hand, I would not load the array package; if you were to load the array package, you'd get as a (presumably unwelcome) side-effect a widening of the inter-column space between columns 3 and 4 by the width of the frame line.

\documentclass{article}
\begin{document}
$M = \left[ \: % \: serves as a spacer between the left-hand bracket of % the matrix and the left-hand side of the inner frame \begin{array}{*{13}{c}} \cline{1-3} \multicolumn{1}{|c}{0} & 1 & \multicolumn{1}{c|}{0} & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0\\ \multicolumn{1}{|c}{1} & 0 & \multicolumn{1}{c|}{1} & 0 & 0 & 0 & 1 & 1 & 0 & 1 & 0 & 1 & 0\\ \multicolumn{1}{|c}{0} & 1 & \multicolumn{1}{c|}{0} & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1\\ \cline{1-3} 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ \end{array} \right]$
\end{document}


-
bmatrix is a wrapper that uses array internally, so \multicolumn and \cline work. – egreg Jan 4 '12 at 15:36

Another approach always using TikZ.

Since the backgrounds library was not exploited so far, I made use of it; first I defined two commands that allow to fill the background of the elements highlighted or not:

\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);
}


corresponding to the styles:

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


Inside the document, to highlight some elements of the matrix, it must be used the environment pgfonlayer with a dedicated background layer myback:

\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}


Then it is possible to call:

\fhighlight{m-1-1}{m-3-3}


or

\highlight[red]{m-2-2}{m-4-5}


The complete example:

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

\tikzset{mycolor/.style = {line width=1bp,color=#1}}%
\tikzset{myfillcolor/.style = {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}

Using the \verb!myfillcolor style! (\verb!\fhighlight! command):
$P= \begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax ] \matrix (m)[matrix of math nodes,left delimiter=(,right delimiter=)] { 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ }; \begin{pgfonlayer}{myback} \fhighlight{m-1-1}{m-3-3} \fhighlight[green!30]{m-3-9}{m-5-13} \end{pgfonlayer} \end{tikzpicture}$

Using the \verb!mycolor style! (\verb!\highlight! command):
$P= \begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax ] \matrix (m)[matrix of math nodes,left delimiter=(,right delimiter=)] { 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ }; \begin{pgfonlayer}{myback} \highlight[red]{m-2-2}{m-4-5} \highlight[orange]{m-5-7}{m-6-9} \end{pgfonlayer} \end{tikzpicture}$

It is even possible to highlight single elements:
$P= \begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax ] \matrix (m)[matrix of math nodes,left delimiter=(,right delimiter=)] { 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ }; \begin{pgfonlayer}{myback} \foreach \element in {m-1-1,m-6-5,m-5-3}{ \highlight[violet]{\element}{\element} } \foreach \element in {m-3-10,m-4-8,m-5-12}{ \fhighlight[violet!20]{\element}{\element} } \end{pgfonlayer} \end{tikzpicture}$
\end{document}


The result:

-
+1 for joining the party late and the idea to use backgrounds :-) – Daniel Jul 1 '12 at 10:12

Here is a pstricks-related approach that requires pst-node:

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{pst-node}% http://ctan.org/pkg/pst-node
\begin{document}
$M = \left[\begin{array}{*{13}{c}} \pnode(-.5ex,2ex){left}0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 1 & 0 & 1 & 0 \\ 0 & 1 & 0\pnode(.5ex,-.5ex){right} & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 1 & 1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \end{array}\right]$

\psframe[linestyle=dashed,linecolor=red!60,linewidth=1pt](left)(right)% Draw frame
\end{document}​


\psframe[<option>](<ll>)(<ur>) draws a frame from the lower-left coordinate <ll> to the upper right coordinate <ur> with <options>. xcolor provides colour shades (red!60 or 60% red in this case). This requires a latex->dvips->ps2pdf or xelatex compile sequence, due to pstricks.

For more information on colouring or boxing array cells, see Herbert Voß' mathmode document. In particular, sections 62.5 Colored cells and 62.6 Boxed rows and columns (p 107) as part of the discussion on 62 Arrays.

-

run with xelatex or latex=>dvips=>ps2pdf

\documentclass{article}
\usepackage{pst-node}
\begin{document}
$M = \left[\begin{array}{*{13}{c}} \rnode[lt]{left}{0} & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 1 & 0 & 1 & 0 \\ 0 & 1 & \rnode[rb]{right}{0} & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 1 & 1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \end{array}\right]$
\psframe[fillstyle=solid,fillcolor=red,opacity=0.2,linecolor=red,framearc=0.1]%
([nodesep=3pt,angle=135]left)([nodesep=3pt,angle=-45]right)% Draw frame

\end{document}​


-

It seems that Claudio Fiandrino forgot to mention his hf-tikz package as an option for this question.

\documentclass{article}
\usepackage{hf-tikz}

\begin{document}

$P= \left( \begin{array}{*{13}{c}} \tikzmarkin{a}(0.1,-0.1)(-0.1,0.35)0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1\tikzmarkend{a} & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & \tikzmarkin{b}(0.1,-0.1)(-0.1,0.35)0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\tikzmarkend{b} & 1 & 0 \end{array} \right)$

\end{document}


-