总结:有没有办法让节点根据它们的“提示”进入空间。从而避免到处
x-scale=#cm
乱跑,让事情变得恰到好处。
我正在将一堆 visio 图表转换为 TikZ,以便对它们进行版本控制。我不断遇到的问题之一是节点间距。我通常会编写代码,然后想出类似这样的代码:(预路径)
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\usetikzlibrary{shapes, arrows, calc, positioning}
% Define block styles
\tikzstyle{state} = [ rounded rectangle, draw, text centered, minimum height=3em ]
\tikzstyle{test} = [ diamond, draw, shape aspect=2, inner sep = 0pt, text width = 7em, text centered ]
\tikzstyle{action} = [ rectangle, draw, text width=8em, inner sep = 5pt, minimum height=5em ]
\begin{tikzpicture}[node distance = 2cm, align = flush center, font = \small, xscale=5cm, on grid=true]
% Place nodes
\node [state] (a) {aLorem ipsum};
\node [test, below of=a] (b) {bLorem ipsum dolor sit};
\node [test, below of=b] (c) {cLorem ipsum dolor sit};
\node [test, below of=c] (d) {dLorem ipsum dolor sit amet, consectetur adipiscing elit.};
\node [action, below of=d] (e) {eLorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas elementum,};
\node [test, below of=e] (f) {fLorem ipsum dolor sit amet};
\node [test, right of=f] (g) {gLorem ipsum dolor};
\node [test, below of=f] (h) {hLorem ipsum dolo};
\node [action, below of=h] (i) {iLorem ipsum dolor sit amet, consectetur adipiscing elit.};
\node [action, right of=i] (j) {jLorem ipsum dolor sit amet, consectetur adipiscing elit.};
\node [action, right of=j] (k) {kLorem ipsum dolor sit};
\node [test, below of=i] (l) {lLorem ipsum dolor};
\node [test, below of=j] (m) {mLorem ipsum dolor};
\node [state, below of=l] (n) {nLorem ipsum};
\node [state, below of=m] (o) {oLorem ipsum dol};
\node [state, below right of=o] (p) {pLorem ipsum dolor sit \\ amet, consectetur};
\end{tikzpicture}
\end{document}
最终它总是看起来像垃圾:
因此,我通常会开始尝试,node distance
但这通常会使图表超出页面,并且我的垂直间距通常比水平间距更紧。我想知道是否有办法根据节点的提示将节点分隔开。从而避免到处乱七八糟,让x-scale=1cm
事情变得恰到好处。
这很重要,因为我希望将来可以轻松编辑,所以布局必须是动态的。如果 API 设计发生变化,只是添加一些文本,我不想花 20 分钟来调整它x-scale
。
我在这里看到了一些东西用矩阵绘图这看起来很有希望,但这将重写我已经完成的工作,所以我不想花更多的时间让它在我面前失败。
作为参考,这就是我尝试复制的内容:
答案1
您应该使用positioning
库的语法:不要说 ,而right of=<node>
要说right=of <node>
。此外,在这种情况下,您需要使用on grid=false
。这样,node distance
指的是节点边缘之间的距离,而不是它们的中心:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\usetikzlibrary{shapes, arrows, calc, positioning}
% Define block styles
\tikzstyle{state} = [ rounded rectangle, draw, text centered, minimum height=3em ]
\tikzstyle{test} = [ diamond, draw, shape aspect=2, inner sep = 0pt, text width = 7em, text centered ]
\tikzstyle{action} = [ rectangle, draw, text width=8em, inner sep = 5pt, minimum height=5em ]
\begin{tikzpicture}[node distance = 2em, align = flush center, font = \small, on grid=false]
% Place nodes
\node [state] (a) {aLorem ipsum};
\node [test, below=of a] (b) {bLorem ipsum dolor sit};
\node [test, below=of b] (c) {cLorem ipsum dolor sit};
\node [test, below=of c] (d) {dLorem ipsum dolor sit amet, consectetur adipiscing elit.};
\node [action, below=of d] (e) {eLorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas elementum,};
\node [test, below=of e] (f) {fLorem ipsum dolor sit amet};
\node [test, right=of f] (g) {gLorem ipsum dolor};
\node [test, below=of f] (h) {hLorem ipsum dolo};
\node [action, below=of h] (i) {iLorem ipsum dolor sit amet, consectetur adipiscing elit.};
\node [action, right=of i] (j) {jLorem ipsum dolor sit amet, consectetur adipiscing elit.};
\node [action, right=of j] (k) {kLorem ipsum dolor sit};
\node [test, below=of i] (l) {lLorem ipsum dolor};
\node [test, below=of j] (m) {mLorem ipsum dolor};
\node [state, below=of l] (n) {nLorem ipsum};
\node [state, below=of m] (o) {oLorem ipsum dol};
\node [state, below right=of o] (p) {pLorem ipsum dolor sit \\ amet, consectetur};
\end{tikzpicture}
\end{document}