矩阵节点内子节点的奇怪行为

矩阵节点内子节点的奇怪行为

子节点在矩阵节点中表现得很奇怪。而且它只出现在一个矩阵节点中。我知道在这个简单的情况下有方法可以在没有矩阵节点的情况下绘制它。但本质上我需要绘制偏序集的哈斯图,其中每个元素都是一棵树。所以如果保留矩阵节点就好了。无论如何,奇怪的行为也很有趣。也可以检查一下子节点奇怪地向右倾斜发现使用嵌套的 tikzpicture 时也会出现这种情况。建议针对矩阵节点的情况提出一个新问题。

两棵树相邻

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
    [level distance = 10mm]

    \node [matrix, label=left:{$T_1$}] (T1)
    {
            \begin{scope}
                [every node/.style={draw, circle, inner sep=1pt, minimum size = 1mm}]
                \node {}
                child {node {} child {node {}}}
                child {node {} child {node {}}}
                child {node {} child {node {}}};
            \end{scope}\\
        };

    \node [matrix, right = of T1, label=left:{$T_2$}] (T2)
    {
            \begin{scope}
                [every node/.style={draw, circle, inner sep=1pt, minimum size = 1mm}]
                \node {}
                child {node {} child {node {}}}
                child {node {} child {node {}}}
                child {node {} child {node {}}};
            \end{scope}\\
        };

\end{tikzpicture}
\end{document}

答案1

positioningright = of …隐式设置了anchor = west。不幸的是,这会传递给矩阵内的节点。

我们可以positioning为矩阵创建额外的键来使用matrix anchor而不是anchor但在这种情况下,最简单的解决方案是明确设置矩阵所有节点的anchor = center(默认值)或centered(的别名)。anchor = center

TikZ 的树语法非常简单,并且不会自动设置任何锚点。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
  level distance = 10mm,
  every matrix/.style={
    nodes={draw, circle, inner sep=1pt, minimum size = 1mm, centered}}]
\node [matrix, label=left:{$T_1$}] (T1){
  \node {}
    child {node {} child {node {}}}
    child {node {} child {node {}}}
    child {node {} child {node {}}};\\};
\node [matrix, right = of T1, label=left:{$T_2$}] (T2){
  \node {}
    child {node {} child {node {}}}
    child {node {} child {node {}}}
    child {node {} child {node {}}};\\};
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案2

我发现问题似乎与有关positioning。有以下解决方法。

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}
    [level distance = 10mm]

    \node [matrix, label=left:{$T_1$}] (T1)
    {
            \begin{scope}
                [every node/.style={draw, circle, inner sep=1pt, minimum size = 1mm}]
                \node [grow=down] {}
                child {node {} child {node {}}}
                child {node {} child {node {}}}
                child {node {} child {node {}}};
            \end{scope}\\
        };

    \node [matrix, label=left:{$T_2$}, matrix anchor=west] at ($(T1.east) + (10mm,0)$) (T2)
    {
            \begin{scope}
                [every node/.style={draw, circle, inner sep=1pt, minimum size = 1mm}]
                \node {}
                child {node {} child {node {}}}
                child {node {} child {node {}}}
                child {node {} child {node {}}};
            \end{scope}\\
        };

\end{tikzpicture}
\end{document}

相关内容