2

Background: I understand that TikZ is built on top of pgf. Both are described in the pgf manual. However, I have been unable to find the section (if it exists at all) of the manual describing how the two are connected. This is a problem to me, as I enjoy the high level constructs of TikZ, yet I notice a number of useful primitives in pgf that I don't know how to access.

As an example, consider this simple picture:

two line segments and the intersection of their extensions

There are two short, non-intersecting line segments. The circle marks the point where the extended lines meet. The line segments and node labels were created with TikZ, the intersection with pgf.

Here is the code, lifted from the manual and adapted slightly:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=20mm,y=20mm]
  \draw
  (.5,0) node[left](A){$A$} -- (1,.8) node[left]{B}
  (2,0) node[left](C){$C$} -- (1.5,1) node[right](D){D};
  \pgfpathcircle{%
    \pgfpointintersectionoflines
      {\pgfpointxy{.5}{0}}{\pgfpointxy{1}{.8}}
      {\pgfpointxy{2}{0}}{\pgfpointxy{1.5}{1}}}
    {2pt}
  \pgfusepath{stroke}
\end{tikzpicture}
\end{document}

Clearly, the code is not DRY: The coordinates are repeated in the pgf section. But if the points A–D were them selves the result of calculations, this is not a viable option. Also, I can draw the intersection point, but I cannot use it in further work at the TikZ level. Hence my questions:

  • How do I represent the points A–D in terms that \pgfpointintersectionoflines and other pgf constructs can use?
  • And how do I extract the result from the pgf world, for further use in the TikZ world?

Note that I am looking for answers that can be generalised, not a hack applying only to the present problem.

  • 1
    For the second part, seems you can do e.g. \pgfcoordinate{foo}{ \pgfpointintersectionoflines {\pgfpointxy{.5}{0}}{\pgfpointxy{1}{.8}} {\pgfpointxy{2}{0}}{\pgfpointxy{1.5}{1}}} to get a coordinate named foo at the intersection. – Torbjørn T. Mar 3 at 14:30
  • @TorbjørnT. Indeed, that works. Thanks! (To be clear, it can then be referred to as (foo) later on.) – Harald Hanche-Olsen Mar 3 at 15:09
3

In addition to what Torbjørn T. says you can use existing nodes/coordinates with \pgfpointanchor{<name>}{<anchor>}.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=20mm,y=20mm]
  \draw
  (.5,0) node[left](A){$A$} -- (1,.8) node[left](B){B}
  (2,0) node[left](C){$C$} -- (1.5,1) node[right](D){D};  
   \pgfcoordinate{aux}{\pgfpointintersectionoflines
      {\pgfpointanchor{A}{east}}{\pgfpointanchor{B}{east}}
      {\pgfpointanchor{C}{east}}{\pgfpointanchor{D}{west}}}
   \pgfpathcircle{\pgfpointanchor{aux}{center}}{2pt}
  \pgfusepath{stroke}
  \draw (aux) -- (aux|-A.south);
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks, that is useful. But I had just realised, before seeing your answer, that I messed up a little: I should have let (A) refer to coordinates, not anchors. I.e., I should have used (.5,0) coordinate(A) node{$A$} and similarly for the other points. And then, all the \pgfpointanchor macros can be given {center} as their second argument. – Harald Hanche-Olsen Mar 3 at 17:29
  • @HaraldHanche-Olsen You haven't "messed up" I think.;-) – user121799 Mar 3 at 17:31
1

I did come up with another solution myself. It is not as nice as marmot's answer, but as it is a different approach to the same problem, possibly enabling a different set of options, I offer it here, for the record:

\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[x=20mm,y=20mm]
  \draw
  (.5,0) coordinate(A) node[left]{$A$} -- (1,.8) coordinate(B) node[left]{B}
  (2,0) coordinate(C) node[left]{$C$} -- (1.5,1) coordinate(D) node[right]{D};
  \tikzmath{
    coordinate \A; \A = (A);
    coordinate \B; \B = (B);
    coordinate \C; \C = (C);
    coordinate \D; \D = (D); }
  \pgfcoordinate{E}{
    \pgfpointintersectionoflines
      {\pgfpoint{\Ax}{\Ay}}{\pgfpoint{\Bx}{\By}}
      {\pgfpoint{\Cx}{\Cy}}{\pgfpoint{\Dx}{\Dy}}}
  \draw (E) circle[radius=2pt];
\end{tikzpicture}
\end{document}

In the process of getting there, I learned the difference between \pgfpoint (canvas coordinates?) and \pgfpointxy (user coordinates).

  • I knew you would go for \tikzmath ;-) – user121799 Mar 3 at 17:50

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.