TikZ add frame to a picture

There is also the background library (not sure which version of PDF/TikZ this arrived in, it's in PGF2.10). From the manual (section 25 in PGF2.10):

This library defines "backgrounds" for pictures. This does not refer to background pictures, but rather to frames drawn around and behind pictures.

It then gives various examples, from drawing a grid behind a picture to drawing a rectangle. In the simplest case, we can just supply the option framed to the tikzpicture environment to get a simple rectangular frame. Using the background rectangle style, we can make it a little more fancy (though as the manual says, no-one in their right mind would use this particular framing).

Code:

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[framed]
\draw (0,0) circle (2);
\draw (0,0) rectangle (3,2);
\end{tikzpicture}

\bigskip

\begin{tikzpicture}[framed,background rectangle/.style={double,ultra thick,draw=red, top color=blue, rounded corners}]
\draw (0,0) circle (2);
\draw (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document}

Picture:

framed tikzpicture

There are, of course, several options to change the background, in particular to change how far from the picture the frame is. See Section 25 of the manual (2.10 version) for details.


You can get the size of the current picture using the special rectangle node current bounding box. To frame the whole picture add the following code at the very end:

\draw (current bounding box.north east) rectangle (current bounding box.south west);

If you want to save this coordinates (which can also be used to calculate the height) see the question How can I save the bounding box of a TikZpicture and use in other TikZpicture.


The simplest would be to just use a framebox.

\fbox{%
  \begin{tikzpicture}
  % picture content
  \end{tikzpicture}
}

Tags:

Tikz Pgf