I'm trying in TikZ to find a general way to draw an arrow and label it at the end. Something like this works reasonably well:
\draw[->] (0, 0) -- (1, 3) node[pos=1.1]{$\vec{v}$};
However, depending on the orientation, if the label is too long, then the label can overlap the arrow:
\draw[->] (0, 0) -- (3, 0) node[pos=1.1]{$\vec{v} + \vec{w}$};
What I would ideally like is, for the latter arrow, the left side is at pos=1.1. I know I can do this in this specific case by using node[pos=1.1, anchor=west]
, but I'm looking for something more general (so that I could abstract this all and define a command to do it automatically).
One approach I considered was trying to find the width and height of the label and move the label using those values. So, 2 questions: Is there a way to find the width/height? Or, alternatively, is there a better approach?
Best Answer
For straight lines (
--
) a specialto path
could be the solution.I have defined three
to
styles:a=<node text>
: relative positioninga position=<pos amount>
(default1.1
)aa=<node text>
: absolute positioningaa distance=<length>
(default1ex
)bb
: absolute positioning but saves the coordinate and the angle for later use:\xVecN
: the x value,\yVecN
: the y value,\aVecB
: theanchor
angle.bb style
uses these values internally.aa distance=<length>
(default1ex
)The lines in the code sample that are marked with
% debug
can be removed; they are only there to show how the styles work.Code
Output
% debug
How it works
Difference between absolute and relative positioning
Output