2

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.

enter image description hereenter image description here

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.

enter image description here

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}
  • 3
    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. – Phelype Oleinik Jun 30 '18 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. – Sigur Jun 30 '18 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... – Phelype Oleinik Jun 30 '18 at 17:41
5

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}

enter image description here

5

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}

enter image description here

  • Very nice and explanatory answer. – Sigur Jun 30 '18 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). – Sigur Jun 30 '18 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. – Phelype Oleinik Jun 30 '18 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. – Sigur Jun 30 '18 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. – Phelype Oleinik Jun 30 '18 at 19:30
4

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} 

demo

  • Interesting and simple solution. – Sigur Jun 30 '18 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.