我在两个坐标之间有一个箭头
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[fill=blue,circle,text width=3cm] (first) at (1,1) {};
\node[fill=purple,circle,text width=3cm] (third) at (1,9) {};
\draw [->] (first) -- (third);
\end{tikzpicture}
\end{document}
我怎样才能将箭头改成这样,我想在它上面添加形状,其中可以包含一些文本
编辑
如果箭头在曲线上怎么办?
答案1
您可以使用选项在路径顶部绘制节点pos=x
,其中x
是介于 0(表示路径的起点)和 1(表示终点)之间的数字。在您的示例中:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzset{
my box/.style = {rectangle, draw=black, fill=white, minimum width=2.5em,
}
}
\begin{tikzpicture}
\node[fill=blue,circle,text width=3cm] (first) at (1,1) {};
\node[fill=purple,circle,text width=3cm] (third) at (9,1) {};
\draw [->] (first) -- (third)
node[pos=0.25, my box] {A}
node[pos=0.5, my box] {B}
node[pos=0.75, my box] {C};
\end{tikzpicture}
\end{document}
此解决方案的优点是它也能轻松用于非水平或垂直路径。此外,如果您添加sloped
选项,my box/.style
这将导致 A、B、C 框使用与箭头线相同的斜率:
\tikzset{
my box/.style = {rectangle, draw=black, fill=white, minimum width=2em, sloped,
}
}
\begin{tikzpicture}
\node[fill=blue,circle,text width=1cm] (first) at (1,1) {};
\node[fill=purple,circle,text width=1cm] (third) at (7,-3) {};
\draw [-latex] (first) -- (third)
node[pos=0.35, my box] {A}
node[pos=0.5, my box] {B}
node[pos=0.65, my box] {C};
\end{tikzpicture}
更新
回答评论中 OP 的一个问题,只要使用语法指定路径..
并包括贝塞尔控制点,上述方法适用于曲线路径。例如:
\tikzset{
my box/.style = {rectangle, draw=black, fill=white, minimum width=2em, sloped,
}
}
\begin{tikzpicture}
\node[fill=blue,circle,text width=1cm] (first) at (1,1) {};
\node[fill=purple,circle,text width=1cm] (third) at (7,-3) {};
\draw [-latex] (first) .. controls (+5,0) and +(-5,0) .. (third)
node[pos=0.2, my box] {A}
node[pos=0.5, my box] {B}
node[pos=0.8, my box] {C};
\end{tikzpicture}
但是,它不适用于 更新:to
更方便的语法:to
只要node
将 s 放在最终坐标之前,语法也受支持(感谢 mwibrow 的提示!),因此以下内容也有效:
\tikzset{
my box/.style = {rectangle, draw=black, fill=white, minimum width=2em, sloped,
}
}
\begin{tikzpicture}
\node[fill=blue,circle,text width=1cm] (first) at (1,1) {};
\node[fill=purple,circle,text width=1cm] (third) at (7,-3) {};
\draw [-latex] (first) to[out=0, in=180]
node[pos=0.3, my box] {A}
node[pos=0.5, my box] {B}
node[pos=0.7, my box] {C}
(third);
\end{tikzpicture}