如何使用 tikzset 将文本换行到箭头中

如何使用 tikzset 将文本换行到箭头中

我参考了网络并找到了一些添加花式箭头的好例子,我展示了我的代码

\documentclass[a4paper,11pt]{book}

\usepackage{color}
\usepackage{xcolor,colortbl}
\definecolor{ForestGreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{OrangeRed}{rgb}{1.0, 0.27, 0.0}
\definecolor{FireBrick}{rgb}{0.7, 0.13, 0.13}
\definecolor{Gold}{rgb}{1.0, 0.84, 0.0}
\definecolor{DodgerBlue}{rgb}{0.12, 0.56, 1.0}
\definecolor{Tomato}{rgb}{1.0, 0.39, 0.28}

\usepackage{tikz}
\usetikzlibrary{calc,fadings,shapes.arrows,shadows,backgrounds, positioning}
\tikzset{bubble/.style={rectangle, draw=gray,rounded corners,fill=#1,align = flush center,minimum height=1cm,minimum width=1.75cm}}
\tikzfading [name=arrowfading, top color=transparent!0, bottom color=transparent!95]
\tikzset{arrowfill/.style={top color=OrangeRed!20, bottom color=red, general shadow={fill=black, shadow yshift=-0.8ex, path fading=arrowfading}}}
\tikzset{arrowstyle/.style={draw=FireBrick,arrowfill, single arrow,minimum height=#1, single arrow, single arrow head extend=.4cm,}}

\begin{document}
\begin{figure}[htbp]
\begin{tikzpicture}
    % Bubbles
    \node[bubble=ForestGreen!40] (ba) at (0,0) {Bubble A};
    \node[bubble=Gold!40] (bb) at (2.6,0) {Bubble B};
    \node[bubble=DodgerBlue!40] (bc) at (5.5,0) {Bubble C};
    \node[bubble=Tomato!40] (bd) at (8.5,0) {Bubble\\with long\\name};

    \node[above= 0.5 cm of bb,font={\bf}]{Part 1};
    \node[above= 0.5 cm of bd,font={\bf}]{Part 2};

    % Lines
    \foreach \bubble in {ba,bb,bc,bd}
    \draw[ultra thick, gray] ($(\bubble.south)-(0,0.1)$)--($(\bubble.south)-(0,5)$);

    % Arrows
    \node [arrowstyle=2.9cm,xshift=-0.1cm,yshift=-1cm] at ($(ba.south)!0.5!(bb.south)$) {test sting 1};
    \node [arrowstyle=3cm,xshift=-0.1cm,yshift=-2cm] at ($(bb.south)!0.5!(bc.south)$) {test sting 2, test sting 2.1, test sting 2.2};
    \node [arrowstyle=3.1cm,xshift=-0.1cm,yshift=-3cm] at ($(bc.south)!0.5!(bd.south)$) {test sting 3};
    \node [arrowstyle=6cm,xshift=-0.1cm,yshift=-4.5cm] at ($(bb.south)!0.5!(bd.south)$) {test sting 4};
    \node [arrowstyle=3.3cm,yshift=-1cm,shape border rotate=180] at ($(bc.south)!0.55!(bd.south)$) {test sting 5};

    % Background
    \begin{pgfonlayer}{background}
    \fill[gray!10]($(bc.north)!0.5!(bd.north)+(0,0.5)$)rectangle($(bc.south)!0.5!(bd.south)+(4,-5.1)$) ;
    \draw[dashed, shorten <=-1.5cm] ($(bc.south)!0.5!(bd.south)$)--($(bc.south)!0.5!(bd.south)-(0,5.1)$);
    \end{pgfonlayer}
\end{tikzpicture}
\end{figure}
\end{document}

这将提供良好的输出,但我的问题是,如何将字符串从气泡包装到箭头bbbc

我的输出显示: 在此处输入图片描述

答案1

如果某些节点内容text width是固定的,则可以将其换行。也可以选择应用对齐选项 ( align=left|center|right)。使用此选项,无需\\像在 bubble 中那样使用换行符 ( ) bd

下一个代码还展示了如何使用relative定位而不是硬编码坐标。您可以使用intersection coordinate system(|--|) {x|y}shift、它们等来固定参考点。我现在无法解释所有这些,但您会在不远处找到很多解释过的示例。

最后,我还将绘制垂直线移至background范围,以便使用最底部的箭头设置它们的长度。

TikZ当然,这只是一个例子,向您展示一些可以获得类似结果的选项。

\documentclass[a4paper,11pt]{book}

\usepackage{color}
\usepackage{xcolor,colortbl}
\definecolor{ForestGreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{OrangeRed}{rgb}{1.0, 0.27, 0.0}
\definecolor{FireBrick}{rgb}{0.7, 0.13, 0.13}
\definecolor{Gold}{rgb}{1.0, 0.84, 0.0}
\definecolor{DodgerBlue}{rgb}{0.12, 0.56, 1.0}
\definecolor{Tomato}{rgb}{1.0, 0.39, 0.28}

\usepackage{tikz}
\usetikzlibrary{calc,fadings,shapes.arrows,shadows,backgrounds, positioning}
\tikzset{bubble/.style={rectangle, draw=gray,rounded corners,fill=#1,align = flush center,minimum height=1cm,minimum width=1.75cm, text width=1.5cm, align=center}}
\tikzfading [name=arrowfading, top color=transparent!0, bottom color=transparent!95]
\tikzset{arrowfill/.style={top color=OrangeRed!20, bottom color=red, general shadow={fill=black, shadow yshift=-0.8ex, path fading=arrowfading}}}
\tikzset{arrowstyle/.style={draw=FireBrick,arrowfill, single arrow,minimum height=#1, single arrow, single arrow head extend=.4cm,text width=3cm, align=center}}

\begin{document}
\begin{figure}[htbp]
\begin{tikzpicture}
    % Bubbles
    \node[bubble=ForestGreen!40] (ba) at (0,0) {Bubble A};
    \node[bubble=Gold!40] (bb) at (2.6,0) {Bubble B};
    \node[bubble=DodgerBlue!40] (bc) at (5.5,0) {Bubble C};
    \node[bubble=Tomato!40] (bd) at (8.5,0) {Bubble with long name};

    \node[above= 0.5 cm of bd,font={\bf}] (part2){Part 2};
    \node at (part2-|bb) (part1) {Part 1};


    % Arrows
    \node [arrowstyle=2.9cm,below = 1cm of ba, anchor=tail] (sting1) {test sting 1};
    \node [arrowstyle=3.3cm,below = 1cm of bd, shape border rotate=180, anchor=tail] (sting5) {test sting 5};
    \node [arrowstyle=3cm, text width=3cm, align=center, anchor=tail] at ([yshift=-10mm]bb|-sting5.before tip) (sting2) {test sting 2, test sting 2.1, test sting 2.2};
    \node [arrowstyle=3.1cm,below=5mm of sting2.after tip,anchor=tail, ] (sting3) {test sting 3};
    \node [arrowstyle=6cm,below=3mm of {bb.south|-sting3.before tail}, anchor=tail] (sting4) {test sting 4};

    % Background
    \begin{pgfonlayer}{background}
    \fill[gray!10]($(bc.north)!0.5!(bd.north)+(0,0.5)$) rectangle ([shift={(3mm,-3mm)}]sting4.after tip-|bd.east) ;

\draw[dashed, shorten <=-1.5cm] ($(bc.south)!0.5!(bd.south)$)--($(bc.south)!0.5!(bd.south)-(0,5.1)$);

    % Lines
    \foreach \bubble in {ba,bb,bc,bd}
    \draw[ultra thick, gray] (\bubble.south)--([yshift=-3mm]\bubble.south|-sting4.after tip);
    \end{pgfonlayer}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容