# TikZ: local bounding box incompatible with use as bounding box?

The TikZ/PGF documentation (v 2.10) contains the following example for the use of local bounding box:

\documentclass{article}
\usepackage{tikz}
\usepgfmodule{shapes}
\usetikzlibrary{scopes}

\begin{document}

\begin{tikzpicture}
\draw [help lines] (0,0) grid (3,2);
{ [local bounding box=outer box]
\draw (1,1) circle (.5) [local bounding box=inner box] (2,2) circle (.5);
}
\draw (outer box.south west) rectangle (outer box.north east);
\draw[fill,red] (inner box.south west) rectangle (inner box.north east);
\end{tikzpicture}

\end{document}


– actually just the tikzpicture, I made it into a complete document. This works as advertised until I explicitly set a global bounding box:

\documentclass{article}
\usepackage{tikz}
\usepgfmodule{shapes}
\usetikzlibrary{scopes}

\begin{document}

\begin{tikzpicture}
\path[use as bounding box] (-1,-1) rectangle (4,3);  % <------------
\draw [help lines] (0,0) grid (3,2);
{ [local bounding box=outer box]
\draw (1,1) circle (.5) [local bounding box=inner box] (2,2) circle (.5);
}
\draw (outer box.south west) rectangle (outer box.north east);
\draw[fill,red] (inner box.south west) rectangle (inner box.north east);
\end{tikzpicture}

\end{document}


– then the two drawn outlines of outer box and inner box disappear. Changing \draw to \fill indicates that the boxes have become huge and cover the whole page and more.

My question: How do I get this to work? Is this a known bug with a workaround?

My goal in using local bounding box is to measure the width and height of part of a TikZ figure using Caramdir's approach. In case local bounding box is broken, what else can I do?

-
Try putting \usetikzlibrary{scopes} into the preamble. –  Mark Wibrow Nov 16 '13 at 16:29
That removes the need for an explicit scope, but doesn't address my main problem. I'll remove the part about {} not working from the question, since apparently the manual example assumed to use this library. Thanks! –  A. Donda Nov 16 '13 at 16:32
I found a simple workaround: use a clipping path instead of use as bounding box. Not the same, but sufficient for my purposes. I'd still like to know why use as bounding box interferes with local bounding box. –  A. Donda Nov 16 '13 at 17:54

From the documentation of /tikz/use as bounding box:

Normally, when this option is given on a path, the bounding box of the present path is used to determine the size of the picture and the size of all subsequent paths are ignored.

This is why you can't use local bounding box after it.

But what you can do is to reset the bounding box after that, like this :

\pgfresetboundingbox
\path[use as bounding box] (-1,-1) rectangle (4,3);


Here is the complete code:

\documentclass[varwidth,border=10,convert]{standalone}
\usepackage{tikz}

\begin{document}
before
\begin{tikzpicture}
\draw [help lines] (0,0) grid (3,2);
\scoped[local bounding box=outer box]
\draw (1,1) circle (.5) [local bounding box=inner box] (2,2) circle (.5);
\draw (outer box.south west) rectangle (outer box.north east);
\draw[red] (inner box.south west) rectangle (inner box.north east);
\pgfresetboundingbox
\path[use as bounding box] (-1,-1) rectangle (4,3);
\end{tikzpicture}
after
\end{document}


-