是否可以使用相对定位(例如below of
)指定节点之间的距离,以免发生这种情况:
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{shapes.multipart}
\usepackage{flowchart}
\usetikzlibrary{positioning}
\begin{document}
\pagestyle{empty}
\tikzstyle{decision_lb} = [diamond, draw, fill = blue!20,
text badly centered, node distance=3cm, text width = ]
% Operation
\tikzstyle{c_op} = [rectangle, draw, fill = blue!20,
text centered, minimum height=4em, text width=,minimum size=2em]
\begin{tikzpicture}[node distance = 1.5cm, auto]
\node[decision_lb](dec1){veeeery looooooooooooooooonnnnnnnnnnnnnnnnnggggggggggggggg text};
%yes branch
\node[c_op, below of = dec1] (op4) {$formulas$};
\end{tikzpicture}
\end{document}
当然,这是一个夸张的例子,但我在更复杂的图形中也遇到了同样的问题。
我需要一些类似的命令,below of
但它必须包含前一个形状的外部尺寸。我使用的是 no yshift
,但需要反复试验才能找到合适的值以使其看起来不错。到目前为止,我还没有找到类似的问题。
编辑:我想放置op4
在与第二个示例相同的距离,而不管写的是什么dec1
在与第二个示例相同的距离。
第二个例子:
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{shapes.multipart}
\usepackage{flowchart}
\usetikzlibrary{positioning}
\begin{document}
\pagestyle{empty}
\tikzstyle{decision_lb} = [diamond, draw, fill = blue!20,
text badly centered, node distance=3cm, text width = ]
% Operation
\tikzstyle{c_op} = [rectangle, draw, fill = blue!20,
text centered, minimum height=4em, text width=,minimum size=2em]
\begin{tikzpicture}[node distance = 1.5cm, auto]
\node[c_op](dec1){veeeery looooooooooooooooonnnnnnnnnnnnnnnnnggggggggggggggg text};
%yes branch
\node[c_op, below of = dec1] (op4) {$formulas$};
\end{tikzpicture}
\end{document}
编辑 2:我希望我的形状之间有相同的垂直距离。我的问题是below of = dec1
只指定节点原点之间的距离。如果菱形形状中的文本变得很长,菱形也会在垂直方向上变得很大,但到下一个形状的距离不会改变(因为节点原点保持不变)。所以我需要一个命令来指定到另一个形状外部的距离。
答案1
我猜你想获得这样的东西:
如果不是这样,请澄清您的问题。
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, positioning, shapes}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 11mm and 22mm,
decision_lb/.style = {diamond, draw, fill = blue!20,
text width=4.5em, align=flush center},
c_op/.style = {rectangle, draw, fill = blue!20,
text width=5em, align=center, minimum height=4em}
]
\node (dec1) [decision_lb] {very long text};
%yes branch
\node (op4) [c_op, below=of dec1] {$formulas$};
\end{tikzpicture}
\end{document}
在上面姆韦我执行以下操作:
\tikzstyle
用选项替换折旧tikzpicture
。您还可以将这些选项移动到\tikzset{...}
- 清理重复的样式定义
text width
...... - 降低
inner sep
为默认值 - 使用包定义的语法
positioning
:below=of ...
而不是below of= ...
附录(1):告诉我,如果我现在理解正确的话:您希望下一个节点的宽度与前一个节点的宽度相同,即:菱形下方的矩形的宽度应与菱形的宽度相同?
如果我的猜测(这次)正确,那么可能的解决方案之一是:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, calc, positioning, shapes}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 11mm and 22mm,
decision_lb/.style = {diamond, draw, fill = blue!20,
align=flush center},
c_op/.style = {rectangle, draw, fill = blue!20,
text width=#1 - 2*\pgfkeysvalueof{/pgf/inner xsep},
align=center, minimum size=4em},
]
\node (dec1) [decision_lb] {very long text};
%yes branch
\path let \p1 = ($(dec1.west)-(dec1.east)$),
\n1 = {veclen(\y1,\x1)} in
node (op4) [c_op=\n1, below=of dec1] {$formulas$};
\end{tikzpicture}
\end{document}
这使:
附录(2):如下来自问题的上次编辑,你的问题是使用错误的语法对于节点距离。正确的是参见上面列表中的最后一项。让我重复一遍:
错误的: below of = dec1
正确的: below = of dec1
为了证明这一点,让我将第一个答案扩展到更多节点:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, calc, positioning, shapes}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 4mm and 8mm,
decision_lb/.style = {diamond, draw, fill = blue!20,
text width=4.5em, align=flush center},
c_op/.style = {rectangle, draw, fill = blue!20,
minimum width=5em,
align=center, minimum height=4em},
]
\node (dec1) [decision_lb] {very long text};
%yes branch
\node (op4) [c_op, below=of dec1] {$formulas$}; % <-- observe syntax for distance between nodes
\node (op5) [c_op, below=of op4] {$formulas$}; % <-- observe syntax for distance between nodes
\end{tikzpicture}
\end{document}
如您所见,节点之间的距离相等。
答案2
您还可以使用matrix
并让其tikz
为您工作:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, calc, positioning, shapes, matrix}
\begin{document}
\begin{tikzpicture}[
decision_lb/.style = {diamond, draw, fill = blue!20,
text width=4.5em, align=flush center},
c_op/.style = {rectangle, draw, fill = blue!20,
minimum width=5em,
align=center, minimum height=4em},
]
\matrix[matrix of nodes,
row sep=1.5cm, column sep=1.5cm]{
|[decision_lb, name=dec1]| {very long text}
& |[c_op, name= dec2]| {veeeery
looooooooooooooooonnnnnnnnnnnnnnnnnggggggggggggggg text}\\
|[c_op, name= op4]| {$formulas$}
& |[c_op, name= op5]| {$formulas$} \\
};
\end{tikzpicture}
\end{document}