# Using multirow with smallmatrix

I'd like to typeset a bunch of small 3x3 matrices to describe generators of certain ideals, as in this paper, p7.

Using smallmatrix works beautifully as long as I only want circles and bullets, but when I try to use multirow to merge cells and put a square across four entries of the matrix, it simply doesn't work. It works in the standard matrix or array environments, but they're far too large for what I need.

I've also tried How to model {smallmatrix} with {array}? but the squares get shrunk and it doesn't quite look right. Since I don't really understand the code in that thread, I don't know how to edit it to make it do what I want. I think it would be fine if I could make the squares a bit bigger.

Here is the code that hopefully illustrates what I'm trying to do. The smallarray environment comes directly from the thread I linked to above.

\documentclass{article}

\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{multirow}

\newcommand{\ZZ}{\multicolumn{2}{c}{\multirow{2}{*}{$\square$}}}

\newenvironment{smallarray}[1]
{\null\,\vcenter\bgroup\scriptsize
\renewcommand{\arraystretch}{0.7}%
\arraycolsep=.13885em
\hbox\bgroup$\array{@{}#1@{}}} {\endarray$\egroup\egroup\,\null}

\begin{document}
When there's no cells to merge, both smallmatrix (left) and smallarray (right) work fine.
$$\begin{smallmatrix} \circ & \circ & \bullet \\ \circ & \circ &\bullet \\ \circ & \circ & \circ \end{smallmatrix} \qquad \begin{smallarray}{ccc} \circ & \circ & \bullet \\ \circ & \circ &\bullet \\ \circ & \circ & \circ \end{smallarray}$$
But when we try to merge some cells...
$$\begin{smallmatrix} \circ & \ZZ \\ \circ & & \\ \circ & \circ & \circ \end{smallmatrix} \qquad \begin{smallarray}{ccc} \circ & \ZZ \\ \circ & & \\ \circ & \circ & \circ \end{smallarray}$$
...smallmatrix just doesn't work.  The second one is almost right and I'd happily just use that, but I also need a matrix of this form:
$\begin{smallarray}{ccc} \circ & \ZZ \\ \ZZ &\\ & & \circ \end{smallarray}$

and that's really not quite right.

\end{document}


Any hints or suggestions would be appreciated.

• Welcome to TeX.SX! \multirow can't work in smallmatrix. – egreg Jul 19 '13 at 11:33

One option using TikZ and a matrix of math nodes:

\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}

\newenvironment{smallarray}[1]
{\gdef\MatName{#1}\begin{tikzpicture}
\matrix[
matrix of math nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={inner sep=1pt,rectangle,text width=2mm,align=center},
text depth=0mm,
text height=2mm,
nodes in empty cells
]  (#1)}
{\end{tikzpicture}}
\def\MyZ(#1,#2){%
\draw ([xshift=-3.6pt,yshift=3.6pt] $(\MatName-#1-#2)!0.5!(\MatName-\the\numexpr#1+1\relax-\the\numexpr#2+1\relax)$ ) rectangle ([xshift=3.6pt,yshift=-3.6pt] $(\MatName-#1-#2)!0.5!(\MatName-\the\numexpr#1+1\relax-\the\numexpr#2+1\relax)$ );
}

\begin{document}

\begin{smallarray}{mat1}
{
\circ & \circ & \circ \\
\circ & \circ & \circ \\
\circ & \circ & \circ \\
};
\end{smallarray}
\begin{smallarray}{mat2}
{
\circ & \circ & \bullet \\
\circ & \circ & \bullet \\
\circ & \circ & \circ \\
};
\end{smallarray}

\begin{smallarray}{mat3}
{
\circ & & \\
\circ & & \\
\circ & \circ & \circ \\
};
\MyZ(1,2)
\end{smallarray}
\begin{smallarray}{mat4}
{
\circ & & \\
& & \\
& & \circ \\
};
\MyZ(1,2)
\MyZ(2,1)
\end{smallarray}

\end{document}


The smallarray environment has one mandatory argument that will be used as the name of the matrix; the second matrix in the image was produced with:

\begin{smallarray}{mat2}
{
\circ & \circ & \bullet \\
\circ & \circ & \bullet \\
\circ & \circ & \circ \\
};
\end{smallarray}


\MyZ allows to draw the squares; the syntax is

\MyZ(x,y)


where x,y is the entry of the upper left corner of the square.

• That's lovely, thank you! For the matrix with two squares, I'm aiming to have just the corners of the square touching in the middle, but I guess that can be achieved by just tweaking the distances slightly? – eithil Jul 19 '13 at 13:54
• @eithil You're welcome! Yes, you can change the lengths used in the shifts in the definition of \MyZ according to your needs; I changed them in my updated answer to produce the described result. – Gonzalo Medina Jul 19 '13 at 14:00