Tikz:在坐标之间绘制带有文字的粗箭头

Tikz:在坐标之间绘制带有文字的粗箭头

我想要绘制带有文本的箭头,就像下面我在 Visio 中勾勒出的示例一样:

带有方框和箭头的示例草图

以下代码给出了一个漂亮的箭头形状,但我无法旋转向下箭头中的文本,使其像双箭头一样从右侧读取。此外,如果可以在坐标之间而不是节点之间绘制箭头,定位箭头会更容易。

我还希望能够拉伸箭头(理想情况下是相对于坐标而不是指定绝对长度),就像“更长的箭头”一样。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.arrows}

\tikzset{MyArrow/.style={single arrow, draw=black, minimum width=10mm, minimum height=30mm,
                         inner sep=0mm, single arrow head extend=1mm}}
                         
\begin{document}

\begin{tikzpicture}

\draw[] (0,0) rectangle +(2,3)  node[midway] {Box 1};
\draw[] (4,0) rectangle +(2,3)  node[midway] {Box 2};
\path[] (1,3.5) node[anchor=tip,MyArrow,rotate=270] {single down};
\path[] (5,3.5) node[anchor=west,MyArrow,rotate=90,double arrow] {double up / down};

\end{tikzpicture}

\end{document}

答案1

欢迎使用 TeX.SX!您可以使用该选项shape border rotate仅旋转节点的形状。结合常规rotate选项,您可以分别旋转箭头和内部文本。

至于在两个节点或坐标之间定位这样的箭头,您可以使用与将标签放置到矩形相同的技术:首先在两个节点或两个坐标之间创建一条路径,然后在这条路径的中间(或其他地方)放置一个箭头形的节点。

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}

\tikzset{MyArrow/.style={single arrow, draw=black, minimum width=10mm, minimum height=30mm, inner sep=0mm, single arrow head extend=1mm}}
                         
\begin{document}
\begin{tikzpicture}

\draw[] (0,0) rectangle +(2,3) node[midway] (box1) {Box 1};

\draw[] (6,0) rectangle +(2,3) node[midway] (box2) {Box 2}; 

\path (box1) -- (box2) node[midway, MyArrow] {right}; 

\path (box1) -- (box2) node[midway, MyArrow, shape border rotate=180, yshift=30] {left}; 

\path (box1) -- (box2) node[midway, MyArrow, double arrow, yshift=-30] {left/right}; 

\path (box1) -- +(0,3.5) node[pos=1, MyArrow, rotate=90] {right}; 

\path (box2) -- +(0,3.5) node[pos=1, MyArrow, rotate=90, shape border rotate=180] {left}; 
%      ^ you can also place a coordinate like (0,0) here

\end{tikzpicture}
\end{document}

在此处输入图片描述


更新

由于 OP 在第一个问题中添加了更多要求,因此我对原始答案进行了更新。

要使双箭头的粗细与单箭头的粗细相匹配,您只需将选项添加double arrow head extend到样式中并为其分配与选项相同的值即可。 这两个选项只有在形状为 a或 asingle arrow head extend时才会起作用,因此可以安全地同时添加两个选项。single arrowdouble arrow

为了使箭头延伸到两个坐标或节点之间的距离,可以使用这个答案。我添加了一种新样式,它以两个坐标或节点名称(由 连接--)作为参数,并根据该信息计算箭头的长度。

笔记:为了使其工作,您需要加载该calc库。

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows, calc}

\tikzset{
    MyArrow/.style={
        single arrow, draw=black, minimum width=10mm, minimum height=30mm, inner sep=0mm, single arrow head extend=1mm, double arrow head extend=1mm
    },
    MyLongArrow/.style args={#1 -- #2}{
        insert path={let \p1=($(#1)-(#2)$) in}, 
        single arrow, draw=black, minimum width=10mm, minimum height={veclen(\x1,\y1)-30mm}, inner sep=0mm, single arrow head extend=1mm, double arrow head extend=1mm
    }
}

\begin{document}
\begin{tikzpicture}

\draw[] (0,0) rectangle +(2,5) node[midway] (box1) {Box 1};

\draw[] (6,0) rectangle +(2,5)  node[midway] (box2) {Box 2}; 

\draw[] (12,0) rectangle +(2,5)  node[midway] (box3) {Box 3}; 

\path (box1) -- (box2) node[midway, MyArrow, yshift=15] {right}; 

\path (box1) -- (box2) node[midway, MyArrow, shape border rotate=180, yshift=45] {left}; 

\path (box1) -- (box2) node[midway, MyArrow, double arrow, yshift=-15] {left/right}; 

\path (box1) -- (box3) node[midway, MyLongArrow={box1 -- box3}, yshift=-45] {right}; 

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容