1

In this code below

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture} 
\clip (-0.5,-0.75) rectangle (3.25,2.25); 
\foreach \pathname/\shift in {line/0cm, curve/2cm}{ \tikzset{xshift=\shift} 
\draw [->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
\draw [->, name path=line](0,-.5) -- (1,2) ; \fill [name intersections={of=line and curve,sort by=\pathname, name=i}] [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] \foreach \s in {1,2,3} {(i-\s) circle (2pt) node {\footnotesize\s}}; 
} 
\end{tikzpicture}
\end{document}

Here in case of \pathname line shift is set 0cm but why it is showing this result?enter image description here

And also i am a bit confused what actually the code means in this part

\foreach \pathname/\shift in {line/0cm, curve/2cm}{ \tikzset{xshift=\shift} 
\draw [->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
\draw [->, name path=line](0,-.5) -- (1,2) ; \fill [name intersections={of=line and curve,sort by=\pathname, name=i}] [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] \foreach \s in {1,2,3} {(i-\s) circle (2pt) node {\footnotesize\s}}; 
} 
  • Hi and welcome. What is your question ? Is this why there is a shift to the right? – AndréC Jun 20 at 19:48
  • The shift if for the curve not for the line – Soham Chatterjee Jun 20 at 19:52
2

This is the example under the documentation of option /tikz/intersection/sort by=<path name> in PGF manual.

Without any \foreach, the left side can be drawn by

\begin{tikzpicture} 
  \draw[->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
  \draw[->, name path=line] (0,-.5) -- (1,2); 
  \fill
    [name intersections={of=line and curve, sort by=line, name=i}]
    [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] 
    (i-1) circle (2pt) node {\footnotesize1} 
    (i-2) circle (2pt) node {\footnotesize2}
    (i-3) circle (2pt) node {\footnotesize3};
\end{tikzpicture}

With the inner \foreach, three circles can be drawn with

\fill[name intersections={..., name=i}] [...]
  \foreach \s in {1,2,3}
    { (i-\s) circle (2pt) node {\footnotesize\s} };

To show the effect of option sort by, this example needs to show how sort by=line and sort by=curve effects the numbering of intersections. This leads to the right side. In order to put two drawings side by side, the following structure is used:

% left side
\tikzset{xshift=0cm} % useless
\draw[..., name path=curve] ...
\draw[..., name path=line ] ...
\fill[name intersections{..., sort by =line}] ...

% right side
\tikzset{xshift=2cm} % shift every following drawings to right by 2cm
\draw[..., name path=curve] ...
\draw[..., name path=line ] ...
\fill[name intersections{..., sort by =curve}] ...

The similarity between left and right sides lead to the outer \foreach.

| improve this answer | |
  • Thanks now i understood the problem. Well explained – Soham Chatterjee Jun 20 at 21:29
2

The syntax of the foreach loop allows to iterate on several variables. Here, the loop iterates on two variables named \pathname and \shift. This latter variable defines the horizontal shift to the right through \tikzset{xshift=\shift}.

The name chosen in the loop {line/0cm, curve/2cm} is actually unrelated to the path names name path=curve and name path=line as shown in the following example where I modified these names: {lin/0cm, cur/1cm, lol/2cm}

screenshot

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture} 
%\clip (-0.5,-0.75) rectangle (3.25,2.25); 
\foreach \pathname/\shift in {bin/0cm, cur/1cm, lol/2cm, gag/3cm}{ \tikzset{xshift=\shift} 
\draw [->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
\draw [->, name path=line](0,-.5) -- (1,2) ; 
\fill [name intersections={of=line and curve,%sort by=\pathname,
 name=i}] [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] 
\foreach \s in {1,2,3} {(i-\s) circle (2pt) node {\footnotesize\s}}; 
} 
\end{tikzpicture}
\end{document}
| improve this answer | |

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.