6

In TikZ, is it possible to place a node at a specific distance along a path. I know that there is

\draw (A) -- node[pos=<f>, above] {$+$} (B);

which will place a node at fraction f along the path. However, I want to place a node at, say 10pt, before the end of the path from (A) to (B). What is the simplest way to do this in TikZ?

Minimal example

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (A) {$A$};
\node [right=of A] (B) {$B$};
\draw [->] (A) -- node[pos=0.9, above] {$+$} (B);
\end{tikzpicture}
\end{document}
6
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (A) {$A$};
\node [right=of A] (B) {$B$};
\draw [->] (A) -- (B) node[left=10pt, above]  {$+$};
\end{tikzpicture}
\end{document}

enter image description here

11

If you don't mind the decorations library dependency, then

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning, decorations}
\makeatletter
\tikzset{
  distance from start/.code={%
    \pgfgetpath\currentpath\pgfprocessround{\currentpath}{\currentpath}%
    \pgf@decorate@parsesoftpath{\currentpath}{\currentpath}%
    \pgfmathparse{#1/\pgf@decorate@totalpathlength}\tikzset{pos=\pgfmathresult}},
  distance from end/.code={%
    \pgfgetpath\currentpath\pgfprocessround{\currentpath}{\currentpath}%
    \pgf@decorate@parsesoftpath{\currentpath}{\currentpath}%
    \pgfmathparse{1-(#1/\pgf@decorate@totalpathlength)}\tikzset{pos=\pgfmathresult}}
}
\makeatother
\begin{document}
\begin{tikzpicture}

\node (A) {$A$};
\node [right=of A] (B) {$B$};
\draw(A) -- node[distance from start=10pt, above] {$+$}
            node[distance from end=5pt, below] {$\times$} (B);
\draw[->, red] (A.east) -- ++(10pt,0); % Test distances
\draw[->, blue] (B.west) -- ++(-5pt,0);
% test curve
\draw[green](A) to[in=45,out=225] 
            node[distance from start=10mm, inner sep=1pt,circle,fill] {}
            node[distance from end=10mm, inner sep=1pt,circle,fill] {} (B);

\end{tikzpicture}
\end{document}

enter image description here

  • 2
    Nice; that should work even on non-straight paths. – Rmano Oct 8 '17 at 16:39
  • @Rmano ah yes let me add an example – percusse Oct 8 '17 at 16:42
  • Thanks. This is the most versatile answer, but I am accepting the other one as it is simpler and meets my needs. – Aditya Oct 8 '17 at 19:37
  • 1
    This is a good answer, but I must grudgingly admit that the main reason that I upvoted it is because the green swoosh is really pretty. – wchargin Oct 8 '17 at 19:39
5

With the calc library:

\path ($(B)!10pt!(A)$) node [below, red]{$*$};

should work (10pt along the (B)-(A) line).

  • 1
    In contrast to some of the other answers this works even if the path is not horizontal. – Scz Oct 8 '17 at 16:34
  • yes; but I think that @percusse works also for curved path... I think! – Rmano Oct 8 '17 at 16:40
  • I agree, but this answer looks much easier. And that's why I said "some of the other answers". – Scz Oct 8 '17 at 16:43
3

If the path is on a horizontal line, you can use xshift:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
    \begin{tikzpicture}
        \node (A) {$A$};
        \node [right=of A] (B) {$B$};
        \draw [->] (A) -- (B.west) node[above,xshift=-10pt] {$+$};
    \end{tikzpicture}
\end{document}

Note: Use (B.west) instead of (B) if you want the new node left of the end of the path (as said in your question) and not left of the center of (B).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.