Is there some way put boxes around matrix elements without too much effort?
Here is TikZ proposal using fit
. I've wrapped the code in a command I've called mymatrixbox
. It takes 4 inputs which define the boundaries of the box using the cell coordinates (it also takes 1 optional input which allows you some control over the default style).
The way it works is like this, say you want to draw a box from (1,1) to (3,1), then use \mymatrixbox{1}{1}{3}{1}
. To draw a box around a single cell then just repeat the cell coordinates - e.g. \matrixbox{2}{3}{2}{3}
. If I wanted to draw a box around the entire matrix the code for that would be \mymatrixbox{1}{1}{3}{3}
.
\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes={inner sep=6pt}}}
\newcommand*\mymatrixbox[5][]{\node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] {};}
\begin{document}
\begin{tikzpicture}[baseline=0cm]
\matrix [mymatrix] (m)
{
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
};
\mymatrixbox{1}{1}{3}{1}
\mymatrixbox{3}{2}{3}{3}
\mymatrixbox[red]{2}{3}{2}{3}
\end{tikzpicture}
\end{document}
A simple solution with pstricks
and a plain matrix
environment from amsmath
:
\documentclass[11pt, svgnames]{article}
\usepackage{amssymb, amsmath, array}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}
\begin{document}
\begin{equation*}
\begin{postscript}
\psset{nodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue}
\setlength{\arraycolsep}{6pt}
\setlength{\extrarowheight}{3pt}
\begin{matrix}
\Rnode{A}{1}& 0 & 0 \\
0 &1 & \rnode{E}{0} \\
\Rnode{B}{0}& \Rnode{C}{0} & \Rnode{D}{1}
\end{matrix}
\ncbox{A}{B}
\ncbox{C}{D}
\fnode[framesize = 0.56 0.50](E){F}
\end{postscript}
\end{equation*}
\end{document}