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

Pg 65 of the PGF Manual, shows different operations with the intersections library in TikZ. But I am not able to understand the code. What is the meaning of the option by here.

The explanation given is:

The name intersections takes an optional argument by, which lets you specify names for the coordinates and options for them. This creates more compact code.

This is not clear.

• forget the label and read as "name intersection of D and E by C"... So, C is the name of the intersection of D and E paths. – koleygr Mar 7 at 15:37
• If you do not use the by option, then name of the intersection will be (intersection-1) etc. by=... lets you name the intersection in a more convenient manner. – daleif Mar 7 at 15:37
• @koleygr do you have any examples of the and options for them part? – daleif Mar 7 at 15:38
• @daleif I don't... just tried to give a basic answer to the question but I don't use it often... Do you think that I should delete my comment? Seems somehow clear to me but I am not sure about that – koleygr Mar 7 at 15:41
• \fill [name intersections={of=curve 1 and curve 2, by={a,b,c,d}}] if exist four intersections, or \fill [name intersections={of=curve 1 and curve 2, name=i, total=\t}] . see tikz \& pgf manual, page 142 (version 3.1). – Zarko Mar 7 at 15:50

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.

\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}

• I was looking for a way to name them like "A\i, total=\t" (by using the answer here: tex.stackexchange.com/a/31399/120578) but I couldn't find a way about that "\i" counter (This would improve your answer very much) – koleygr Mar 7 at 16:12
• I didn't quite understand what you meant. Can you be more explicit? – AndréC Mar 7 at 16:15
• I mean that it would be more useful if we could name all the intersections by using the internal counter of the tikz when finding them: the 1 of (intersection-1), the 2 of (intersection-2) and so on. This way if we could name that counter "\i" and could use it while giving name with "by={C\i}" (instead of "by={C1,C2,...,C\t}" where many times \t is unknown) we could then use the points as C1, C2, ... C\t... And this would be much better... – koleygr Mar 7 at 16:20
• This is a request for code improvement to TikZ developers, at our TikZ user level the only thing we can do is follow the syntax ... – AndréC Mar 7 at 16:24
• I said I was looking for something like this and I am not sure (yet) if this can't be done with the existing TikZ code... this is why I was looking to find some similar syntax like this request... But if not I would also agree that this would be a useful feature request.... Anyway you already have my +1... – koleygr Mar 7 at 16:31

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}


• Wow! Thanks! Didn't knew that this works like this! Thanks (+1). – koleygr Mar 7 at 17:10
• Nice explanation, but now we have a compound word, sort by! :P. – manooooh Mar 7 at 18:44
• @manooooh As long as the user doesn't use \bye I guess it is fine. ;-) – marmot Mar 7 at 18:46
• @marmot how does sort by work in this case. Also sort by=line is only for the intersections on the line and what is the order of sorting ( ascending or descending ) – subham soni Mar 8 at 1:07
• @subhamsoni Yes, intersections are always between two paths, and you can order along one of them. The slight subtlety is that, if you order along a line that is drawn with \draw (X) -- (Y);, sorting may not always work. And yes, sorting is ascending (which is why I added the arrow head in the example). – marmot Mar 8 at 1:14