The code adds some completely useless invisible (or rather white) stuff. The lines
\clip(0pt,403pt) -- (389.957pt,403pt) -- (389.957pt,99.6166pt) -- (0pt,99.6166pt) -- (0pt,403pt);
\color[rgb]{1,1,1}
\fill(3.76406pt,399.236pt) -- (380.923pt,399.236pt) -- (380.923pt,253.19pt) -- (3.76406pt,253.19pt) -- (3.76406pt,399.236pt);
\fill(53.4497pt,394.719pt) -- (374.901pt,394.719pt) -- (374.901pt,289.325pt) -- (53.4497pt,289.325pt) -- (53.4497pt,394.719pt);
draw a white background that is larger than the actual picture. TikZ sees that and thinks it is part of the picture. Simply removing/uncommenting these lines removes most of the whitespace.
Near the end of the first scope,
\color[rgb]{1,1,1}
\fill(3.76406pt,249.426pt) -- (386.193pt,249.426pt) -- (386.193pt,103.381pt) -- (3.76406pt,103.381pt) -- (3.76406pt,249.426pt);
does the same.
Additionally (near the end of the second scope
),
\pgftext[center, base, at={\pgfpoint{220.95pt}{106.392pt}}]{\sffamily\fontsize{9}{0}\selectfont{\textbf{ }}}
adds a blank node below the picture, again enlarging the bounding box.
Removing all those lines gives a tight bounding box.
As far as I know, TikZ cannot do the cropping for you, as it can't know whether the white stuff is intentional or not (there might for example be a dark background behind the image so that white is visible).
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}
Best Answer
OK, quick and dirty, so not sure how robust this is, but by using the
calc
library (section 13.5 "Coordinate Calculations" in the 2.10 PGF Manual) and tying things up using thestyle args
key handle (section 55.4.4 "Defining Styles"), it at least goes part of the way to showing how it could be done.In fact, we could go further and tie a lot of stuff up in the
connect
style. This involves the use of\pgfextra
(section 14.18 "The PGF-Extra Operation") and thepgfinterruptpath
environment (section 69.3.2 "Graphic Scope Environments").The result is the same as before.