我在使用 TikZ 时遇到了一些问题,我有一个示例,我想在块的右侧和左侧添加Yes
/箭头。到目前为止,箭头显示在块上方,而且间距太窄。我将不胜感激任何帮助。No
Finished?
平均能量损失
% vim:ft=tex:
%
\documentclass[12pt]{article}
% UML diagram generation
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes, positioning}
% \usetikzlibrary{fit, backgrounds, matrix, arrows.meta} % Optional widgets for background
\usepackage{caption}
% Define drawing set
\tikzset{%
>={Latex[width=2mm,length=2mm]},
% Specifications for style of nodes:
base/.style = {rectangle, rounded corners, draw=black, minimum width=4cm, minimum height=1cm, text centered, font=\sffamily},
activityStarts/.style = {base, fill=blue!30},
startstop/.style = {base, fill=red!30},
activityRuns/.style = {base, fill=green!30},
process/.style = {base, minimum width=2.5cm, fill=orange!15, font=\ttfamily},
decision/.style = {diamond, rounded corners, draw=black, fill=green!10, text centered, font=\sffamily}
}
\begin{document}
\begin{tikzpicture}[node distance=1.5cm, every node/.style={fill=white, font=\sffamily}, align=center]
\node (start) [activityStarts] {Application starts};
\node (onTextInput) [process, below of=start] {procedureA()};
\node (computeStuff) [process, below of=onTextInput] {procedureB};
\node (runningProcess) [activityRuns, below of=computeStuff] {Compute x};
\node (decideStuff) [decision, below=0.5cm of runningProcess] {Finished?};
\node (createOutput) [process, below of=decideStuff] {addd};
\draw[->] (start) -- (onTextInput);
\draw[->] (onTextInput) -- (computeStuff);
\draw[->] (computeStuff) -- (runningProcess);
\draw[->] (runningProcess) -- (decideStuff);
\draw[->] (decideStuff) |- node {No} (runningProcess);
\draw[->] (runningProcess) -- (createOutput);
\end{tikzpicture}
\end{document}
编译
latexmk -pdf -xelatex tikz-example.tex
解释和期望结果
答案1
我不会below=of
与below of=
语法混淆,即避免后者。在这里可以使用chains
(which loads positioning
) 来避免不必要的重复。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, chains,shapes.geometric}
% Define drawing set
\tikzset{%
>={Latex[width=2mm,length=2mm]},
% Specifications for style of nodes:
base/.style = {rectangle, rounded corners, draw=black, minimum width=4cm, minimum height=1cm, text centered, font=\sffamily},
activityStarts/.style = {base, fill=blue!30},
startstop/.style = {base, fill=red!30},
activityRuns/.style = {base, fill=green!30},
process/.style = {base, minimum width=2.5cm, fill=orange!15, font=\ttfamily},
decision/.style = {diamond, rounded corners, draw=black, fill=green!10, text centered, font=\sffamily}
}
\begin{document}
\begin{tikzpicture}[node distance=0.5cm]
\begin{scope}[start chain=going below,
nodes={fill=white, font=\sffamily, align=center,on chain,join=by ->}]
\node (start) [activityStarts] {Application starts};
\node (onTextInput) [process] {procedureA()};
\node (computeStuff) [process] {procedureB};
\node (runningProcess) [activityRuns] {Compute x};
\node (decideStuff) [decision, below] {Finished?};
\end{scope}
\node (createOutput) [process,below=of decideStuff] {add};
\draw[->,rounded corners=2pt] (decideStuff) -- ++ (3,0) |- node[pos=0.25,right] {No} (runningProcess);
\draw[->,rounded corners=2pt] (decideStuff) -- ++ (-3,0) |- node[pos=0.25,left] {Yes} (createOutput);
\end{tikzpicture}
\end{document}