1

I'm stuck a little with TikZ, I've this example where I would like to add Yes /No arrows on the right and left sides of the Finished? block. So far, the arrows show over the block also the spacing is too narrow. I'll be grateful for any help.

MWE

% 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}

Compile

latexmk -pdf -xelatex tikz-example.tex

Explanation and desired outcome

Output with desired arrows marked

1 Answer 1

4

I would not mix below=of with below of= syntax, i.e. avoid the latter. And one can use chains (which loads positioning) here to avoid unnecessary repetition.

\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}

enter image description here

2
  • Great, answer, thank you. Could you kindly explain what's the role of -- ++ syntax?
    – Konrad
    Nov 26, 2021 at 22:58
  • 2
    @Konrad -- ++ (x,y) means "draw a straight line to the current position (in the path) + (x,y)". In this very case one could also use -- + (x,y) because this is also the start position of the path plus (x,y).
    – user255043
    Nov 26, 2021 at 23:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .