编辑

编辑

我用 tikz 绘制了一些块元素,并将它们命名为 A 和 B。

我试图在它们之间划一条线

\path [line] (requested) -- (processing);

但我还想在线上方显示一些文字。

\path [line] (requested) -- node [above,midway] {Some text} (processing);

可以工作,但如果文本很长,它就会继续运行而不会换行。

我如何确保文本不会比线条本身更宽?

编辑

\documentclass[tikz]{standalone}

\usetikzlibrary{shapes,arrows,chains}

\begin{document}

\tikzstyle{block} = [rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance=1cm, auto]
    \node (init) {};
    \node [block] (A) {A};
    \node [block, right=of A] (B) {B};

    \path [line] (A) -- node [text width=2.5cm,midway,above] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}

\end{document}

我明白了

在此处输入图片描述

我怎样才能更聪明地确保有足够的空间容纳文本?我有多个框。

答案1

两种可能性 - 指定上面的距离 - 通过分离块

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,chains}
\usetikzlibrary[calc]

\begin{document}

\tikzstyle{block} = [rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance=1cm, auto]
\node (init) {};
\node [block] (A) {A};
\node [block, right= of A] (B) {B};

\path [line] (A) -- node [text width=2.5cm,midway,above=3em ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}


\begin{tikzpicture}[node distance=1cm, auto]
\node (init) {};
\node [block] (A) {A};
\node [block, right=3cm of A] (B) {B};

\path [line] (A) -- node [text width=2.5cm,midway,above ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}

\end{document}

在此处输入图片描述

如果你想让文本居中

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes,arrows,chains}
\usetikzlibrary[calc]

\begin{document}

\tikzstyle{block} = [rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance=1cm, auto]
    \node (init) {};
    \node [block] (A) {A};
    \node [block, right= of A] (B) {B};

    \path [line] (A) -- node [text width=2.5cm,midway,above=3em,align=center ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}


\begin{tikzpicture}[node distance=1cm, auto]
    \node (init) {};
    \node [block] (A) {A};
    \node [block, right=3cm of A] (B) {B};

    \path [line] (A) -- node [text width=2.5cm,midway,above,align=center ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

作为锻炼...

\documentclass[tikz,border=2mm]{standalone}
    \usetikzlibrary{arrows,chains}

\begin{document}
    \begin{tikzpicture}[
 start chain = going right,
block/.style = {rectangle, draw, rounded corners, 
                text width=6em, align=center, minimum height=4em,
                on chain},
every pin/.style = {inner sep=1mm, align=center, font=\footnotesize,
                    pin distance=9mm, pin edge={angle 60-, solid, black}},
                        ]
\node[block] (A) {A};
\node[block] (B) {B};

\linespread{0.9}
    \draw[-latex'] (A) to node[inner sep=0pt,
                                pin=above:My very looooooong\\
                                text which is wider\\
                                than the arrow below] {}    (B);
    \end{tikzpicture}
\end{document}

当一行中没有足够的空间放置文本时,这是添加行描述(含义)的常用方法。此文本通常比正文小。为此,我选择\footnotesize。我手动将其拆分,我认为通过这种方式,您可以轻松地根据自己的喜好或可用空间调整其外观。

在此处输入图片描述

答案3

这里有一种方法可以使用 MetaPost 和包(圆角框包rboxes的补充),可能对大家有用:将文本标签放在指定宽度(这里是 2.5 厘米)中,将两个框分开,间距大于此宽度(这里是 2.8 厘米),并将标签放置在连接两个框的箭头中间。请注意,我选择将文本置于标签的中心,因为这样看起来更好。boxes\parbox

代码已包含在 LuaLaTeX 程序中,以方便排版。此外,它还允许使用emenTeX 单位,这要归功于luamplib命令\mpdim。另请参阅包裹gmp另一方面,这不是特定于 LuaLaTeX 的,而是将 MetaPost 代码插入 LaTeX 程序的不太直接的方法。

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\begin{document}
  \begin{mplibcode}
    input rboxes;
    beginfig(1);
      defaultdx := \mpdim{3em}; defaultdy := \mpdim{3ex};
      rboxit.A(btex A etex); rboxit.B(btex B etex);
      .5[A.e, B.w] = origin;
      B.w - A.e = (2.8cm, 0);
      drawboxed(A,B);
      drawarrow A.e -- B.w;
      label.top(btex \parbox{2.5cm}{\centering My very looooooong text 
        which is wider than the arrow below} etex, .5[A.e,B.w]);
    endfig;
  \end{mplibcode}
\end{document}

在此处输入图片描述

相关内容