Positioning arrow labels at absolute distance from end or start

I am making arrows using

\begin{tikzpicture}
\tikzset{
operatorNode/.style={
every node/.style={minimum size=1cm,draw,rectangle}
},
variableNode/.style={
every node/.style={minimum size=1cm,draw,circle}
}
}

\begin{scope}[operatorNode]
\node (x) at (0,0) {$4$};
\end{scope}
\begin{scope}[variableNode]
\node (z1) at (4,0) {$z$};
\node (z2) at (8,-2) {$z$};
\end{scope}
\draw[->] (x) -- (z1) node [near start, above] {$1$};
\draw[->] (x) -- (z2) node [near start, above] {$2$};
\end{tikzpicture}


This produces

However I have multiple arrows of variable length, hence the 'near start' option results in different label positions for different arrows. This happens because the 'near start' places the label at a relative position.

What I want is:

Is it also possible to place the label at an absolute distance from the start (or the end) from the arrow head?

• \draw[->] (v2) node [above right] {$1$} -- (z) ; ? how far the node should be from v2? – Zarko May 13 '19 at 10:45
• There is a container around the nodes (z) and (v2). It seems that this is not respecting the container, because the label is now placed inside the container, not at the arrow. (the arrow does respect the container). @Zarko, I added the code for the container. – Jens Wagemaker May 13 '19 at 10:51
• sorry, how can i know this? your code doesn't gives very different result than mine ...please, extend your code sniped to complete small document, which we can compile. and clearly describe, where you like to have arrows label. – Zarko May 13 '19 at 10:54
• @Zarko, I added a small complete document (I ommited the \begin{document} stuff). Is the problem more clear now? – Jens Wagemaker May 13 '19 at 11:01
• yes, it is. you should rename your "container" (it is actually node style). apparently you have different nodes style (still is not know, why you need this style. you šhould show use more context and sketch, what you like to have. – Zarko May 13 '19 at 11:05

2 Answers

see if the following solution is acceptable for you:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
lbl/.style = {inner sep=2pt, node font=\footnotesize,
above right=0pt and 10 pt of #1}% set distance according to your wish
]

\begin{scope}[every node/.style={minimum size=2cm,draw}]
\node (v1) at (0,3) {$4$};
\node (z1) at (4,3) {$z$};
\node (v2) at (0,0) {$4$};
\node (z2) at (8,0) {$z$};
\end{scope}
\draw[->] (v1) node [lbl=v1.east] {$1$} -- (z1);
\draw[->] (v2) node [lbl=v2.east] {$2$} -- (z2);
\end{tikzpicture}
\end{document}


• I try to compile your code in Tikzedit, but I can't get it working. Is it possible for you to give the code so that I can compile it in Tikzedit, or explain how I can do that. I get the error: "Couldn't parse code. NoViableAltException in line 4 at position 30. Is some \end{} command missing? Compilation failed with exit code 1" Even after I add \end{document}. – Jens Wagemaker May 13 '19 at 11:34
• well, than your editor is faulty. this work any standard latex editor this work (i use WinEdt). – Zarko May 13 '19 at 11:46
• When I run the following code in Overleaf "\documentclass{article} \usepackage[utf8]{inputenc} \usepackage{tikz} [your code] \end{document}" I get the error: "Package PGF Math Error: Unknown operator a' or an' (in '0pt and 10 pt of v1.east')." – Jens Wagemaker May 13 '19 at 11:51
• I got the problem: it needed to include \usetikzlibrary{positioning}. – Jens Wagemaker May 13 '19 at 11:58
• Sorry, now i see, that i provide only picture body without of the document preamble and \end{document} . now is corrected and also tested in Overleaf. It should work. – Zarko May 13 '19 at 12:02

The decorations.markings library allows you to put stuff at an absolute position of a path.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
operatorNode/.style={
every node/.style={minimum size=1cm,draw,rectangle}
},
variableNode/.style={
every node/.style={minimum size=1cm,draw,circle}
},at absolute position/.style n args={2}{postaction={decorate,
decoration={markings,
mark=at position #1 with {#2}}}}
]

\begin{scope}[operatorNode]
\node (x) at (0,0) {$4$};
\end{scope}
\begin{scope}[variableNode]
\node (z1) at (4,0) {$z$};
\node (z2) at (8,-2) {$z$};
\end{scope}
\draw[->,at absolute position={1cm}{\node [above] {$1$};}] (x) -- (z1)  ;
\draw[->,at absolute position={2cm}{\node [above] {$2$};}] (x) -- (z2) ;
\end{tikzpicture}
\end{document}


The calc library also allows you to do that.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\tikzset{
operatorNode/.style={
every node/.style={minimum size=1cm,draw,rectangle}
},
variableNode/.style={
every node/.style={minimum size=1cm,draw,circle}
}
}

\begin{scope}[operatorNode]
\node (x) at (0,0) {$4$};
\end{scope}
\begin{scope}[variableNode]
\node (z1) at (4,0) {$z$};
\node (z2) at (8,-2) {$z$};
\end{scope}
\draw[->] (x) -- (z1) coordinate[pos=0] (aux0) coordinate[pos=1] (aux1);
\path ($(aux0)!1cm!(aux1)$) node [above] {$1$};
\draw[->] (x) -- (z2) coordinate[pos=0] (aux0) coordinate[pos=1] (aux1);
\path ($(aux0)!2cm!(aux1)$) node [above] {$2$};
\end{tikzpicture}
\end{document}