7

I am trying to place a node at the intersection of two lines, however I do not obtain the desired result.

I would like the circled x in the pciture below to appear on the intersection of the two lines. What am I doing wrong?

enter image description here

mwe:

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{intersections}
\PreviewEnvironment{tikzpicture}
\begin{document}
    \begin{tikzpicture}
        \node (l3) at (-1,-2) {test};
        \node (c1) at (intersection of (0,0)-- ++(300:7) and {l3.north--++(99,0)})[draw, circle]{x};
        \draw (0,0)-- ++(300:3) (l3.north)--++(4,0);
    \end{tikzpicture}
\end{document}

EDIT Explanation, why I need the different syntax: I am creating a picture like this:

pyramid

I draw and adjust the text nodes by hand, afterwards I need to draw the horizontal lines into the triangle. I decided to be to lazy to use pgfmath, with the chosen awnser I can write the following to generate the horizontal lines:

\foreach \level in {nodeL1, nodeL2, ...} {      
    \coordinate (c1) at (intersection of 0,0--[shift={(300:7)}]0,0 and \level.north--[xshift=99]\level.north);
    \coordinate (c2) at (intersection of 0,0--[shift={(240:7)}]0,0 and \level.north--[xshift=99]\level.north);      
    \draw (c1) -- (c2);
};
ted
  • 3,377
  • 1
    Have you seen this post: http://tex.stackexchange.com/questions/31398/tikz-intersection-of-two-lines?rq=1 – dustin Jun 23 '13 at 17:48
  • @dustin: yes but it is concerned about something not being named, I was concerned about the intersection of-Syntax WITHOUT named paths not working with relative coordinates (see Qrrbrbirlbel s awnser) – ted Jun 23 '13 at 18:39
  • This is an old question and this syntax is deprecated, but in case somebody need it you can use (intersection of 0,0--300:7 and l3.north--[xshift=4cm]l3.north). – Kpym Apr 30 '18 at 14:01

3 Answers3

8

The two path for the lines are first named, and then the intersection is obtained. The pgfinterruptboundingbox environment ensures that the paths used for the calculation don't add unwanted space:

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{intersections}
\PreviewEnvironment{tikzpicture}

\begin{document}
    \begin{tikzpicture}
        \node (l3) at (-1,-2) {test};
\begin{pgfinterruptboundingbox}
        \path[name path=line1] (0,0) -- ++(300:3);
        \path[name path=line2] (l3.north) -- ++(99,0);
        \path[name intersections={of=line1 and line2, by={a}}] node (c1) at (a)[draw, circle]{x};
\end{pgfinterruptboundingbox}
        \draw (0,0)-- ++(300:3) (l3.north)--++(4,0);
    \end{tikzpicture}
\end{document}

enter image description here

Gonzalo Medina
  • 505,128
8

You're not using the intersections syntax correctly. The following should work

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{intersections}
\PreviewEnvironment{tikzpicture}
\begin{document}
    \begin{tikzpicture}
        \node (l3) at (-1,-2) {test};
        \draw[name path=A] (0,0)-- ++(300:3); 
        \draw[name path=B] (l3.north)--++(4,0);
        \node[name intersections={of= A and B}] (c1) at (intersection-1)[draw, circle]{x};
    \end{tikzpicture}
\end{document}

enter image description here

Herr K.
  • 17,946
  • 4
  • 61
  • 118
7

Neither the intersection of coordinate syntax nor the intersection coordinate system can handle relative coordinates (directly). And the intersections library isn’t needed for both of them.

I suggest declaring the needed coordinate beforehand (wrapped inside the pgfinterruptboundingbox environment) or using the shift option for the relative nodes.

The syntax

(intersection cs: first line={(origin)--(origin')}, second line={(l3)--(l3')})

is the same as

(intersection of origin--origin' and l3--l3')

The latter is transformed into the first by the parser.

Code

\documentclass[tikz,convert=false]{standalone}
\tikzset{nodes={draw,circle,fill,minimum size=+1pt, inner sep=+0pt}}
\begin{document}
\begin{tikzpicture}
    \begin{pgfinterruptboundingbox}
    \path (-1,-2) coordinate (l3)
        ++(99,0)  coordinate (l3')
          (0,0)   coordinate (origin)
        ++(300:7) coordinate (origin')
    ;
    \end{pgfinterruptboundingbox}
    \node (c1) at (intersection of origin--origin' and l3--l3') {};
    \draw (0,0)-- ++(300:3) (l3)--++(4,0);
\end{tikzpicture}
\begin{tikzpicture}
    \path (-1,-2) coordinate (l3)
          (0,0)   coordinate (origin);
    \node (c1) at (intersection of origin--[shift={(300:7)}]origin and l3--[shift={(l3)}]99,0) {};
    \draw (0,0)-- ++(300:3) (l3)--++(4,0);
\end{tikzpicture}
\begin{tikzpicture}
    \path (-1,-2) coordinate (l3)
        ++(4,0)   coordinate (l3')
          (0,0)   coordinate (origin)
        ++(300:3) coordinate (origin')
    ;
    \node (c1) at (intersection of origin--origin' and l3--l3') {};
    \draw (origin)--(origin') (l3)--(l3');
\end{tikzpicture}
\begin{tikzpicture}
    \coordinate (l3) at (-1,-2);
    \node (c1) at (intersection of 0,0--300:7 and l3--[shift={(l3)}]99,0) {};
    \draw (0,0)-- ++(300:3) (l3)--++(4,0);
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • You pinpointed my mistake, assuming that if intersection of (namedCoord1)--(namedCoord2) and (namedCoord3)--(namedCoord4) was allowed, giving absolute/relative coordinates would be fine too. Thanks for the detailed background explanations. – ted Jun 23 '13 at 18:45