我希望箭头的起点/终点与点 A/B 完全匹配。
\documentclass[convert,border=10mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows, calc}
\makeatletter
\newcommand{\getxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
\newcommand{\darr}[3]{%
\getxy{(#1)}{\ax}{\ay}
\getxy{(#2)}{\bx}{\by}
\newcommand{\ang}{atan((\by-\ay)/(\bx-\ax))}
\path (#1) -- (#2) node[
draw=black,single arrow,midway,inner sep=0mm,outer sep=0mm,
insert path={let \p1=($(#1)-(#2)$) in},
minimum width=0mm,
minimum height={veclen(\x1,\y1)},
single arrow head extend=2mm,
double arrow head extend=2mm,
rotate=\ang,
] {#3};
}%
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3,3);
\darr{A}{B}{hello}
\draw[red!90] (A) grid (B);
\end{tikzpicture}
\end{document}
答案1
锚点center
并不位于节点的真正中心,而是位于矩形文本位的中心。
如果我理解正确的话,您将必须使用锚点tail
和at start
。也就是说,如果您使用,您甚至不需要该rotate
部件sloped
。
您也可以使用,rotate
但首先不需要实际路径:我们可以在节点的样式定义中使用 `let`(来自 `TikZ`)吗?
代码
\documentclass[convert,border=10mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows, calc}
\newcommand{\darr}[3]{%
\path (#1) -- (#2) node[
shape=single arrow, draw=black,
at start, anchor=tail,
inner sep=0mm, outer sep=0mm,
insert path={let \p1=($(#2)-(#1)$) in},
minimum width=0mm,
minimum height={veclen(\x1,\y1)},
single arrow head extend=2mm,
double arrow head extend=2mm,
% rotate={atan2(\y1,\x1)}
] {#3};}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3,3);
\darr{A}{B}{hello}
\draw[red!90] (A) grid (B);
\end{tikzpicture}
\end{document}