# TikZ: Align array of matrices

The following code:

\documentclass[tikz,border=1mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix, positioning}

\tikzset{
matrixstyle/.style={
matrix of nodes,
nodes={
draw
}
}}

\begin{document}
\begin{tikzpicture}

\matrix (M) [matrixstyle]{
.1 & .1 \\
.1 & .1 \\
};

\begin{scope} [local bounding box=MA, shift={(M.west)}, left = 0cm of M.west]
\matrix (M11) [matrixstyle]{
.1 & .0  \\
.5 & .0 \\
};
\matrix (M12) [matrixstyle, right = 0cm of M11]{
.2 & .0  \\
.3 & .9 \\
};
\matrix (M21) [matrixstyle, below = 0cm of M11]{
.0 & .0  \\
.0 & .2 \\
};
\matrix (M22) [matrixstyle, right = 0cm of M21]{
.3 & .0  \\
.3 & .9 \\
};
\end{scope}

\matrix (N) [matrixstyle, left = 1cm of MA.west]{
.2 & .2  \\
.2 & .2 \\
};

\end{tikzpicture}
\end{document}


produces the following output:

The output should show six matrices. Four in the middle. And one matrix each on the right and left side of the matrix array. However, I did not manage to place the matrix M to the right of the matrix array MA. The error is probably in the following line

\begin{scope} [local bounding box=MA, shift={(M.west)}, left = 0cm of M.west]

What is the right way to do it? Why does my approach not work?

like this?

matrices you can consider as nodes, so their positioning is with positioning library is straightforward:

\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary{matrix, positioning}

\begin{document}
\begin{tikzpicture}[
node distance = 1mm and 2mm,
every matrix/.style = {matrix of nodes,
nodes={draw},
column sep=-\pgflinewidth,
row sep=-\pgflinewidth}
]
\matrix (m1)
{
.2 & .2  \\
.2 & .2 \\
};
\matrix (m11) [above right=of m1.east]
{
.1 & .0  \\
.5 & .0 \\
};
\matrix (m12) [right=of m11]
{
.1 & .1 \\
.1 & .1 \\
};
\matrix (m21) [below right= of m1.east]
{
.0 & .0  \\
.0 & .2 \\
};
\matrix (m22) [right = of m21]{
.3 & .0  \\
.3 & .9 \\
};

\matrix (m2) [right = of m1 -| m12.east]
{
.2 & .0  \\
.3 & .9 \\
};
\end{tikzpicture}
\end{document}

• What does right = of m1 -| m12.east mean? Especially the -| part. – Samuel Jan 12 at 16:21
• Is it possible to define the four matrices in the middle as a single object that can be moved and resized? I want to resize the four matrices in the middle to have the same size as the matrices on the left and on the right. – Samuel Jan 12 at 16:27
• @Samuel, this is new question in which you need to show (by sketch), how you like scale and shift a group of matrices. probably solution require a different solution as is shown in both answers. i think, that both answers perfectly solve the problem described in this question, therefore it is time, that you accept one of them. – Zarko Jan 13 at 0:21

Although not exactly the same, something very similar can be done with some tabular and without TiKZ.

\documentclass{article}

\newcommand{\mytab}[4]{%
\begin{tabular}[c]{|c|c|}
\hline
#1&#2\\\hline
#3&#4\\\hline
\end{tabular}}

\begin{document}

\mytab{.2}{.2}{.2}{.2}\
\begin{tabular}[c]{cccc}
\mytab{.1}{.0}{.5}{.2} & \mytab{.1}{.1}{.1}{.1}
\\[.5cm]
\mytab{.0}{.0}{.0}{.2} & \mytab{.3}{.0}{.3}{.9}
\end{tabular}\
\mytab{.2}{.0}{.3}{.9}
\end{document}


EDIT: or even better with ytableau package

\documentclass{article}
\usepackage{ytableau}

\newcommand{\mytab}[4]{%
\begin{ytableau}
#1&#2\\
#3&#4\\
\end{ytableau}}

\begin{document}
\ytableausetup{centertableaux}

\mytab{.2}{.2}{.2}{.2}\
\begin{tabular}[c]{cccc}
\mytab{.1}{.0}{.5}{.2} & \mytab{.1}{.1}{.1}{.1}
\\[.5cm]
\mytab{.0}{.0}{.0}{.2} & \mytab{.3}{.0}{.3}{.9}
\end{tabular}\
\mytab{.2}{.0}{.3}{.9}
\end{document}