使用经过测试的 tikz 图进行布局

使用经过测试的 tikz 图进行布局

我正在使用tree layouttikz,它通常工作得很好,但有时,当使用子图时,我最终会得到完美对齐的边缘,如下所示:

在此处输入图片描述

在下面的代码中,我还尝试了其他各种布局方法,例如spring layout和电动变体。但它们似乎同样存在嵌套布局问题,并且力无法很好地在各个层级间传播。这导致一堆节点相互叠加。

我需要使用什么技巧来使布局与嵌套布局兼容?

\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing, quotes, arrows.meta}
\usegdlibrary{trees}
\begin{document}
\tikz[
    every node/.style={
        font=\scriptsize,
        inner sep=2pt,
    },
    identity/.style={circle, draw=black, fill=white, inner sep=0pt, minimum size=4pt},
    var/.style={circle, draw=black, fill=white, inner sep=2pt},
    function/.style={circle, draw=black, fill=white, inner sep=2pt},
    subgraph nodes={draw=gray, rounded corners},
    subgraph text none,
]
\graph [
    tree layout,
    grow'=right,
    fresh nodes,
    sibling sep=5em,
    node distance=1cm,
    node sep=1cm,
    nodes behind edges,
    nodes={align=center},
] {
    main+cluster [draw=black, circle] // [tree layout]{
        sub+cluster // [tree layout]{
            node+A[function,as=$A$];
            node+x1[var,as=$x$];
            (node+x1) -> [-latex, "$i$"] (node+A);
        },
        node+x2[var,as=$x$];
        (node+A) -- ["$i$"] (node+x2);
    },
    node+j[as=];
    (node+A) -- ["$j$"] (node+j);
    node+i+prime[as=];
    (main+cluster) -- [Circle-, "$i'$"] (node+i+prime);
};
\end{document}

答案1

这是建议,我用 将x节点向下移动nudge down =5mm,并用 弯曲箭头out=-80,in=230,looseness=1.4。您可以更改这些设置,以尽可能接近您的想法。

node+x2[var,nudge down=5mm,as=$x$];
(node+A) -- [out=-80,in=230,looseness=1.4,"$i$"] (node+x2);

完整代码

\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing, quotes, arrows.meta}
\usegdlibrary{trees}
\begin{document}
\tikz[
    every node/.style={
        font=\scriptsize,
        inner sep=2pt,
    },
    identity/.style={circle, draw=black, fill=white, inner sep=0pt, minimum size=4pt},
    var/.style={circle, draw=black, fill=white, inner sep=2pt},
    function/.style={circle, draw=black, fill=white, inner sep=2pt},
    subgraph nodes={draw=gray, rounded corners},
    subgraph text none,
]
\graph [
    tree layout,
    grow'=right,
    fresh nodes,
    sibling sep=5em,
    node distance=1cm,
    node sep=1cm,
    nodes behind edges,
    nodes={align=center},
] {
    main+cluster [draw=black, circle] // [tree layout]{
        sub+cluster // [tree layout]{
            node+A[function,as=$A$];
            node+x1[var,as=$x$];
            (node+x1) -> [-latex, "$i$"] (node+A);
        },
        node+x2[var,nudge down=5mm,as=$x$];
        (node+A) -- [out=-80,in=230,looseness=1.4,"$i$"] (node+x2);
    },
    node+j[as=];
    (node+A) -- ["$j$"] (node+j);
    node+i+prime[as=];
    (main+cluster) -- [Circle-, "$i'$"] (node+i+prime);
};
\end{document}

在此处输入图片描述

相关内容