7

I aimed at drawing a square (using TikZ) with a red dot inside it, placed on the intersection of its diagonals:

enter image description here

And while the following method worked fine:

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0); 
\coordinate (b) at (1,1);
\coordinate (c) at (1,0); 
\coordinate (d) at (0,1);
\coordinate (i) at (intersection of a--b and c--d);
\fill[red] (i) circle (2pt);
\draw (0,0) rectangle (1,1);
\end{tikzpicture}
\end{document}

when I tried this solution which does not involve specification of the coordinates:

\begin{tikzpicture}
\coordinate (i) at (intersection of {(0,0)--(1,1)} and {(1,0)--(0,1)});
\fill[red] (i) circle (2pt);
\draw (0,0) rectangle (1,1);
\end{tikzpicture}

the result was:

enter image description here

So I tried modifying the code to this:

\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\coordinate (i) at (intersection of {(0,0)--(1,1)} and {(1,0)--(0,1)});
\fill[red] (i) circle (2pt);
\end{tikzpicture}

which resulted in the following error message:

! Package PGF Math Error: You asked me to calculate `1/0.0', but I cannot divide any number by zero.

And here's the question - I do not understand why the codes above behave like that. What is the difference between computing the intersection point via defined coordinates and via points on the plane directly. Could you please explain this to me?

  • 5
    You have to much parentheses, \coordinate (i) at (intersection of {0,0--1,1} and {1,0--0,1}); works fine for me. – Ulrike Fischer Feb 01 '16 at 09:01
  • 2
    @MadHatter It looks for stripped coordinates in the form of {#1--#2} – percusse Feb 01 '16 at 09:10
  • @percusse Yes, now I see it. I failed to notice that in the first - properly working code - there are no parentheses around defined coordinates. Thanks one more time! – Rafał Gruszczyński Feb 01 '16 at 09:13
  • 1
    @percusse: Is this use of intersection of X and Y documented somewhere? I can find the documentation of the library and named path but nothing else. – Ulrike Fischer Feb 01 '16 at 09:14
  • @UlrikeFischer Probably not. there is this example in the key syntax though: \coordinate (X) at (intersection cs:first line={(A)--(B)}, second line={(E)--(F)}); – percusse Feb 01 '16 at 09:19
  • 1
    @UlrikeFischer I don't find it documented in TiKZ 3.0, but I keep a printed manual for v1.18 (previous to intersections library). With reference to intersections cs there's an example with \fill[red] (intersection cs: first line={(A)--(B)}, second line={(1,2)--(3,0)}) circle (2pt); but also says: "The implicit way of specifying this coordinate system is to write (intersection of <p1>--<p2> and <q1>--<q2>). Note that there are no parentheses around the pi and qi. Thus, you would write (intersection of A--B and 1,2--3,0). – Ignasi Feb 01 '16 at 09:48
  • Please don't use minimal for examples.... – cfr Feb 02 '16 at 02:32

3 Answers3

6

Although syntax used by Mad Hatter is still valid, it's and old syntax which is not anymore documented in TikZ 3.0. So, for completion, the a la TikZ 3.0 way to obtain similar results could be:

  1. load intersections library
  2. Declare two named paths: \path[name path=ac] (0,0)--(1,1); ...
  3. Act on intersections of named paths: name intersections={of=ac and bd}

More information about intersections library in 13.3.2 Intersections of arbitrary paths.

Complete code:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\path[name path=ac] (0,0)--(1,1); 
\path[name path=bd] (1,0)--(0,1); 
\fill [red, name intersections={of=ac and bd}] (intersection-1) circle (2pt);
\draw (0,0) rectangle (1,1);
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
3

As per Ulrike's comment.

MWE

\documentclass[convert]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0); 
\coordinate (b) at (1,1);
\coordinate (c) at (1,0); 
\coordinate (d) at (0,1);
\coordinate (i) at (intersection of {0,0--1,1} and {1,0--0,1});
\fill[red] (i) circle (2pt);
\draw (0,0) rectangle (1,1);
\end{tikzpicture}
\end{document}

Result

enter image description here

Roald
  • 1,245
0

@UlrikeFischer -- @Ignasi -- Would this code be counted as okay

enter image description here

\documentclass[12pt]{article}
\usepackage{tikz}
%\usetikzlibrary{intersections}

\begin{document} \begin{tikzpicture} \path[] (0,0)--(1,1) node (A) [circle,fill, red,inner sep=0pt, pos=0.5,minimum size=4pt,text width=1pt] {}; \path[] (1,0)--(0,1); %\fill [red, pos=0.5] (path=ac) circle (2pt); \draw (0,0) rectangle (1,1); \end{tikzpicture} \end{document}

js bibra
  • 21,280