使用經過測試的 tikz 圖進行佈局

使用經過測試的 tikz 圖進行佈局

我使用的tree layout是 tikz,它通常運作得很好,但有時,當使用子圖時,我最終會得到彼此完美對齊的邊緣,如下所示:

在此輸入影像描述

在下面的程式碼中,我還嘗試了各種其他佈局方法,例如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}

在此輸入影像描述

相關內容