# Insert grid on tikz picture with standalone

I'd like to insert a grid to fill the whole picture. I'm trying to mimic a blueprint paper.

So, I have two questions:

1. Why the use of colored rectangle change the border around picture?
2. Why the grid is much bigger than the picture size? In some sense, the anchors are not working.

Edit: using the current bounding box as suggested by Phelype Oleinik, the grid is over the draw. But the space around produced by the background rectangle is out of the grid.

MWE

% !TeX program = pdflatex
\documentclass[border=0]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[
background rectangle/.style={fill=green},
show background rectangle, %% <-- insert space around
]
\draw (0,0) -- (3,4) -- (0,3) -- cycle;

\draw[white,step=.5cm]   (current bounding box.south west) grid (current bounding box.north east);
\end{tikzpicture}
\end{document}
• You could use \draw[white,step=.5cm] (current bounding box.south west) grid (current bounding box.north east);. That is, use the picture's current bounding box instead of the current page. Jun 30, 2018 at 17:14
• @PhelypeOleinik, nice!. In this case, the grid is over the picture, but not over the page. I supposed that the space around the picture created by the colored background was part of the picture, so should be included in the bounding box. Jun 30, 2018 at 17:26
• I think this happens because the backgrounds library defines the show background rectangle as \tikzstyle{show background rectangle}=[execute at end picture=\tikz@background@framed], which does the framing after the picture is created and the bounding box is set, that's why the grid does not span the outer rectangle. You could use something to enlarge the bounding box prior to the end of the picture, then draw the grid... Jun 30, 2018 at 17:41

backgrounds library offers two option show background rectangle and show background grid (or gridded) which can be combined to obtain the desired result.

Initially separation between figure and background rectangle is .5ex, but tight background forces it to 0pt.

If background rectangle is filled, background grid should be drawn after it.

% !TeX program = pdflatex
\documentclass[border=0]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\tikzset{%
mybackground/.style={%
tight background, % 0pt margins around figure
background rectangle/.style={fill=green},
background grid/.style={draw=white, step=2mm, ultra thin},
show background rectangle,
show background grid % after a filled background rectangle
}%
}%

\begin{tikzpicture}[mybackground]
\draw (0,0) -- (3,4) -- (0,3) -- cycle;
\end{tikzpicture}
\end{document}

The standalone class, by default, puts everything inside \begin{document}...\end{document} in an \hbox, then crops the page to the size of this \hbox. When you use the current page hook, the page is not yet cropped, so the full size is used.

Then, as I said in the comment, you can switch to the current bounding box hook, so the grid is drawn around the picture only, and the boxing of standalone is correct.

But then the grid does not span the whole page, just the picture because you used the show background rectangle option, which does:

\tikzstyle{show background rectangle}=[execute at end picture=\tikz@background@framed]

after the picture is created and the bounding box is set, so the current bounding box looks smaller than the "actual bounding box".

You can, however, tell TikZ to execute some piece of code after the picture is created. You just have to assure that the grid is drawn after the show background rectangle is executed:

% !TeX program = pdflatex
\documentclass[border=0]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}
\begin{document}
\tikzset{%
blueprint/.style={%
execute at end picture={%
\begin{scope}[on background layer]
\draw[white,step=.5cm] (current bounding box.south west) grid (current bounding box.north east);
\end{scope}
}%
}%
}%
\begin{tikzpicture}[%
inner frame sep = 0.5cm, % to make the grid match the border
show background rectangle,%% <-- insert space around
blueprint,% This MUST be after "show background rectangle"
background rectangle/.style={fill=green},
]
\draw (0,0) -- (3,4) -- (0,3) -- cycle;
\end{tikzpicture}
\end{document}

• Very nice and explanatory answer. Jun 30, 2018 at 18:54
• In this case, the grid will be over any other draw? Some of it lines are hiding other drawn lines in my case (not in the MWE). Jun 30, 2018 at 19:05
• @Sigur I got distracted by the rest and forgot about this :) You have to tell TikZ to draw the grid on the background as John did. I edited my answer to include this. Jun 30, 2018 at 19:09
• Oh, now it is ok. So, since the trick was the background layer, do you mind if I accept his answer? Also, it is short. Jun 30, 2018 at 19:23
• @Sigur No, I don't mind :), go for which best fits your needs. I'm no TikZ expert either, the fun part was figuring out what standalone did :). Note, however, that John's answer removes the border around the picture. The extra code I used was to keep the border on. Jun 30, 2018 at 19:30

Here is one solution.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (3,4) -- (0,3) -- cycle;

\begin{scope}[on background layer]
\fill[green]   (current bounding box.south west) rectangle (current bounding box.north east);
\draw[white,step=.5cm]   (current bounding box.south west) grid (current bounding box.north east);
\end{scope}
\end{tikzpicture}
\end{document}

• Interesting and simple solution. Jun 30, 2018 at 18:54