TikZ - 文本位于两个直角之间

TikZ - 文本位于两个直角之间

我似乎无法让文本位于路径上两个直角之间的中心。

平均能量损失

\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 输出

在此处输入图片描述

预期输出

text below位于两个直角之间的箭头路径的中心。

在此处输入图片描述

答案1

使用pos=0.25而不是midway

位置=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}

解释

当您将|-选项用于路径时,角度处的点将被视为midway整个路径的 。因此,第一条线的中间位置为pos=0.25,而第二条线的中间位置为pos=0.75

答案2

SebGlav 的回答无疑是解决您问题最简单的方法。但是,出于信息目的,我将提出一个替代解决方案:

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

我承认使用附加项coordinate来定义路径很不方便,但我想向您介绍一种替代方法来定义有角度的箭头,即使用 (name.number) 语法而不是 calc 语法。此外,对于这种类型的图表,我发现语法(A |- B)非常有用。

答案3

另一个解决方案是使用标准包(stackengine)和pstricks:。相关框架定义为pstricks nodes,并与特设节点连接;

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

在此处输入图片描述

相关内容