我想使用graph
CVS 版本的 TikZ 库(源码或者tlcontrib) 绘制流程图。类似于 TikZ 手册第 5 章中所示的流程图。在本教程中,声明了一些样式,以便轻松绘制节点之间的环和角。我想为这些线添加一些标签,但edge label
选项不适用于这些类型的边缘。它似乎适用于直线路径,但不适用于复杂路径。
下一个示例显示了该问题:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,graphs,matrix}
\begin{document}
\begin{tikzpicture}[>=stealth',
font=\sffamily\small,
every node/.style={align=center},
skip loop left/.style={to path={-- ++(-#1,0)|- (\tikztotarget)}},
skip loop right/.style={to path={-- ++(#1,0)|- (\tikztotarget)}},
hv path/.style ={to path={-| (\tikztotarget)}},
vh path/.style ={to path={|- (\tikztotarget)}},
]
\matrix[column sep=1cm,row sep=1cm] (mymatrix) {
\coordinate (A11) {}; & \node[draw] (A12) {A12};\\
\node[draw] (A21) {A21}; & \coordinate (A22) {};\\
};
\graph[use existing nodes] {
A21->[edge label=A]A12;
A21->[hv path, edge label=B] A12; %<- this label is not shown
A21--[edge label=C]A11->A12;
};
\end{tikzpicture}
\end{document}
结果显示标签 A 和 C,但没有显示 B。显示标签 C 是因为边缘被分成了两个直边。我总是可以使用类似的东西,但你知道更好的解决方案吗?
答案1
graphs
您的问题与库无关。sto path
缺少节点。您需要\tikztonodes
在要添加节点的路径操作符后添加目标坐标。
hv path/.style ={to path={-| (\tikztotarget) \tikztonodes}},
vh path/.style ={to path={|- (\tikztotarget) \tikztonodes}},
我还切换到arrows.meta
图书馆(stealth
变成Stealth
),因为图书馆现在arrows
已经arrows.spaced
被弃用(但当然仍然可用)。
虽然似乎引入了一个新的错误,可以通过
A21 -- [edge label=C] A11 -> A12;
A21
因为从 到的边A11
即使<-
在该图的任何地方使用也会得到箭头提示。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta,graphs}
\begin{document}
\begin{tikzpicture}[
>=Stealth,
font=\sffamily\small,
every node/.style={align=center},
hv path/.style ={to path={-| (\tikztotarget) \tikztonodes}},
vh path/.style ={to path={|- (\tikztotarget) \tikztonodes}},
]
\matrix[column sep=1cm,row sep=1cm] (mymatrix) {
\coordinate (A11); & \node[draw] (A12) {A12}; \\
\node[draw] (A21) {A21}; & \coordinate (A22); \\
};
\graph[use existing nodes] {
A21 -> [edge label=A]A12;
A21 -> [hv path, near end, edge label'=B] A12;
A21 -- [edge label=C] A11 -> A12;
};
\end{tikzpicture}
\end{document}
输出
答案2
如果您只想创建流程图,那么使用纯 tikz 而不使用库会容易得多。以下是来自 tikz 示例站点的一个示例(略有修改)。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\begin{document}
\tikzstyle{decision} = [diamond, draw, fill=gray!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=gray!15,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{altblock} = [rectangle, draw, fill=gray!15,
text width=5em, text centered, rounded corners=6pt, minimum height=2em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=gray!10, node distance=3cm,
minimum height=2em]
\begin{tikzpicture}[node distance = 3cm, auto,scale=0.75,transform shape]
% Place nodes
\node [altblock] (init) {initialize};
\node [cloud, left of=init] (expert) {expert};
\node [cloud, right of=init] (system) {system};
\node [decision, below of=init] (identify) {Was successful?};
\node [block, below of=identify] (evaluate) {evaluate candidate models};
\node [block, left of=evaluate, node distance=3cm] (update) {update model};
\node [decision, below of=evaluate] (decide) {is best candidate better?};
\node [block, below of=decide, node distance=3cm] (stop) {stop};
% Draw edges
\path [line] (init) -- (identify);
\path [line] (identify) -- node[auto] {yes} (evaluate);
\path [line] (identify) -| node[auto,right] {no} (system);
\path [line] (evaluate) -- (decide);
\path [line] (decide) -| node [near start] {yes} (update);
\path [line] (update) |- (identify);
\path [line] (decide) -- node {no}(stop);
\path [line,dashed] (expert) -- (init);
\path [line,dashed] (system) -- (init);
%\path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}
\end{document}
输出结果如下: