我一直在尝试使用 tikz 绘制流程图。我借助 overleaf 指南来定义元素。使用 TikZ 制作 Overleaf LaTeX 图形:初学者教程(第 3 部分)— 创建流程图 在我面对一个复杂的图表,需要从决策块后退几步之前,这种方法一直运行良好。我使用的代码:
\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{xcolor}
\usepackage[colorlinks=true,urlcolor=blue,citecolor=blue]{hyperref}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\tikzstyle{io} = [trapezium, trapezium left angle=80, trapezium right angle=100, minimum width=1cm, minimum height=1cm, text centered, draw=black, fill=blue!30]
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{document}
\section{Example of a Flowchart}
%
\begin{figure}[h]
\centering
\begin{tikzpicture}[node distance=2cm]
\node (W) [io]{Some Matrix A};
\node (C) [io, right of=W, xshift=6cm]{Some Matrix B};
\node (Comp3) [process, below of=W]{Compute this};
\node (Comp4) [process, below of=C]{Compute that};
\node (Proc) [process, below of=Comp3, xshift=4cm]{Compare result A with result B sequentially};
\node (Match) [decision, below of=Proc, yshift=-0.5cm] {Match found ?};
\node (Comp5) [process, below of=Match,yshift=-0.5cm]{Found this match};
\draw [arrow] (W) -- (Comp3);
\draw [arrow] (C) -- (Comp4);
\draw [arrow] (Comp3) |- (Proc);
\draw [arrow] (Comp4) |- (Proc);
\draw [arrow] (Proc) -- (Match);
\draw [arrow] (Match) -- node[anchor=east] {Yes} (Comp5);
\draw [arrow] (Match) -| node[anchor=north] {No} (Comp4);
\end{tikzpicture}
\end{figure}
\end{document}
结果,两个箭头合并了。我想在它们之间创建一些水平空间。有什么方法可以引入空间或调整这些箭头的起点和终点吗?或者制作弯曲的箭头,而不是直线?
答案1
您在某处找到的代码是非常古老的代码,是用 语法编写的TikZ version 2
。随着 的出现TikZ 3.0
,代码已经发展,并且使管理节点的相对定位。为此,只需加载positioning
库。
例如,旧语法为 ,而below of=W
新语法为below=of W
。您会注意到 和=
是of
反过来写的。
使用这种新语法,我将节点的放置顺序与代码中的顺序不同。我更新了styles
也是用旧语法编写的语法。
翻译www.DeepL.com/Translator(免费版)
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{positioning}
\tikzset{io/.style = {trapezium, trapezium left angle=80, trapezium right angle=100, minimum width=1cm, minimum height=1cm, text centered, draw=black, fill=blue!30},
process/.style = {rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30},
decision/.style={diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30},
every arrow/.style={thick,>=stealth}}
\begin{document}
\begin{tikzpicture}%[node distance=1cm]
\node (W) [io]{Some Matrix A};
\node (Comp3) [process, below =of W]{Compute this};
\node (Proc) [process, below right=of Comp3]{Compare result A with result B sequentially};
\node (Comp4) [process, above right =of Proc]{Compute that};
\node (C) [io, above =of Comp4]{Some Matrix B};
\node (Match) [decision, below =of Proc] {Match found ?};
\node (Comp5) [process, below =of Match]{Found this match};
\draw [->] (W) -- (Comp3);
\draw [->] (C) -- (Comp4);
\draw [->] (Comp3) |- (Proc);
\draw [->] (Comp4.230) |- (Proc);
\draw [->] (Proc) -- (Match);
\draw [->] (Match) -- node[anchor=east] {Yes} (Comp5);
\draw [->] (Match) -| node[anchor=north] {No} (Comp4.310);
\end{tikzpicture}
\end{document}