我想改进以下代码:我的问题在屏幕截图中有所解释。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc}
\begin{document}
\pagestyle{empty}
% Source: http://www.texample.net/tikz/examples/simple-flow-chart/
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=green!20,
text width=5.5em, text badly centered, node distance=2.5cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5.5em, text centered, node distance=2.5cm, rounded corners, minimum height=2em]
\tikzstyle{loop} = [rectangle, draw, fill=green!20,
text width=5.5em, text centered, node distance=2.5cm, rounded corners, minimum height=2em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=2.5cm,
text width=6.5em, text centered, minimum height=2em]
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [cloud] (input) {$(n ; x) \in N \times R$};
\node [block, below of=input] (affect1) {$u \leftarrow x$};
\node [loop, below of=affect1] (whileinit) {$i \leftarrow 1$};
\node [decision, below of=whileinit] (whiletest) {$i \leq n$};
\node [block, below of=whiletest] (affect2) {$u \leftarrow A_1(u)$};
\node [loop, left of=affect2] (whilenext) {$i \leftarrow i + 1$};
\node [cloud, below of=affect2] (output) {$u$};
\path[line] (input) -- (affect1);
\path[line] (affect1) -- (whileinit);
\path[line] (whileinit) -- (whiletest);
\path[line] (whiletest) -- node {oui} (affect2);
\path[line] (affect2) -- (whilenext);
\path[line] (whilenext) |- (whiletest);
\path[line] (whiletest.east) -| node {non} ($(whiletest)!.7!(output.east)$) -| (output.north);% (output);
\end{tikzpicture}
\end{document}
答案1
您的问题可以通过以下方式解决
- 加载和使用
positioning
库和 - 改变
non
路径。
第一点意味着例如below of=
需要去below=<distance> of
。距离可以省略,在这种情况下node distance
使用值。至于第二点,我使用了
\path[line] (whiletest.east) -- ++ (2cm,0) node[right] {non} |-
([yshift=-5mm]affect2.south) -- (output.north);
其中2cm
控制距离,您可以根据自己的喜好进行调整。此外,我\tikzstyle
用相应的\tikzset
语法替换了它。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc,positioning}
\begin{document}
\pagestyle{empty}
% Source: http://www.texample.net/tikz/examples/simple-flow-chart/
% Define block styles
\tikzset{decision/.style={diamond, draw, fill=green!20,
text width=5.5em, text badly centered, node distance=2.5cm, inner sep=0pt},
block/.style={rectangle, draw, fill=blue!20,
text width=5.5em, text centered, node distance=2.5cm, rounded corners, minimum height=2em},
loop/.style={rectangle, draw, fill=green!20,
text width=5.5em, text centered, node distance=2.5cm, rounded corners, minimum height=2em},
line/.style={draw, -latex'},
cloud/.style={draw, ellipse,fill=red!20, node distance=2.5cm,
text width=6.5em, text centered, minimum height=2em}}
\begin{tikzpicture}[node distance = 1cm and 0.8cm, auto]
% Place nodes
\node [cloud] (input) {$(n ; x) \in N \times R$};
\node [block, below=of input] (affect1) {$u \leftarrow x$};
\node [loop, below=of affect1] (whileinit) {$i \leftarrow 1$};
\node [decision, below=of whileinit] (whiletest) {$i \leq n$};
\node [block, below=of whiletest] (affect2) {$u \leftarrow A_1(u)$};
\node [loop, left=of affect2] (whilenext) {$i \leftarrow i + 1$};
\node [cloud, below=of affect2] (output) {$u$};
\path[line] (input) -- (affect1);
\path[line] (affect1) -- (whileinit);
\path[line] (whileinit) -- (whiletest);
\path[line] (whiletest) -- node {oui} (affect2);
\path[line] (affect2) -- (whilenext);
\path[line] (whilenext) |- (whiletest);
\path[line] (whiletest.east) -- ++ (2cm,0) node[right] {non} |-
([yshift=-5mm]affect2.south) -- (output.north);% (output);
\end{tikzpicture}
\end{document}
答案2
这里有一种方法:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc}
\begin{document}
\pagestyle{empty}
% Source: http://www.texample.net/tikz/examples/simple-flow-chart/
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=green!20,
text width=5.5em, text badly centered, node distance=2.5cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5.5em, text centered, node distance=2.5cm, rounded corners, minimum height=2em]
\tikzstyle{loop} = [rectangle, draw, fill=green!20,
text width=5.5em, text centered, node distance=2.5cm, rounded corners, minimum height=2em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=2.5cm,
text width=6.5em, text centered, minimum height=2em]
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [cloud] (input) {$(n ; x) \in N \times R$};
\node [block, below of=input] (affect1) {$u \leftarrow x$};
\node [loop, below of=affect1] (whileinit) {$i \leftarrow 1$};
\node [decision, below of=whileinit] (whiletest) {$i \leq n$};
\node [block, below of=whiletest] (affect2) {$u \leftarrow A_1(u)$};
\node [loop,left] at ($(affect2)+(-3,0)$) (whilenext) {$i \leftarrow i + 1$};
\node [cloud, below of=affect2] (output) {$u$};
\path[line] (input) -- (affect1);
\path[line] (affect1) -- (whileinit);
\path[line] (whileinit) -- (whiletest);
\path[line] (whiletest) -- node {oui} (affect2);
\path[line] (affect2) -- (whilenext);
\path[line] (whilenext) |- (whiletest);
\path[line] (whiletest.east) -| node {non} ($(whiletest)!.7!($(output.east)+(3,0)$)$) -| (output.north);% (output);
\end{tikzpicture}
\end{document}
(-3,0)
我只是使用了你的代码,并通过在你给定的位置添加或改进了请求的内容(3,0)
。我认为这很容易学习,并且可以为你提供更大的灵活性,而不是以我的方式从头开始创建代码,而不是只向你介绍这个简单的东西。