23

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} 
  • I knew that something more sensible exist somewhere ;-) – Ulrike Fischer Dec 6 '12 at 19:21
20

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

  • 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 7 '12 at 8:50
  • @UlrikeFischer Haha, no problem at all. – percusse Dec 7 '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)? – Casimir Sep 12 '17 at 9:01
  • @Casimir You can add overlay key to most (not all) elements to be ignored from bounding box updates – percusse Sep 12 '17 at 9: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. – Casimir Sep 12 '17 at 9:22
21

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.

  • 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 7 '12 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.