tikz: Ignore one path in bounding box calculation

You can actually use the key overlay on a single path:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw(0,0)-- (2,2);
\draw (current bounding box.south west)
             rectangle (current bounding box.north east);
\path[overlay] (-1,-1)--(5,5); %ignore this

\fill[red](0,1)rectangle (1,3);
\draw [yellow,ultra thick] (0,0)rectangle (2,3); %wanted bounding box 
\draw [blue,thick] (current bounding box.south west) 
             rectangle (current bounding box.north east);%current bounding box
\end{tikzpicture}
\end{document}

Produces:

overlay on a single path

The overlay key sets \pgf@relevantforpicturesizefalse. This is used when adjusting the global bounding box according to the path:

\def\pgf@protocolsizes#1#2{%
  \ifpgf@relevantforpicturesize%
    \ifdim#1<\pgf@picminx\global\pgf@picminx#1\fi%
    \ifdim#1>\pgf@picmaxx\global\pgf@picmaxx#1\fi%
    \ifdim#2<\pgf@picminy\global\pgf@picminy#2\fi%
    \ifdim#2>\pgf@picmaxy\global\pgf@picmaxy#2\fi%
    \ifpgf@size@hooked%
      \let\pgf@size@hook@x#1\let\pgf@size@hook@y#2\pgf@path@size@hook%
    \fi%
  \fi%
  \ifdim#1<\pgf@pathminx\global\pgf@pathminx#1\fi%
  \ifdim#1>\pgf@pathmaxx\global\pgf@pathmaxx#1\fi%
  \ifdim#2<\pgf@pathminy\global\pgf@pathminy#2\fi%
  \ifdim#2>\pgf@pathmaxy\global\pgf@pathmaxy#2\fi%
}

So from that we see that if the path is relevantforpicturesize then the picture bounding box is adjusted to include it, but if not then not. Thus whilst it is most usual to use this key globally on a picture (or scope), its effect is actually seen path by path and so it can be used on an individual path.


For such pauses of bounding box calculations, we have two options; the overlay option to the scope at the TikZ frontend and the pgfinterruptboundingbox environment at the basic layer.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw(0,0)-- (2,2);
\begin{scope}[overlay]
\draw (current bounding box.south west)
             rectangle (current bounding box.north east);
\path (-1,-1)--(5,5); %ignore this
\end{scope}

\fill[red](0,1)rectangle (1,3);
\draw [yellow,ultra thick] (0,0)rectangle (2,3); %wanted bounding box 
\draw [blue,thick] (current bounding box.south west) 
             rectangle (current bounding box.north east);%current bounding box
\end{tikzpicture}
\end{document}

or using

\begin{pgfinterruptboundingbox}
\draw (current bounding box.south west)
             rectangle (current bounding box.north east);
\path (-1,-1)--(5,5);
\end{pgfinterruptboundingbox}

both give the following result.

enter image description here

Tags:

Tikz Pgf