# Displaced lines in grid with tikzpicture

I am having troble making a grid with tikzpicture. The vertical and horizontal lines are somewhat displaced. This is similar to TikZ not drawing some lines in grid. I have been fiddling with adding small amounts ("epsilons") to the corner points but to no avail. This is my code:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

%% Rulers -------------------------
\draw [black!80, dotted] (0, 0) -- (9, 0);
\draw [black!80, dotted] (0, 4) -- (9, 4);
\draw (-0.25, 0) -- (.25, 0);
\draw (-0.25, 4) -- (.25, 4);
\draw [black!50, thick, dashed] (0, .5) -- (1.5, .5);
\draw [black!50, thick, dashed] (0, .25) -- (3.25, .25);
\node (A) at (0,2) {$\Delta r$};
\draw[->] (A) -- (0,4);
\draw[->] (A) -- (0,0);

% First and second grids
\draw[step=1,black,thin] (1,0) grid (2,4);
\draw[step=0.5, black,thin] (2.99,0) grid (3.5,4);
% cell centers
\foreach \r in {.5, 1.5,..., 3.5}
\fill [] (1.5, \r)  circle [radius=1pt];
\foreach \r in {.25, .75,..., 3.75}
\fill [] (3.25, \r)  circle [radius=1pt];

% -----------------------------------------------
% Third anf fourth grids
\draw[step=4/3,black,thin] (5-2/3,-2/3) grid (5+2/3,4+2/3);
\draw[step=2/3,black,thin] (6+2/3,-1/3) grid (7+1/3,4+1/3);
% cell centers
\foreach \r in {0, 4/3, 8/3, 4}
\fill [] (5, \r)  circle [radius=1pt];
\foreach \r in {0, 0.6666, ..., 4}
\fill [] (7, \r)  circle [radius=1pt];

\node (notea) at (1.5,-1) {$(a)$};
\node (noteb) at (3.25,-1) {$(b)$};
\node (notec) at (5,0-1) {$(c)$};
\node (noted) at (7,-1) {$(d)$};

\end{tikzpicture}
\end{document}


The output I get looks like this:

But I want it to look like this (differences in red):

I suspect that this has to do with the length units I've chosen, because I have multiples of 1/3 that in the end don't sum up to round numbers. \draw[step=4/3,black,thin] (5-2/3,-2/3) grid (5+2/3,4+2/3);

• Welcome to TeX.SX! Is your system up to date? My result looks different. Still wrong, but different. – LaRiFaRi Jul 14 '15 at 10:32
• I have tikz.sty 2010/10/13 v2.10 (rcs-revision 1.76). I've been trying, but I always get the wrong results in the grids (c) and (d). – Bubastis Jul 14 '15 at 10:45
• Grids are always centered around zero. Make the first coordinates (0,0) of the 3rd and 4th and you'll see they agree at the origin. Instead you can draw a grid and shift it to a position with a scope – percusse Jul 14 '15 at 11:02

Here a possible solution. I used nodes instead of grid. This way it is very easy to store all the work needed to be done into foreach-loops and you don't have to do the calculations by hand.

\documentclass[tikz, border=6mm]{standalone}

\pgfmathsetmacro{\mya}{4/3}
\pgfmathsetmacro{\myb}{2/3}

\begin{document}
\begin{tikzpicture}[>=latex]
\draw [<->] (.5,0) -- ++(0,4) node [midway, fill=white] {$\Delta r$};

\foreach \y in {0,4}    {
\draw (.25,\y) --++ (.5,0);
\draw [dotted] (.25,\y) --++ (8.5,0);
}

\draw [blue, dashed] (.5,.5) -- ++(1.5,0);
\draw [blue, dashed] (.5,.25) -- ++(3.5,0);

\begin{scope}[every node/.style={draw, rectangle}]]
\foreach \y in {0,...,3} \fill (2,\y+.5) circle (1pt) node [minimum size=1cm] {};
\foreach \y in {0,.5,...,3.5} \fill (4,\y+.25) circle (1pt) node [minimum size=.5cm] {};
\foreach \y in {0,\mya,...,4} \fill (6,\y) circle (1pt) node [minimum size=\mya cm] {};
\foreach \y in {0,\myb,...,4} \fill (8,\y) circle (1pt) node [minimum size=\myb cm] {};
\end{scope}

\foreach \x\l in {2/a,4/b,6/c,8/d} \node at (\x,-1.5) (note\l) {$(\l)$};
\end{tikzpicture}
\end{document}


• Thank you moospit! I was about to post a solution still using grid thanks to the comment about shifting the coordinates with scope, but your solution is more elegant! – Bubastis Jul 14 '15 at 11:39

You can create a pic that draws your grid and the points. And then place it where you want.

\documentclass[tikz,border=5]{standalone}
% define pic that draw a grid and dots
% #1 = number of squares, #2 = scale factor
\tikzset{
pics/mygrid/.style 2 args={
code={
\draw[pic actions,scale=#2,shift={(-.5,-.5)}] (0,0) grid (1,#1);
\fill[pic actions] foreach \i in {1,...,#1}{(0,{#2*(\i-1)}) circle (1pt)};
}
}
}
\begin{document}
\begin{tikzpicture}
% Rulers -------------------------
\draw [black!80, dotted] (0, 0) -- (9, 0) (0, 4) -- (9, 4);
\draw [black!50, thick, dashed] (0, .5) -- (1.5, .5) (0, .25) -- (3.25, .25);
\draw[|<->|] (0,0) -- node[fill=white]{$\Delta r$} (0,4);
% Grids -------------------------
\draw (1.5,.5) pic{mygrid={4}{1}}
(3.25, .25) pic{mygrid={8}{.5}}
(6, 0) pic{mygrid={4}{4/3}}
(8, 0) pic{mygrid={7}{4/6}};
\end{tikzpicture}
\end{document}