65

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.

Submatrix

  • 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
  • 1
    Related : Highlighting the diagonal of a square matrix – 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

11 Answers 11

46

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:

Transposing a matrix

\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}​
  • Nice solution, but you can only highlight a single selection per matrix. Isn't it? – G M Feb 25 at 13:30
  • 1
    @GM It's just a demonstration. The point is using \tikzmark. You can use it many times and use different labels, like left1 or left2 etc. The \Highlight helper macro is mad just to use left and right, but you can change that for example to have labels as arguments and repeat to highlight several selections in the same matrix. – Stefan Kottwitz Feb 25 at 13:43
44

I recommend TikZ. The result:

enter image description here

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}
  • 3
    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
35

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:

enter image description here

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.

Update:

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}​
31

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

enter image description here

  • 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
26

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}

enter image description here

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

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:

enter image description here

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

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

enter image description here

\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.

20

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}​

enter image description here

6

easybmat offers an alternative syntax for math tabulars and also includes the command addpath which allows to draw a path between matrix elements.

BMAT syntax is

\begin{BMAT}{ccccc}{ccccc}...\end{BMAT}

where first parameter represents alignment in columns and second alignment in rows. It also offers other optional parameters to define relation between columns widths and/or rows heights.

Inside a BMAT environment it's possible to use command addpath to draw a line between matrix elements. As an example:

\addpath{(0,1,1)rruulldd}

starts a path from 0,1 (being 0,0 the bottom left corner, each unit moves one row to the right or column to top), 1 defines the kind of line to draw, and rruulldd defines the path, each letter draws one column/row to right, up, left or down.

An example of use:

\documentclass{article}
\usepackage{easybmat}

\begin{document}

\[P=
\left(
\begin{BMAT}{ccccc}{ccccc}
0 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 
\addpath{(0,1,1)rruulldd}
\addpath{(1,5,1)drrdrdrdld}
\end{BMAT}
\right)
\]

\end{document}

and the result

enter image description here

4

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}

enter image description here

1

Simple tikz solution

I prefer to use pure tikz without defining new commands or importing packages, using a rectangle that takes the lower left corner and the upper right corner as input. In this case, we use the index of the matrix m that we define previously:

\draw (lower left coordinates) rectangle (upper right corner);

In the matrix I wrote the coordinates of the elements so it is easier to understand which are the starting and ending points.

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{arrows,matrix,positioning}
    \begin{tikzpicture}
        \matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
        {
           i_{1,1} & i_{1,2} & i_{1,3} & i_{1,4} & i_{1,5} & i_{1,6} \\ 
            i_{2,1} & i_{2,2} & i_{2,3} & i_{2,4} & i_{2,5} & i_{2,6} \\ 
            i_{3,1} & i_{3,2} & i_{3,3} & i_{3,4} & i_{3,5} & i_{3,6} \\ 
            i_{4,1} & i_{4,2} & i_{4,3} & i_{4,4} & i_{4,5} & i_{4,6} \\ 
            i_{5,1} & i_{5,2} & i_{5,3} & i_{5,4} & i_{5,5} & i_{5,6} \\ 
            i_{6,1} & i_{6,2} & i_{6,3} & i_{6,4} & i_{6,5} & i_{6,6} \\ 
        };
        %simple rectangle
        \draw (m-3-1.south west) rectangle (m-1-3.north east);
        %fancy blue rectangle
        \draw[rounded corners,ultra thick, draw=black, fill=blue, opacity=0.3] (m-6-4.south west) rectangle (m-4-6.north east);             
    \end{tikzpicture}    
\end{document}

Compared to other answers

This answer is similar to someonr answer however his answer will output trapezoidal shape if the matrix elements have different lengths, compared to Stephen answer it can handle multiple selections without modifying the code.

For reference I post also a Python 2.7 script for generating the matrix which can be quite a tedious task to perform manually:

size = 6
for i in range(1,size+1):
    for j in range(1,size+1):
        if j%size != 0 or j == 0:
            print "i_{%s,%s} &" %(i,j),
        else: 
            print r"""i_{%s,%s} \\ """ %(i,j)

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.