5

Is it possible to set node position in absolute distance along an edge, rather than using pos=<x> where pos=0 is at edge start and pos=1 at edge end (which are relative positions between the two edges extremities)?

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=1pt, draw, circle}]
\draw (0,0) -- node[pos=2mm] {} (1,-.2);  % doesn't work
\draw (0,0) -- node[pos=.5]  {} (1, .2);  % not what i'm looking for
\end{tikzpicture}
\end{document}
2
  • Just to confirm: a. the node shall appear along the linear path (it might be curved in general), b. at an absolute distance, i.e. the intersect of, in your example, a circle with radius 2mm and said (non-)linear path? What shall happen, if the path is shorter than your absolute pos? // What prevents you from placing said node independently from the path? // Thank your
    – MS-SPO
    Commented May 20 at 10:31
  • 1
    My edges are not that easy of course, but thanks for the greant answers that were given !
    – Jav
    Commented May 20 at 15:54

2 Answers 2

7

This can be done with the decorations.markings library, which accepts absolute distances or relative positions. This has the advantage of working on curved edges as well as straight ones.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary {decorations.markings}

\tikzset{marknode/.style={decoration={markings, mark=at position #1 with{\node[inner sep=1pt, draw, circle]{};}},
    postaction=decorate}}

\begin{document}

\begin{tikzpicture}
\draw[marknode=2mm] (0,0) -- (1,-.2);
\draw[marknode=.5] (0,0) -- (1, .2);
\draw[marknode=3mm] (0,0) to[bend right] (1, -.4);    
\end{tikzpicture}

\end{document}
5

With the calc libray. Section 13.5 Coordinate Calculations in pgfmanual'documentation.

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=1pt, draw, circle}]
\coordinate (A) at (0,0);
\coordinate (B) at (1,-.2);    
\draw (A) --  (B);  % doesn't work
\node at ($(A)!2mm!(B)$){};
\draw (0,0) -- node[pos=.5]  {} (1, .2);  % not what i'm looking for
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .