Tikz 水平对齐矩阵组

Tikz 水平对齐矩阵组

有人能帮我水平对齐前两个矩阵,使它们相对于下面的其他矩阵居中吗? 在此处输入图片描述

先感谢您。

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

\begin{document}
\begin{tikzpicture}[
    mymatrix/.style={
        matrix of nodes, 
        column sep=-\pgflinewidth,
        nodes={minimum width=8mm, draw},
    },
    ]
    \matrix[mymatrix] (A1) 
    {
        2&8&7&1&3&5&6&|[fill=green]|4\\
    };
 %%%%   
    \matrix[mymatrix, below=1mm of A1] (A2) 
    {
        2&1&3&|[fill=lightgray]|4&7&5&6&8\\
    };
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=5mm of A2] (A31) 
    {
        |[fill=lightgray]|4\\
    };
%%%%%
    \matrix[mymatrix, left=1mm of A31] (A3) 
    {
        2&1&|[fill=green]|3&\\
    };
%%%%%
    \matrix[mymatrix, right=1mm of A31] (A32) 
    {
        7&5&6&|[fill=green]|8\\
    };
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=1mm of A31] (A41) 
{
    |[fill=lightgray]|4\\
};
%%%%%
\matrix[mymatrix, left=1mm of A41] (A4) 
{
    2&1&|[fill=lightgray]|3&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A41] (A42) 
{
    7&5&6&|[fill=lightgray]|8\\
};
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=5mm of A41] (A51) 
{
    |[fill=lightgray]|4\\
};
%%%%%
    \matrix[mymatrix, left=1mm of A51] (A52) 
{
    |[fill=lightgray]|3\\
};
%%%%%
\matrix[mymatrix, left=1mm of A52] (A53) 
{
    2&|[fill=green]|1&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A51] (A54) 
{
    7&5&|[fill=green]|6\\
};
%%%%%
\matrix[mymatrix, right=1mm of A54] (A55) 
{
    |[fill=lightgray]|8\\
};
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=1mm of A51] (A61) 
{
    |[fill=lightgray]|4\\
};
%%%%%
\matrix[mymatrix, left=1mm of A61] (A62) 
{
    |[fill=lightgray]|3\\
};
%%%%%
\matrix[mymatrix, left=1mm of A62] (A63) 
{
    |[fill=lightgray]|1&2&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A61] (A64) 
{
    5&|[fill=lightgray]|6&7\\
};
%%%%%
\matrix[mymatrix, right=1mm of A64] (A65) 
{
    |[fill=lightgray]|8\\
};
\end{tikzpicture}
\end{document}

答案1

顾名思义,amatrix of nodes由组成nodes,因此每个node都有一个姓名的形式<matrix name>-<row number>-<column number>

这里,我将第三个矩阵与第二个矩阵node 4(名为)对齐。A2-1-4

   \matrix[mymatrix, below=5mm of A2-1-4] (A31) 
    {
        |[fill=lightgray]|4\\
    };

截屏

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

\begin{document}
\begin{tikzpicture}[
    mymatrix/.style={
        matrix of nodes, 
        column sep=-\pgflinewidth,
        nodes={minimum width=8mm, draw},
    },
    ]
    \matrix[mymatrix] (A1) 
    {
        2&8&7&1&3&5&6&|[fill=green]|4\\
    };
 %%%%   
    \matrix[mymatrix, below=1mm of A1] (A2) 
    {
        2&1&3&|[fill=lightgray]|4&7&5&6&8\\
    };
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=5mm of A2-1-4] (A31) %<--- alignment of the 4 under the 4
    {
        |[fill=lightgray]|4\\
    };
%%%%%
    \matrix[mymatrix, left=1mm of A31] (A3) 
    {
        2&1&|[fill=green]|3&\\
    };
%%%%%
    \matrix[mymatrix, right=1mm of A31] (A32) 
    {
        7&5&6&|[fill=green]|8\\
    };
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=1mm of A31] (A41) 
{
    |[fill=lightgray]|4\\
};
%%%%%
\matrix[mymatrix, left=1mm of A41] (A4) 
{
    2&1&|[fill=lightgray]|3&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A41] (A42) 
{
    7&5&6&|[fill=lightgray]|8\\
};
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=5mm of A41] (A51) 
{
    |[fill=lightgray]|4\\
};
%%%%%
    \matrix[mymatrix, left=1mm of A51] (A52) 
{
    |[fill=lightgray]|3\\
};
%%%%%
\matrix[mymatrix, left=1mm of A52] (A53) 
{
    2&|[fill=green]|1&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A51] (A54) 
{
    7&5&|[fill=green]|6\\
};
%%%%%
\matrix[mymatrix, right=1mm of A54] (A55) 
{
    |[fill=lightgray]|8\\
};
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=1mm of A51] (A61) 
{
    |[fill=lightgray]|4\\
};
%%%%%
\matrix[mymatrix, left=1mm of A61] (A62) 
{
    |[fill=lightgray]|3\\
};
%%%%%
\matrix[mymatrix, left=1mm of A62] (A63) 
{
    |[fill=lightgray]|1&2&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A61] (A64) 
{
    5&|[fill=lightgray]|6&7\\
};
%%%%%
\matrix[mymatrix, right=1mm of A64] (A65) 
{
    |[fill=lightgray]|8\\
};
\end{tikzpicture}
\end{document}

答案2

要使上部两个矩阵相对于下部矩阵居中,您可以使用local bounding box适合下部矩阵的矩阵,然后相对于该局部边界框放置上部矩阵。这样,如果您更改下部或上部矩阵中的细节,事物仍将居中。

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

\begin{document}
\begin{tikzpicture}[
    mymatrix/.style={
        matrix of nodes, 
        column sep=-\pgflinewidth,
        nodes={minimum width=8mm, draw},
    },
    ]
\begin{scope}[local bounding box=lower]
\matrix[mymatrix] (A31) 
    {
        |[fill=lightgray]|4\\
    };
%%%%%
\matrix[mymatrix, left=1mm of A31] (A3) 
    {
        2&1&|[fill=green]|3&\\
    };
%%%%%
\matrix[mymatrix, right=1mm of A31] (A32) 
    {
        7&5&6&|[fill=green]|8\\
    };
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=1mm of A31] (A41) 
{
    |[fill=lightgray]|4\\
};
%%%%%
\matrix[mymatrix, left=1mm of A41] (A4) 
{
    2&1&|[fill=lightgray]|3&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A41] (A42) 
{
    7&5&6&|[fill=lightgray]|8\\
};
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=5mm of A41] (A51) 
{
    |[fill=lightgray]|4\\
};
%%%%%
    \matrix[mymatrix, left=1mm of A51] (A52) 
{
    |[fill=lightgray]|3\\
};
%%%%%
\matrix[mymatrix, left=1mm of A52] (A53) 
{
    2&|[fill=green]|1&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A51] (A54) 
{
    7&5&|[fill=green]|6\\
};
%%%%%
\matrix[mymatrix, right=1mm of A54] (A55) 
{
    |[fill=lightgray]|8\\
};
%%%%%
%%%%%
%%%%%
    \matrix[mymatrix, below=1mm of A51] (A61) 
{
    |[fill=lightgray]|4\\
};
%%%%%
\matrix[mymatrix, left=1mm of A61] (A62) 
{
    |[fill=lightgray]|3\\
};
%%%%%
\matrix[mymatrix, left=1mm of A62] (A63) 
{
    |[fill=lightgray]|1&2&\\
};
%%%%%
\matrix[mymatrix, right=1mm of A61] (A64) 
{
    5&|[fill=lightgray]|6&7\\
};
%%%%%
\matrix[mymatrix, right=1mm of A64] (A65) 
{
    |[fill=lightgray]|8\\
};
\end{scope}
%%%%%
\matrix[mymatrix, above=5mm of lower] (A2) 
    {
        2&1&3&|[fill=lightgray]|4&7&5&6&8\\
    };
%%%%%
\matrix[mymatrix,above=1mm of A2] (A1) 
    {
        2&8&7&1&3&5&6&|[fill=green]|4\\
    };
%%%%%
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容