\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows}
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, 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}
\begin{tikzpicture}[node distance=2cm]
\node (start) [startstop] {Start};
\node (input) [io, below of=start] {Read Input};
\node (i1) [process, below of=input] {$i = 1$};
\node (matrix) [process, below of=i1] {Initialize \\ \texttt{matrix}};
\node (matrix1) [process, below of=matrix] {Initialize \\ \texttt{matrix1}};
\node (loop) [decision, below of=matrix1, yshift=-0.5cm] {Loop Condition \\ $i > 0$};
\node (row) [io, below of=loop, yshift=-0.5cm] {Read Row};
\node (check) [decision, below of=row, yshift=-0.5cm] {Check \\ $-1$};
\node (invalid) [process, below of=check, yshift=-1cm] {Print \\ "Invalid Matrix"};
\node (pop) [process, right of=check, xshift=3.5cm] {Pop Last Row};
\node (valid) [process, below of=pop] {Append \\ to \texttt{matrix1}};
\node (transpose) [process, below of=valid] {Transpose \\ Matrix};
\node (print) [io, below of=transpose] {Print Transposed Matrix};
\node (error) [process, below of=loop, xshift=-4cm, yshift=-3cm] {Print \\ "Error"};
\node (stop) [startstop, below of=error] {Stop};
\draw [arrow] (start) -- (input);
\draw [arrow] (input) -- (i1);
\draw [arrow] (i1) -- (matrix);
\draw [arrow] (matrix) -- (matrix1);
\draw [arrow] (matrix1) -- (loop);
\draw [arrow] (loop) -- node[anchor=east] {yes} (row);
\draw [arrow] (row) -- (check);
\draw [arrow] (check) -- node[anchor=east] {no} (pop);
\draw [arrow] (check) -- node[anchor=south] {yes} (invalid);
\draw [arrow] (pop) -- (valid);
\draw [arrow] (valid) -- (transpose);
\draw [arrow] (transpose) -- (print);
\draw [arrow] (loop.west) -| node[anchor=south] {no} ++(-1.5,0) |- (stop);
\draw [arrow] (print.east) -- ++(1.5,0) |- (loop.east);
\draw [arrow] (loop.south) -- node[anchor=east] {error} (error.north);
\draw [arrow] (error) -- (stop);
\end{tikzpicture}
\end{document}
答案1
一些建议:
- 加载
positioning library
\tikzset
代替\tikzstyle
scale=.7, transform shape
below=of start
或者below=1cm of start
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning}
\tikzset{
startstop/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, align=center, draw=black, fill=red!30},
io/.style={trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, align=center, draw=black, fill=blue!30},
process/.style={rectangle, minimum width=3cm, minimum height=1cm, align=center, draw=black, fill=orange!30},
decision/.style={diamond, minimum width=3cm, minimum height=1cm, align=center, draw=black, fill=green!30},
arrow/.style={thick,->,>=stealth}
}
\begin{document}
\begin{tikzpicture}[scale=.7, transform shape, node distance=.5cm]
\node (start) [startstop] {Start};
\node (input) [io, below=of start] {Read Input};
\node (i1) [process, below=of input] {$i = 1$};
\node (matrix) [process, below=of i1] {Initialize \\ \texttt{matrix}};
\node (matrix1) [process, below=of matrix] {Initialize \\ \texttt{matrix1}};
\node (loop) [decision, below=of matrix1] {Loop Condition \\ $i > 0$};
\node (row) [io, below=1cm of loop] {Read Row};
\node (check) [decision, below=of row] {Check \\ $-1$};
\node (invalid) [process, below=1cm of check] {Print \\ "Invalid Matrix"};
\node (pop) [process, right=3cm of check] {Pop Last Row};
\node (valid) [process, below=of pop] {Append \\ to \texttt{matrix1}};
\node (transpose) [process, below=of valid] {Transpose \\ Matrix};
\node (print) [io, below=of transpose] {Print Transposed Matrix};
\node (error) [process, below left=3cm and 4cm of loop] {Print \\ "Error"};
\node (stop) [startstop, below=of error] {Stop};
\draw [arrow] (start) -- (input);
\draw [arrow] (input) -- (i1);
\draw [arrow] (i1) -- (matrix);
\draw [arrow] (matrix) -- (matrix1);
\draw [arrow] (matrix1) -- (loop);
\draw [arrow] (loop) -- node[right] {yes} (row);
\draw [arrow] (row) -- (check);
\draw [arrow] (check) -- node[above] {no} (pop);
\draw [arrow] (check) -- node[right] {yes} (invalid);
\draw [arrow] (pop) -- (valid);
\draw [arrow] (valid) -- (transpose);
\draw [arrow] (transpose) -- (print);
\draw [arrow] (loop.west) -| node[anchor=south] {no} ++(-1.5,0) |- (stop);
\draw [arrow] (print.east) -- ++(1.5,0) |- (loop.east);
\draw [arrow] (loop.south) -| node[anchor=east] {error} (error.north);
\draw [arrow] (error) -- (stop);
\end{tikzpicture}
\end{document}
答案2
- 抱歉,您的流程图应该是什么样子并不完全清楚。因此,一些箭头可能方向错误或遗漏。
- 我猜你可能会想这样:
- 对于上面的流程图我另外采用了 Ti钾Z库
chains
、、ext.paths.ortho
和positioning
。quotes
- 对于节点定位使用
chains
库。 - 分支中的连续节点通过“join
macro defined in
chains”库连接起来。 - 其他箭头之间的箭头是单独绘制的,其中使用了
ext.paths.ortho
库中定义的箭头。 - 要使用
ext.paths.ortho
库,您需要安装 ext-tikz` 包,可在加拿大运输安全局。
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,https://ctan.org/pkg/tikz-ext?lang=en
chains,
ext.paths.ortho, % defined in the tikz-ext package
% https://ctan.org/pkg/tikz-ext?lang=en
positioning,
shapes.geometric}
\makeatletter
\tikzset{suspend join/.code={\def\tikz@after@path{}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
node distance = 6 mm and 12 mm,
arr/.style = {-{Straight Barb[scale=0.8]}},
lbl/.style = {inner sep=1pt, label=below right:#1},
base/.style = {rectangle, rounded corners, draw,
text width=44mm, align=center, minimum height=1cm,
font=\sffamily},
startstop/.style = {base, rounded corners, fill=red!30},
process/.style = {base, fill=orange!30},
decision/.style = {diamond, aspect=1.3, draw, fill=green!30,
minimum width=32mm, minimum height=1cm,
inner xsep=0pt, align=center},
io/.style = {trapezium, trapezium stretches body,
trapezium left angle=70, trapezium right angle=110,
draw, fill=blue!30,
minimum width=44mm, minimum height=1cm,
text width=\pgfkeysvalueof{/pgf/minimum width}
-2*\pgfkeysvalueof{/pgf/inner xsep},
align=center}
]
\begin{scope}[start chain = going below,
nodes = {on chain, join=by arr}]
\node [startstop] {Start};
\node [io] {Read Input};
\node [process] {$i = 1$};
\node [process] {Initialize \\ \texttt{matrix}};
\node [process] {Initialize \\ \texttt{matrix1}};
\node (D1) [decision] {Loop Condition \\ $i > 0$};
%
\node [suspend join,io] {Read Row};
\node (D2) [decision] {Check \\ $-1$};
\node [process] {Print \\ "Invalid Matrix"};
\end{scope}
\begin{scope}[start chain = going below,
nodes = {on chain, join=by arr}]
\node (Pr) [process, right=of D2] {Pop Last Row};
\node [process] {Append \\ to \texttt{matrix1}};
\node [process] {Transpose \\ Matrix};
\node (IO) [io] {Print Transposed Matrix};
\end{scope}
\begin{scope}[start chain = going below,
nodes = {on chain, join=by arr}]
\node (print) [process, left=of D2] {Print \\ "Error"};
\node (stop) [startstop] {Stop};
\end{scope}
% arrows not drawn by join macro
\draw[arr] (D2.east) node[lbl=above right:No] {} -- (Pr);
\node[lbl=below right:Yes] at (D2.south) {};
%
\draw[arr] (D1.south) node[lbl=below right:Yes] {} |-| [distance=3mm] (print);
\draw[arr] (IO) r-rl (D1);
\draw[arr] (D1.west) node[lbl=above left:No ] {} r-lr (stop);
\draw[arr] (IO) r-rl (D1);
\end{tikzpicture}
\end{document}
tikzpicture
选项也可以在序言中移动文档(如果您也需要这种样式设置在其他 Ti钾Z 图片)使用于\tikzset
:
% rest of preamble
% \usetikzlibrary{...}
\tizset{
% options from `tikzpicture
}