2

I can't seem to get the text centered between the two right angles along the path.

MWE

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture} [node distance = 2cm, state/.style={draw, rectangle, minimum width=2cm, align=center}, >={latex}, arrow text/.style={text width=1.8cm, rectangle, align=center, font={\tiny}, node distance=1cm}, tmp/.style={coordinate}]

% Main blocks
\node [state]           (init)  {Initialization\\State};
\node [state, right=of init]    (nonrt) {Non-Real-Time\\State};
\node [state, right=of nonrt]   (rt)    {Real-Time\\State};
\node [state, left=of init]     (off)   {Off\\State};

% Tmporary nodes for routing arrows
\node [tmp, below=of nonrt, node distance=.5cm] (tmpbelow) {};

% Arrows
\draw [->] (nonrt) -- node [arrow text, anchor=north] {text below} node [arrow text, anchor=south] {text above} (rt);
\draw [->] ($ 0.5*(nonrt.south west)+0.5*(nonrt.south) $) |- ++(0cm,-.5cm) -| node [arrow text, midway, below] {text below} ($ 0.5*(init.south) + 0.5*(init.south east) $);

\end{tikzpicture}

\end{document}

MWE output

enter image description here

Expected output

text below is centered along the arrow path between the two right angles.

enter image description here

3 Answers3

4

Use pos=0.25 instead of midway.

pos=0.25

\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture} [node distance = 2cm, state/.style={draw, rectangle, minimum width=2cm, align=center}, >={latex}, arrow text/.style={text width=1.8cm, rectangle, align=center, font={\tiny}, node distance=1cm}, tmp/.style={coordinate}]

% Main blocks
\node [state]           (init)  {Initialization\\State};
\node [state, right=of init]    (nonrt) {Non-Real-Time\\State};
\node [state, right=of nonrt]   (rt)    {Real-Time\\State};
\node [state, left=of init]     (off)   {Off\\State};

% Tmporary nodes for routing arrows
\node [tmp, below=of nonrt, node distance=.5cm] (tmpbelow) {};

% Arrows
\draw [->] (nonrt) -- node [arrow text, anchor=north] {text below} node [arrow text, anchor=south] {text above} (rt);
\draw [->] ($ 0.5*(nonrt.south west)+0.5*(nonrt.south) $) |- ++(0cm,-.5cm) -| node [arrow text, pos=0.25, below] {text below} ($ 0.5*(init.south) + 0.5*(init.south east) $);

\end{tikzpicture}

\end{document}

Explanation

When you use the |- option for a path, the point at the angle is considered at midway of the entire path. So the midway of the first line is at pos=0.25, as the midway of the second line is at pos=0.75.

SebGlav
  • 19,186
  • 2
    +1 I did not know the behaviour of midway with |-. If one wants to avoid pos=0.25, one could use near start. See https://tex.stackexchange.com/a/29713/8650 – hpekristiansen Sep 07 '21 at 20:38
0

The answer by SebGlav is admittedly the easiest solution to your issue. However, for information purposes I will propose an alternate solution:

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture} [node distance = 2cm, state/.style={draw, rectangle, minimum width=2cm, align=center}, >={latex}, arrow text/.style={text width=1.8cm, rectangle, align=center, font={\tiny}, node distance=1cm}, tmp/.style={coordinate}]

    % Main blocks
    \node [state]           (init)  {Initialization\\State};
    \node [state, right=of init]    (nonrt) {Non-Real-Time\\State};
    \node [state, right=of nonrt]   (rt)    {Real-Time\\State};
    \node [state, left=of init]     (off)   {Off\\State};

    % Tmporary nodes for routing arrows
    \node [tmp, below=of nonrt, node distance=.5cm] (tmpbelow) {};

    % Arrows
    \draw [->] (nonrt) -- node [arrow text, anchor=north] {text below} node [arrow text, anchor=south] {text above} (rt);
    \coordinate (X) at ($(nonrt.210)+(0,-.5cm)$);
    \draw[->]  (nonrt.210) -- (X) -- (X -| init.320) node [arrow text, midway, below] {text below} -- ++(0,.5cm);
\end{tikzpicture}   

\end{document}

I readily admit that using an additional coordinate to define the path is awkward, but I wanted to present you with an alternate way to define your angled arrow using the (name.number) syntax instead of the calc one. Also, for this type of diagram, I find the (A |- B) sytnax very helpful.

Markus G.
  • 2,735
0

Another solution, using standard packages (stackengine) ans pstricks:. The relevant frames are defined as pstricks nodes, and linked with the ad hox node connection;

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\usepackage{geometry}
\setlength{\fboxsep} {1ex}
\usepackage{pst-node}

\begin{document}

\noindent\framebox{\quad\Centerstack{Off \State}\quad}\hfill\rnode{IS}{\framebox{\Centerstack{Initialization \State}}} \hfill\rnode{NRS}{\framebox{\Centerstack{Non-Real-Time \State}}}\hfill\rnode{RTS}{\framebox{\Centerstack{Real-Time \State}}}\par \psset{arrowinset=0.1, linejoin=1} \ncbar[angle=-90, arm=3ex, offsetA=-15pt, offsetB=-15pt]{->}{NRS}{IS}\naput{text below} \ncline{->}{NRS}{RTS}\naput{text above}\nbput{text below}

\end{document}

enter image description here

Bernard
  • 271,350