# Easy border for tikz and forest

Using \fbox around forest creates unwanted horizontal paddings to my graphs:

\documentclass{scrbook}

\usepackage{tikz}
\usepackage[linguistics]{forest}

\begin{document}
\begin{figure}[bth]
\fboxsep = 4pt % Does not matter
\fbox{\begin{forest}
[A [B [D [I] [J] [K] ]
[E [L] [M] ] ]
[C [F] [G] [H] ] ]
\end{forest}}
\end{figure}
\end{document}


Removing \fbox aligns the diagram properly. However, I cannot get TikZ's background library to work with forest.

Using

\usetikzlibrary{backgrounds}
% ...
\tikzset{every picture/.style={show background rectangle}}


fails with the error "Dimension too large".

I now use TikZ nodes to create a frame around the forest environment:

\begin{forest}
tikz+={\draw
(current bounding box.south west) rectangle (current bounding box.north east);
},
[A [B [D [I] [J] [K] ]
[E [L] [M] ] ]
[C [F] [G] [H] ] ]
\end{forest}


This works fine, but seems way too complicated and not very clean, especially when a custom inner padding is required (I solved this using xshift/yshift on the rectangle).

Are there better ways to achieve a frame around every TikZ and forest image?

Update: Working solution for all forest environments

This solution from the comments creates the desired frame for all forest environments:

\forestset{default preamble={tikz+={\tikzset{show background rectangle}}}}


Of course, \tikzset{every picture/.style={show background rectangle}} still results in the above mentioned error, requiring show background rectangle being set in every picture.

Update 2: Bugfixes as of 2019-07-29:

• With your third solution you can just use \forestset{default preamble={tikz+={\tikzset{show background rectangle}}}} to make every tree boxed. That seems like the simplest solution. BTW, the warning not to use \fbox with TikZ is about using \fbox inside a TikZ node, not about wrapping a TikZ picture in an \fbox. – Alan Munn Jul 24 '19 at 15:40
• The problem is not \fbox. Add a A just before your forest environment and you get an unwanted horizontal padding... – Paul Gaborit Jul 24 '19 at 16:52
• The "Dimension too large" error occurs because forest typesets each node separately in a tikzpicture environment (to measure it), but defers its positioning (see 106.2.3 Deferred Node Positioning in PGF manual 3.1.3) so that the node's picture is empty. As you can check, an empty tikzpicture environment produces the same error. I will address this problem in the next release of forest by locally resetting every picture, so that it will apply only to the entire tree. – Sašo Živanović Jul 25 '19 at 8:37