[Tex/LaTex] Missing TikZ grid borders

gridstikz-pgf

As in here, the code

\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);

gives me a nice finite 4×4 grid. But when I choose a different starting point, like

\draw[step=0.5cm,color=gray] (2,2) grid (4,4);

then the left and the bottom border of the grid disappears. What is the problem and how can I fix it so that I have all the borders? I know I can draw a rectangle there, but I assume there is some cleaner solution.

Best Answer

In the pgfmanual it says on p. 145 that

due to rounding errors, the "last" lines of a grid may be omitted. In this case, you have to add an epsilon to the corner points

In this case, you will have to subtract an epsilon, i.e. a very small value, from the corner point. Something like

\draw[step=0.5cm,color=gray] (2-0.001,2-0.001) grid (4,4);

will work without any visible artefacts.