tikz 中多列节点矩阵

tikz 中多列节点矩阵

我想用 tikz 创建一个图表,其中条目会覆盖多列。我已经使用 fit 包解决了这个问题。但现在矩阵中的节点外观存在问题。多列矩形中的文本未居中。我该如何解决这个问题?

\documentclass[
    a4paper,
    12pt,
    headings=normal,      % small
]{scrartcl}

\usepackage{tikz}
\usetikzlibrary{positioning,arrows,chains,matrix,scopes,fit}

\begin{docoument}

\begin{tikzpicture}

\tikzset{element/.style={rectangle, 
                            inner sep=0pt,
                            minimum width=15mm,
                            very  thick,
                            draw = black,
                            top  color=blue!20!white!20, bottom  color=blue!70!white,
                            font=\tiny,
                            }}
\tikzset{element2/.style={rectangle,    
                            minimum  size=5mm,
                            very  thick,
                            draw = black,
                            top  color=blue!20!white!20, bottom  color=blue!70!white,
                            font=\tiny
                            }}
\tikzset{point/.style={circle,
                        inner  sep=0pt,
                        minimum  size=2pt,
                        fill=red
                        }}

    \matrix (table) [%
    matrix of nodes,
    nodes in empty cells,
    row  sep=4mm,column  sep=5mm,
    nodes={text centered}
    ] {%
        \node(p1)[point]{}; &  & \node(p2)[point]{}; &  &  & \node(p3)[point]{}; &  &  & \\
        &  &  &  &  &  &  &  &\\
        &  &  &  &  &  & \node()[element2]{$+1$}; &  & \\
        &  &  & \node(p4)[point]{}; &  &  &  &  & \node(p5)[point]{}; \\
        &  &  &  &  &  &  &  & \\
        \node(p6)[point]{}; &  &  & \node(p7)[point]{}; &  &  &  &  & \\
        & \node(p8)[point]{}; &  &  &  &  & \node(p9)[point]{}; &  & \\
        &  &  &  &  &  &  &  & \\
        \node(p10)[point]{}; &  &  &  &  &  &  &  & \\
    };
    \node[fit=(table-2-2)(table-2-4),element]{label1};
    \node[fit=(table-2-7)(table-2-9),element]{label2};
    \node[fit=(table-5-2)(table-5-3),element]{label3};
    \node[fit=(table-5-6)(table-5-7),element]{label4};
    \node[fit=(table-7-4)(table-7-5),element]{label5};
    \node[fit=(table-8-4)(table-8-5),element]{label6};

\end{tikzpicture}

\end{docoument}

答案1

fit该库会进行一些额外的测量来覆盖给定的坐标,显然它不尊重锚点的位置,base因此最好为拟合节点添加标签。Claudio 的评论是一种选择,但如果拟合节点跨越更多行,它可能会失效。

另外,在matrix of nodes设置中您不需要提供额外的节点声明。

\documentclass[
    a4paper,
    12pt,
    headings=normal,      % small
]{scrartcl}

\usepackage{tikz}
\usetikzlibrary{positioning,arrows,chains,matrix,scopes,fit}

\begin{document}

\begin{tikzpicture}

\tikzset{element/.style={rectangle, 
                            inner sep=0pt,
                            minimum width=15mm,
                            very  thick,
                            draw = black,
                            top  color=blue!20!white!20, bottom  color=blue!70!white,
                            }}
\tikzset{element2/.style={rectangle,    
                            minimum  size=5mm,
                            very  thick,
                            draw = black,
                            top  color=blue!20!white!20, bottom  color=blue!70!white,
                            font=\tiny
                            }}
\tikzset{point/.style={circle,
                        inner  sep=0pt,
                        minimum  size=2pt,
                        fill=red
                        }}

    \matrix (table) [%
    matrix of nodes,
    nodes in empty cells,
    row  sep=4mm,column  sep=5mm,
    nodes={text centered}
    ] {%
        |[point]| &  & |[point]| &  &  & |[point]| &  &  & \\
        &  &  &  &  &  &  &  &\\
        &  &  &  &  &  & |[element2]|$+1$ &  & \\
        &  &  & |[point]| &  &  &  &  & |[point]| \\
        &  &  &  &  &  &  &  & \\
        |[point]| &  &  & |[point]| &  &  &  &  & \\
        & |[point]|&  &  &  &  & |[point]| &  & \\
        &  &  &  &  &  &  &  & \\
        |[point]| &  &  &  &  &  &  &  & \\
    };
    \node[fit=(table-2-2)(table-2-4),element,label={[font=\tiny]center:label1}]{};
    \node[fit=(table-2-7)(table-2-9),element,label={[font=\tiny]center:label2}]{};
    \node[fit=(table-5-2)(table-5-3),element,label={[font=\tiny]center:label3}]{};
    \node[fit=(table-5-6)(table-5-7),element,label={[font=\tiny]center:label4}]{};
    \node[fit=(table-7-4)(table-7-5),element,label={[font=\tiny]center:label5}]{};
    \node[fit=(table-8-4)(table-8-5),element,label={[font=\tiny]center:label6}]{};

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容