The reason the code does not work as provided is that there is only one intersection, and so (intersection-2)
does not exist. One way to alleviate this kind of issue is to specify total=\t
to contain the total number of intersections and the use a foreach
to loop through each intersection:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[every node/.style={black,above right}]
\draw[name path=line 1] (0,0) -- (2,2);
\draw[name path=line 2] (2,0) -- (0,2);
\fill[red,name intersections={of=line 1 and line 2,total=\t}]
\foreach \s in {1,...,\t}{(intersection-\s) circle (2pt) node {\footnotesize\s}};
\end{tikzpicture}
\end{document}
Status-by-design.
When TikZ works out how much a path contributes to a picture, it does its best to ensure that the entire visible path is included in the resulting box. As the actual path is done by the renderer, it has to do some guesswork to figure out exactly how big the box should be. Moreover, it isn't always easy to compute the actual bounding box (bézier curves being a case in point). Once it has an idea of the bounding box for the actual path, that still might not be enough because the path is only the infinitesimal path and the visible path has width. Since these measurements are, by their nature, somewhat crude, PGF simply adds half the line width to each side of the bounding box to take into account this extra part.
One way to see this in action is to repeat each drawing with the additional option line cap=rect
. This extends the line by half the line width at each end, but doesn't change the bounding box because the potential for this extension has already been taken into account by the sizing mechanism.
If you want to force a particular bounding box you should specify it beforehand with the \useasboundingbox
command.
\documentclass[12pt]{article}
%\url{http://tex.stackexchange.com/q/130456/86}
\usepackage{tikz}
\begin{document}
\begin{tabular}{ll}
\fbox{\tikz[baseline=-0.5ex]\draw[thick](0,0)--(3ex,0);}&test\\
\fbox{\tikz[baseline=-0.5ex]\draw[thick,line cap=rect](0,0)--(3ex,0);}&test\\
\fbox{\tikz[baseline=-0.5ex]\draw[thick,double](0,0)--(3ex,0);}&test\\
\fbox{\tikz[baseline=-0.5ex]\draw[thick,double,line cap=rect](0,0)--(3ex,0);}&test\\
\fbox{\tikz[baseline=-0.5ex]\draw[thick,line width=2.2pt](0,0)--(3ex,0);}&test\\
\fbox{\tikz[baseline=-0.5ex]\draw[thick,line width=2.2pt,line cap=rect](0,0)--(3ex,0);}&test\\
\fbox{\tikz[baseline=-0.5ex]\draw[thick,double](0,0)--(3ex,0);}&test\\
\fbox{\tikz[baseline=-0.5ex]\draw[thick,double,line cap=rect](0,0)--(3ex,0);}&test\\
\end{tabular}
\end{document}
So while one could classify this as undesired behaviour, to get it absolutely right would take more computation than I suspect PGF wants to do and certainly more than most people want!
Update 2013-08-29 Here's a straightforward way to achieve what (I think) you want: that only coordinates are used for computing the bounding box and not line width. It uses the fact that the two parts that you want to separate are already separated in the code: the coordinates are processed as the path is built up, and as the path is built then the bounding box is continually adjusted accordingly, but the linewidth is added to the bounding box only when the path is used. So the following inserts some code in between the path being built and the path being used to turn off the computation of the bounding box. There is already a hook to insert code at the relevant point: the \tikz@addmode
command, and updating the bounding box is determined by the condition \ifpgf@relevantforpicturesize
.
\documentclass{article}
%\url{http://tex.stackexchange.com/q/130456/86}
\usepackage{tikz}
\makeatletter
\tikzset{
only coordinates are relevant/.is choice,
only coordinates are relevant/.default=true,
only coordinates are relevant/true/.code={%
\tikz@addmode{\pgf@relevantforpicturesizefalse}},
only coordinates are relevant/false/.code={%
\tikz@addmode{\pgf@relevantforpicturesizetrue}}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw[line width=1cm,red,only coordinates are relevant] (0,0) rectangle (5,5);
\draw (current bounding box.south east) rectangle (current bounding box.north west);
\end{tikzpicture}
\end{document}
Screenshot:
It might need a little tweaking to make it work with pre- and post-actions: I haven't tested it extensively.
Best Answer
A possible different approach might be to use a custom
to path
. The implementation below is very inflexible, relying on hard coded values. As such, this probably isn't a very good solution, though it might be of interest.All coordinates are found using the syntax of the
calc
library, described in section 13.5 Coordinate Calculations of the TikZ manual (for version 3.0.1a).