Buildup
Basic setting
Using the intersection library, I encountered some behaviour of path names that I do not understand. The following works in the intended way:
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[name path=pos] (0, -1) -- (2, 1);
\draw[name path=neg] (0, 1) -- (2, -1);
\fill[name intersections={of=pos and neg}] (intersection-1) circle (2pt);
\end{tikzpicture}
\end{document}
Looping into a problem
But I have multiple paths to intersect, so lets change it up a bit:
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[name path=pos] (0, -1) -- (2, 1);
% Only change: A senseless “loop”
\foreach \i in {0} {
\draw[name path=neg] (0, 1) -- (2, -1);
}
\fill[name intersections={of=pos and neg}] (intersection-1) circle (2pt);
\end{tikzpicture}
\end{document}
This looks to me as if it should be completely equivalent, but now I get error messages:
Package tikz Error: I do not know the path named `neg'. Perhaps you misspelt it. ...[red, name intersections={of=pos and neg}]
Package pgf Error: No shape named intersection-1 is known. ...ections={of=pos and neg}] (intersection-1)
The circle is drawn anyway,
since pgf simply assumes the coordinates of (intersection-1)
to be (0, 0)
,
but this was not the behaviour I expected or hoped for.
A strange workaround
By drawing anything afterwards, we can get it to run smoothly again:
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw[name path=pos] (0, -1) -- (2, 1);
\foreach \i in {0} {
\draw[name path=neg] (0, 1) -- (2, -1);
}
% Only change: Senseless path of vanishing length
\path (5, -1);
\fill[name intersections={of=pos and neg}] (intersection-1) circle (2pt);
\end{tikzpicture}
\end{document}
Question
I would like to know why this happens.
Solutions and workarounds are of interest, too, but might be better suited as answers to this similar question, even though the existing answers are mostly concerned with the expansion of options.
Non-Explanations
As the third variant works without any problems,
there is no general problem exporting path names out of loops,
even though using name path global
provides another workaround.
I did not encounter the same behaviour when declaring nodes, so this seems to be a rather specific problem of the intersection library.
libraries/tikzlibraryintersections.code.tex
. There you can find all sorts ofFIXME
statements and questions whether or not one needs to reset the path. Once you will have the choice to either rewrite this library or just to usename path global
, which worked perfectly fine in all situations I needed it so far.\path (5, -1);
into the loop, the error reappears.