2

I wish start/end of the arrow match the point A/B exactly.

\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}

Output: enter image description here

lucky1928
  • 4,151

1 Answers1

3

The center anchor isn't in the true center of the node but at the center of the rectangular text bit.

You will have to use the tail anchor and at start if I'm understanding you correctly. That said, you don't even need the rotate part if you use sloped.

You can also do use rotate but without the actual path in the first place: Can we use `let` (from `TikZ`) inside a node's style definition?

Code

\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}

Output

enter image description here

Qrrbrbirlbel
  • 119,821