What is the meaning of option 'by' in TikZ Intersections

Just for completeness. You can name the intersections by C-1 etc. by just using name=C. What is perhaps also worth pointing out is that, if you want so sort the intersections along a straight line, then you have to draw the straight line pretending it is a curve.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
    \draw[name path=grid] [xstep=3,ystep=2] (9,8) grid (0,0);
    \draw[->, name path=line] (2,1) to[bend left=0] (7,7);
    \draw[name intersections={of=grid and line, sort by=line, name=C, total=\t}]
        \foreach \s in {1,...,\t}{(C-\s) node {\s}};
\end{tikzpicture}
\end{document}

enter image description here


The relevant line is

\path [name intersections={of=D and E, by={[label=above:$C$]C, [label=below:$C’$]C’}}];

Compare with this simpler version:

\path [name intersections={of=D and E, by={C, C’}}];

Here the intersection points are computed and named C and C' ("name the intersection points of D and E by the names C and C'").

It is shortcut for

\coordinate (C) at ...;
\coordinate (C') at ...;

for some computed coordinates.

Adding the optional styling [label=above:$C$]C is equivalent to

\coordinate[label=above:$C$] (C) at ...;

and allows you to style the intersection point directly. It would be equivalent, though longer, to write

\path [name intersections={of=D and E, by={C, C’}}];
\node[above] at (C) {$C$};
\node[below] at (C') {$C'$};

By default, intersections are named (intersection-1), (intersection-2), etc.

When you write by={a,b} the first two intersections will be called (a) and (b).

Let's look at the example on page 142, slightly modified. It displays the 9 intersections of two curves. The total number of intersections is given by total.

By writing by={a,b}, the first 2 intersections now have two names:

  • (a) or (intersection-1)
  • (b) or (intersection-2)

(a) is an alias of (intersection-1), the others do not have aliases and remain accessibles.

screenshot

\documentclass[border=5mm,tikz]{standalone}

\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\clip (-2,-2) rectangle (2,2);
\draw [name path=curve 1] (-2,-1) .. controls (8,-1) and (-8,1) .. (2,1);
\draw [name path=curve 2] (-1,-2) .. controls (-1,8) and (1,-8) .. (1,2);
\fill [name intersections={of=curve 1 and curve 2, by={a,b}, total=\t}]
[red, opacity=0.5, every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}{(intersection-\s) circle (2pt) node {\footnotesize\s}};
\draw[fill=blue!50,opacity=.5] (a) circle (4pt);
\end{tikzpicture}
\end{document}