Draw an automatic "bounding box"?
For a basic solution, you can use (which would disallow verbatim content in nodes):
\framebox[\textwidth]{<TikZ picture>}
For a TikZ solution the backgrounds
library already offers a show background rectangle
key (alias: framed
). With a proper inner frame xsep
setting we can accomplish a “framebox” very easily. As the frame actually adds more width (half the linewidth on both sides), we need to slightly change the definition of the background path.
Obviously, we would want to add this correction directly to the calculation of the inner frame xsep
value but we couldn’t guarantee that it picks up the correct \pgflinewidth
(as the calculation is done outside of the path). We could obviously hard-code it (related reference: [1]) but then we couldn’t use different line-width for different frames (which we probably won’t (and shouldn’t), though).
Alternatively, protruding half the linewidth into the margins with
background rectangle/.append code={\tikz@addmode{\pgf@relevantforpicturesizefalse}}
Code
\documentclass{scrartcl}
\usepackage{tikz}
\usepackage[showframe,pass]{geometry}
\usetikzlibrary{backgrounds}
\makeatletter
\tikzset{inner frame xsep=.5\textwidth-.5\pgf@picmaxx+.5\pgf@picminx}
\def\tikz@background@framed{% just overwriting the original definition
\tikz@background@save%
\pgfonlayer{background}
\path[style=background rectangle] (\tikz@bg@minx+.5\pgflinewidth,\tikz@bg@miny) rectangle (\tikz@[email protected]\pgflinewidth,\tikz@bg@maxy);
\endpgfonlayer}
\makeatother
\newcommand*\arbitrarycomplexTikZobject[1][]{
\begin{tikzpicture}[#1] % could be an arbitrary complex Tikz object
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
\end{tikzpicture}}
\begin{document}
Space.
\begin{figure}[ht]
\framebox[\textwidth]{\arbitrarycomplexTikZobject}
\caption{Frame with \texttt{\string\framebox}.}
\end{figure}
\tikzset{every picture/.append style={framed}}
\begin{figure}[ht]
\arbitrarycomplexTikZobject
\caption{Frame with the \texttt{framed} option and a nice \texttt{inner frame xsep} setting.}
\end{figure}
\begin{figure}[ht]
\arbitrarycomplexTikZobject[background rectangle/.append style={ultra thick,draw=red,top color=blue,rounded corners}]
\caption{Frame with the \texttt{framed} option, a nice \texttt{inner frame xsep} setting and a badass frame which is taken from the Ti\emph{k}Z manual but without the \texttt{double} option.}
\end{figure}
\end{document}
Output
Another option to frame your figures is tcolorbox
. By default it adjusts box width to \linewidth
.
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{blindtext}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\begin{document}
\blindtext
\begin{figure}[h]
\begin{tcolorbox}\centering
\begin{tikzpicture}
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
\end{tikzpicture}
\end{tcolorbox}
\caption{\texttt{tcolorbox} example}
\end{figure}
\begin{figure}[h]
\begin{tcolorbox}[beamer,colback=red!5!white,colframe=red!75!black,]\centering
\begin{tikzpicture}
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
\end{tikzpicture}
\end{tcolorbox}
\caption{\texttt{tcolorbox} example}
\end{figure}
\end{document}