PGF/TikZ Manual

# The TikZ and PGF Packages Manual for version 3.1.10

## Libraries

#### 45Background Library

• TikZ Library backgrounds

• \usetikzlibrary{backgrounds} % and plain
\usetikzlibrary[backgrounds] % Cont

This library defines “backgrounds” for pictures. This does not refer to background pictures, but rather to frames drawn around and behind pictures. For example, this package allows you to just add the framed option to a picture to get a rectangular box around your picture or gridded to put a grid behind your picture.

The first use of this library is to make the following key available:

• /tikz/on background layer=options(no default)

• This key can (only) be used with a {scope} or \scoped. It will cause everything inside the scope to be typeset on a background layer.

The options will be executed inside background scope. This is useful since other options passed to the {scope} environment will be executed before the actual background material starts and, thus, will have no effect on it.

A scope with this option set should not be “deeply nested” inside the picture since changes to the graphic state (like the color or the transformation matrix) “do not survive a layer switch”, see also Section 113 for details. In particular, setting, say, the line width at the beginning of a picture will not have an effect on the background picture.

For this reason, it may be useful to setup the following style:

• /tikz/every on background layer(style, no value)

• This style is executed at the beginning of each background layer. If you have a global setup in every picture, you should consider putting that part of it that concerns the graphics state into this style.

When this package is loaded, the following styles become available:

• /tikz/show background rectangle(style, no value)

• This style causes a rectangle to be drawn behind your graphic. This style option must be given to the {tikzpicture} environment or to the \tikz command.

The size of the background rectangle is determined as follows: We start with the bounding box of the picture. Then, a certain separator distance is added on the sides. This distance can be different for the $$x$$- and $$y$$-directions and can be set using the following options:

• /tikz/inner frame xsep=dimension (no default, initially 1ex)

• Sets the additional horizontal separator distance for the background rectangle.

• /tikz/inner frame ysep=dimension (no default, initially 1ex)

• Same for the vertical separator distance.

• /tikz/inner frame sep=dimension(no default)

• Sets the horizontal and vertical separator distances simultaneously.

The following two styles make setting the inner separator a bit easier to remember:

• /tikz/tight background(style, no value)

• Sets the inner frame separator to 0pt. The background rectangle will have the size of the bounding box.

• /tikz/loose background(style, no value)

• Sets the inner frame separator to 2ex.

You can influence how the background rectangle is rendered by setting the following style:

• /tikz/background rectangle(style, initially draw)

• This style dictates how the background rectangle is drawn or filled. The default setting causes the path of the background rectangle to be drawn in the usual way. Setting this style to, say, fill=blue!20 causes a light blue background to be added to the picture. You can also use more fancy settings as shown in the following example:

Naturally, no one in their right mind would use the above, but here is a nice background:

• /tikz/framed(style, no value)

• This is a shorthand for show background rectangle.

• /tikz/show background grid(style, no value)

• This style behaves similarly to the show background rectangle style, but it will not use a rectangle path, but a grid. The lower left and upper right corner of the grid is computed in the same way as for the background rectangle:

You can influence the background grid by setting the following style:

• /tikz/background grid(style, initially draw,help lines)

• This style dictates how the background grid path is drawn.

This option can be combined with the framed option (use the framed option first):

• /tikz/gridded(style, no value)

• This is a shorthand for show background grid.

• /tikz/show background top(style, no value)

• This style causes a single line to be drawn at the top of the background rectangle. Normally, the line coincides exactly with the top line of the background rectangle:

The following option allows you to lengthen (or shorten) the line:

• /tikz/outer frame xsep=dimension (no default, initially 0pt)

• The dimension is added at the left and right side of the line.

• /tikz/outer frame ysep=dimension (no default, initially 0pt)

• This option does not apply to the top line, but to the left and right lines, see below.

• /tikz/outer frame sep=dimension(no default)

• Sets both the $$x$$- and $$y$$-separation.

You can influence how the line is drawn grid by setting the following style:

• /tikz/background top(style, initially draw)

• /tikz/show background bottom(style, no value)

• Works like the style for the top line.

• /tikz/show background left(style, no value)

• Works similarly.

• /tikz/show background right(style, no value)

• Works similarly.