6

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.

enter image description 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.

subham soni
  • 9,673
  • 1
    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 07 '19 at 15:37
  • 1
    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 07 '19 at 15:37
  • @koleygr do you have any examples of the and options for them part? – daleif Mar 07 '19 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 07 '19 at 15:41
  • @daleif - Here because we have two insecting points, above and below make sense. What if there are more than 2 intersection points, how will the label work in that case. – subham soni Mar 07 '19 at 15:41
  • 3
    \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 07 '19 at 15:50
  • @koleygr no leave it, I was just wondering, I've never seen options for by – daleif Mar 07 '19 at 16:14

3 Answers3

6

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'$};
6

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

  • 1
    Wow! Thanks! Didn't knew that this works like this! Thanks (+1). – koleygr Mar 07 '19 at 17:10
  • Nice explanation, but now we have a compound word, sort by! :P. – manooooh Mar 07 '19 at 18:44
  • 1
    @manooooh As long as the user doesn't use \bye I guess it is fine. ;-) –  Mar 07 '19 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 08 '19 at 01: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). –  Mar 08 '19 at 01:14
5

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}
AndréC
  • 24,137
  • I was looking for a way to name them like "A\i, total=\t" (by using the answer here: https://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 07 '19 at 16:12
  • I didn't quite understand what you meant. Can you be more explicit? – AndréC Mar 07 '19 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 07 '19 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 07 '19 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 07 '19 at 16:31
  • @koleygr It is not particularly difficult to implement this. –  Mar 07 '19 at 16:32
  • @marmot... Yes, we can do it... but I think TikZ should make it feature because if I try to implement it tikz will look for the intersections twice (one to find \t and one to use it in our naming list)... if it is inside their internal code things will be much simpler. – koleygr Mar 07 '19 at 16:36
  • @koleygr if I understand correctly, you want Tikz to count the number of intersections automatically which is not present in TikZ at the moment. Right? – subham soni Mar 07 '19 at 16:38
  • @subhamsoni: No. I would like a way to use this "by" and for the given number (\t) of this intersections and name at once a list (lets call it list C) that will contain C1, C2, ..., C\t.... without the need of using a loop to do it myself with the names (intersection-1), (intersection-2), ..., (intersection-\t)... – koleygr Mar 07 '19 at 16:42
  • @koleygr ah like an array . Got it – subham soni Mar 07 '19 at 16:45
  • I would call it just a "list" since it will be an "single dimensional array"... But: Yes! This would be the answer that would fit to your nice question... – koleygr Mar 07 '19 at 16:48
  • @koleygr You only have to say name=C. I added an answer because of a potential pitfall. –  Mar 07 '19 at 16:54
  • @marmot He doesn't want a dash between the C and the counter, it complicates everything! – AndréC Mar 07 '19 at 16:55
  • Let @koleygr decide what they want. And stop using exclamation marks when communicating with users!!!!!!!!!!! –  Mar 07 '19 at 16:58
  • His question was clearly asked: he wants to write C\i and not C-\i. What bothers you about the exclamations? – AndréC Mar 07 '19 at 17:00
  • @AndréC... 'May be this discussion seems funny to you... But: Why you didn't named C your intersections but you gave just names "a" and "b" (just the two first of 9) and then you had to use (intersection-\s) and (a) separately instead of (C-\s) and (C-1)... This seems funny to me too... I didn't knew that... But if you did... then your code is too complicated for your knowledge (without exclamation mark). – koleygr Mar 07 '19 at 17:26
  • @koleygr Why would that seem funny to me? I have tried to understand your request, as have the others. The absence of a dash between the C and the counter \i is a change in TikZ syntax. That's why I said that at my level, there was nothing I could do. As for the exclamation point, I don't know what the problem is, if someone could explain it to me, it would help. – AndréC Mar 07 '19 at 17:36
  • No problem AndréC... The comment "He doesn't want a dash between the C and the counter, it complicates everything!" seems ιronical... the last sentence can't be read as a serious conclusion of a missing dash ... and the exclamation mark can convince someone that read the whole comment as a serious comment that it was not so serious. As per your level, I have never read the manual of tikz and so I am sure you know more than me, but in this answer you just didn't use your knowledge on this "dash thing" as you could use it (I was trying to implement @marmot's answer and could not until yours.) – koleygr Mar 07 '19 at 17:45
  • We could delete these comments from here now because they don't really offer something except of confusion... marmot's answer gives everything that was not clear to me and is described here...\ – koleygr Mar 07 '19 at 17:50
  • @koleygr I spend a lot of time trying to understand the manual with the help of machine translators as well as trying to understand each other's questions and answers with the same machine translator. The syntax given by marmot in his answer is located on page 142 of manual 3.1.1 as well as that of by={a,b}. If I had understood your request in its semantics and not in its syntax, it would have avoided any misunderstanding. Nevertheless, I still don't know why exclamation marks are disturbing.

    Translated with www.DeepL.com/Translator

    – AndréC Mar 07 '19 at 17:53
  • No problem @AndréC... I don't really pay attention to ironic comments neither looking to find if someone used irony towards something I said etc... I don't care and usually don't understand irony too... My English are bad too and thus we have to be more careful when communicating online with people that can or can't use English better than us. I just explained about the "Funny"... about the "!" and about my confusion. But still think we should remove all these comments from here... Starting to delete whenever you agree to delete them – koleygr Mar 07 '19 at 18:05