如何以多种方式将多行文本对齐箭头?

如何以多种方式将多行文本对齐箭头?

如何对齐箭头上的文本(相对于箭头末端和其自身的文本对齐方式)?例如,我在箭头上有多条线,我想将它们放置在箭头末端的左侧、中间、右侧和某个指定距离(例如 5 毫米)处,同时还要将文本对齐在左侧、右侧和中间。有没有更好的方法可以让箭头上有多条线,而不是使用“文本宽度”(我暂时使用)?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{calc,decorations.markings}

\begin{document}

\begin{figure}
    \centering  
    \begin{tikzpicture}[
        node distance   = 1cm and 2cm,
        N1/.style   = {rectangle, draw, rounded corners, fill=orange!10, minimum width=1cm, minimum height=1cm, align=center},
        N2/.style   = {rectangle, draw, rounded corners, fill=orange!10, minimum width=1cm, minimum height=4cm, align=center},      arrow/.style = {thick,-stealth}
    ]

    \node   (a1)    [N1]                                        {abc};
    \node   (a2)    [N2, right=4cm of a1]                       {def};

    \path   [draw, arrow]  ([yshift=2mm] a1.east)  coordinate (aux1) node[above right, red, text width=1cm]{going left} --  (aux1 -| a2.west);      
    \path   [draw, arrow]  ([yshift=-2mm] a2.west) coordinate (aux1) node[below left, red,  text width=1cm]{going right} -- (aux1 -| a1.east);      

    \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

答案1

部分基于我对您上一个问题的回答:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes}

\begin{document}

\begin{figure}
    \centering
\tikzset{
node distance = 1cm and 4cm,
     N/.style = {draw, rounded corners, fill=orange!10,
                 minimum height=#1, minimum width=1cm},
    ar/.style = {draw=red, thick, -Straight Barb},
every edge quotes/.append style = {font=\footnotesize,
                                   text width=2em, align=center}
        }
    \begin{tikzpicture}
\node (a1)  [N=10mm]                {abc};
\node (a2)  [N=40mm, right=of a1]   {def};
%
\draw[ar] ([yshift= 2mm] a1.east) coordinate (aux) to["going right", near start]  (aux -| a2.west);
\draw[ar] ([yshift=-2mm] a2.west) coordinate (aux) to["going  left", near start]  (aux -| a1.east);
    \end{tikzpicture}

\medskip
    \begin{tikzpicture}
\node (a1)  [N=10mm]                {abc};
\node (a2)  [N=40mm, right=of a1]   {def};
%
\draw[ar] ([yshift= 2mm] a1.east) coordinate (aux) to["going right", near end]  (aux -| a2.west);
\draw[ar] ([yshift=-2mm] a2.west) coordinate (aux) to["going  left", near end]  (aux -| a1.east);
    \end{tikzpicture}

\medskip
    \begin{tikzpicture}
\node (a1)  [N=10mm]                {abc};
\node (a2)  [N=40mm, right=of a1]   {def};
%
\draw[ar] ([yshift= 2mm] a1.east) coordinate (aux) to["going right"]  (aux -| a2.west);
\draw[ar] ([yshift=-2mm] a2.west) coordinate (aux) to["going  left"]  (aux -| a1.east);
    \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

如果您更喜欢这样的解决方案,您可以手动中断边缘标签文本。在这种情况下,建议text widthevery edge quotes样式定义中删除选项并在标签文本中添加行终止符\\,如以下示例中所示:

\draw[ar] ([yshift= 2mm] a1.east) coordinate (aux) to["going\\ right"]  (aux -| a2.west);
\draw[ar] ([yshift=-2mm] a2.west) coordinate (aux) to["going\\  left"]  (aux -| a1.east);

有关线上节点(即边标签)的更多选项和详细信息,请参阅TikZ & PGF 手册, 部分17.10.2 标签选项,第 246-249 页(版本 3.1.b)。

答案2

另一个不同的选择是将参数pos放入您的node规范中,使用 0 到 1 之间的数字,具体取决于您想要在路径上放置节点的位置。pos=0表示起点,pos=1表示终点,pos=0.5相当于midway
您必须绘制路径,然后放置您的节点(那么您不必使用该to[...]参数)。

结果如下:

路径上的位置

代码如下(只修改了最后两行):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{calc,decorations.markings}

\begin{document}

\begin{figure}
    \centering  
    \begin{tikzpicture}[
        node distance   = 1cm and 2cm,
        N1/.style   = {rectangle, draw, rounded corners, fill=orange!10, minimum width=1cm, minimum height=1cm, align=center},
        N2/.style   = {rectangle, draw, rounded corners, fill=orange!10, minimum width=1cm, minimum height=4cm, align=center},      arrow/.style = {thick,-stealth}
    ]

    \node   (a1)    [N1]                                        {abc};
    \node   (a2)    [N2, right=4cm of a1]                       {def};

    \path   [draw, arrow]  ([yshift=2mm] a1.east)  coordinate (aux1) --  (aux1 -| a2.west) node[pos=0.1,above right, red,text width=1cm, align=center]{going left};      
    \path   [draw, arrow]  ([yshift=-2mm] a2.west) coordinate (aux1) -- (aux1 -| a1.east) node[pos=0.1,below left, red,  text width=1cm, align=center]{going right};      

    \end{tikzpicture}
\end{figure}
\end{document}

只需调整pos值和align=选项即可获得您想要的结果。

相关内容