我正在尝试LaTeX
使用来重现此图像tikz
(但我不确定这是否是最简单的事情)
首先,我尝试通过以下方式重现左边的树
\begin{tikzpicture}
[scale=.8,auto=left,every node/.style={ellipse,draw}]
\usetikzlibrary{shapes}
\node {ForStatement}
child {node {for}}
child {node {(}}
child {node {ForInitializer}}
child {node {;}}
child {node {ForCondition}}
child {node {;}}
child {node {ForIterator}}
child {node {)}}
child {node {EmbeddedStatement}};
\end{tikzpicture}
这就是我得到的
太糟糕了……我非常想知道如何修复这个问题,制作框架并绘制树之间的边缘,使它们不会穿过任何节点。目前我不知道该怎么办。任何帮助都将不胜感激。
答案1
您的树跨度太大,以至于landscape
使用选项可以看到整个图像。基本上您的代码是正确的。只需调整level distance
垂直位移和sibling distance
水平位移。这是我添加的以生成图像的内容。但是,树跨度太大,看不到它landscape
在这里被使用。
代码
\documentclass[landscape]{article}
\usepackage[margin=0.1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,shapes}
\begin{document}
\begin{tikzpicture}
[scale=.5,auto,every node/.style={ellipse,draw},level distance=4cm]
\tikzstyle{level 1}=[sibling distance = 55mm]
\tikzstyle{level 2}=[sibling distance = 18mm] % if level 2 is there
\node {ForStatement}
child {node {for}}
child {node {(}}
child {node {ForInitializer}}
child {node {;}}
child {node {ForCondition}}
child {node {;}}
child {node {ForIterator}}
child {node {)}}
child {node {EmbeddedStatement}};
\end{tikzpicture}
\end{document}
更新:几个小时后再次阅读 OP,此处添加了附录。
树的跨度很宽,除非缩放,否则很难将它们并排放置,但这样会失去清晰度。因此,为了便于说明,将两幅图像垂直放置。当然,一旦理解了代码,OP 就可以改变方向。基本上,第二个 2 级树的绘制与上面的第一个类似。为了连接节点,给出了内部名称,edge[in=xx, out=xx]
其中使用 xx=angle,并且它们在代码中最后列出。
代码
\documentclass[landscape]{article}
\usepackage[margin=0.1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,shapes}
\begin{document}
\begin{tikzpicture}
[scale=.45,auto,every node/.style={ellipse,draw},level distance=4cm,remember picture]
\tikzstyle{level 1}=[sibling distance = 62mm]
\tikzstyle{level 2}=[sibling distance = 18mm]
\node {ForStatement}
child {node {for}}
child {node {(}}
child {node (lizer){ForInitializer}}
child {node {;}}
child {node (fc) {ForCondition}}
child {node {;}}
child {node (tor){ForIterator}}
child {node {)}}
child {node (se){EmbeddedStatement}};
\end{tikzpicture}
% --- newly added for the second two-level tree.
\begin{tikzpicture}
[scale=.5,auto,every node/.style={ellipse,draw},level distance=4cm,shift={(23.2cm,-1cm)},remember picture, overlay]
\tikzstyle{level 1}=[sibling distance = 55mm]
\tikzstyle{level 2}=[sibling distance = 40mm]
\node {Statement}
child {node {for}}
child {node {(}}
child {node {ForControl}
child {node (fu){ForUpdate}}
child {node {;}}
child {node (e){Expression}}
child {node {;}}
child {node (fi){ForInit}}}
child {node {)}}
child {node (s) {Statement}};
\draw[color=blue,<->] (fu) edge[out=180, in=-90] (lizer);
\draw[color=blue,<->] (fi) edge[out=0, in=-90] (tor);
\draw[color=blue,<->] (s) edge[out=0, in=-90] (se);
\draw[color=blue,<->] (e) edge[out=0, in=0] (fc);
\end{tikzpicture}
\end{document}
答案2
Forest
是另一个有趣的树绘制包。它基于TiKZ
并提供非常简单的语法。它计算节点之间的所有距离。下一个代码可能是您方案的起点。
\documentclass[tikz,border=2mm]{standalone}
\usepackage{forest}
\usetikzlibrary{shapes}
\begin{document}
\begin{forest}
for tree={child anchor=north, ellipse, draw}
[, phantom, s sep = 1cm
[ForStatement
[for]
[(]
[ForInitializer]
[{;}]
[ForCondition]
[{;}]
[ForIterator]
[)]
[EmbededStatement,name=A]
]
[Statement, name=C
[for]
[(]
[ForControl
[ForInit]
[{;}]
[Expression]
[{;}]
[ForUpdate]]
[)]
[Statement,name=B]
]
]
\draw[blue,rounded corners,<->] (A) |- ([yshift=3mm]C.north) -| (B);
\end{forest}
\end{document}