31

I want to ignore one specific path for the bounding box calculation in the middle of a tikz picture (I need the path only to get an intersection point and had to make it quite long, to be sure that the point exists).

I came up with the following solution but I find it a bit odd-looking. Does something better exist?

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw(0,0)-- (2,2);

\begin{scope}
\path [use as bounding box] 
             (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 [black] (0,0)rectangle (2,3); %wanted bounding box 
\draw [blue] (current bounding box.south west) 
             rectangle (current bounding box.north east);%current bounding box
\end{tikzpicture}
\end{document} 
Ulrike Fischer
  • 327,261

2 Answers2

27

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.

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • Thanks. You are giving me a hard time to decide which answer to accept. percusse was faster and offers two solutions but you addressed the "specific path" in my question. I think I will throw a dice ;-). – Ulrike Fischer Dec 07 '12 at 08:49
26

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

percusse
  • 157,807
  • Thanks. You are giving me a hard time to decide which answer to accept. You were faster and offers two solutions but Andrew addressed the "specific path" in my question. I think I will throw a dice ;-). – Ulrike Fischer Dec 07 '12 at 08:50
  • @UlrikeFischer Haha, no problem at all. – percusse Dec 07 '12 at 10:47
  • @percusse Is there some similar command that will exclude some element of a TikZ image from extending the bounding box without being cropped if it exits the bounding box (as sketched here)? – Janosh Sep 12 '17 at 09:01
  • 1
    @Casimir You can add overlay key to most (not all) elements to be ignored from bounding box updates – percusse Sep 12 '17 at 09:18
  • @percusse I tried adding that to a node as in \node[overlay,draw,circle,minimum size=16pt,fill=white,postaction={pattern=north east lines},pin={260:$\Gamma_{k,ail}^{(3)}(q_1,\!p_1,\!p_4\!)$}] at (-\radius,0) {}; but to no effect. – Janosh Sep 12 '17 at 09:22
  • @Casimir This works without any problem adsfasdgsdfg\tikz{\node[overlay,draw,circle,minimum size=16pt,fill=white,pattern=north east lines]{};\draw(1,0) rectangle (2,1);}adfgadfgdsfg – percusse Sep 12 '17 at 09:41
  • @percusse Yes, but if you readd the pin={260:$\Gamma_{k,ail}^{(3)}(q_1,\!p_1,\!p_4\!)$} it fails. And I really need that pin. – Janosh Sep 13 '17 at 11:32
  • @Casimir try pin{[overlay].....} – percusse Sep 13 '17 at 12:27
  • @percusse Unfortunately, while this excludes the pins from extending the bounding box, it also crops any part of the pins that falls outside the bounding box. – Janosh Sep 13 '17 at 12:31
  • @Casimir Then I'm out of ideas. Care to open a question? – percusse Sep 13 '17 at 12:38
  • @percusse See here. – Janosh Sep 13 '17 at 12:53