# Alignment of lines with Grid

I am trying to draw the following figure. In this figure, I am trying to provide the dimensions of the rectangular blocks.

I draw the figure using following code-

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}

\tikzset{%
dimen/.style={<->,>=latex,thin,every rectangle node/.style={fill=white,midway,font=\sffamily}},
symmetry/.style={dashed,thin},
}

\tikzstyle{lightGrayBoxStyle}=[fill=gray!20,draw=black,minimum height=0.5cm,minimum width=0.5cm]

\begin{scope}[local bounding box=scope1]

\draw [draw=black, fill=gray,step=0.5] (0,0) grid  (3,1.5) rectangle (0,0);
\node[lightGrayBoxStyle] at (2.25,0.75) (b4) {};
\node[lightGrayBoxStyle] at (1.75,0.75) (b3) {};
\node[lightGrayBoxStyle] at (1.25,0.75) (b2) {};
\node[lightGrayBoxStyle] at (0.75,0.75) (b1) {};

\draw [dimen,-] ([xshift=-1.4cm]b1.south) -- ([xshift=-1.4cm]b1.north) node  {$s_1 = 1$};
\draw [dimen,<-,rotate=-90] ([yshift=-1.2cm]b1.north) -- ++(-7pt,0);
\draw [dimen,<-,rotate=90] ([yshift=1.2cm]b1.south) -- ++(-7pt,0);
\draw[dashed] ([xshift=-0.75cm]b1.south) --([xshift=-2.0cm]b1.south);
\draw[dashed] ([xshift=-0.75cm]b1.north) --([xshift=-2.0cm]b1.north);

\draw [dimen,<->] ([yshift=1.1cm]b1.west) -- ([yshift=1.1cm]b4.east) node {$s_2 = 4$};

\draw[dashed] ([yshift=0.75cm]b1.west) --([yshift=1.25cm]b1.west);
\draw[dashed] ([yshift=0.75cm]b4.east) --([yshift=1.25cm]b4.east);
\node[draw=none] at (1.5,-0.75) {$(s_1,s_2) = (1,4)$};

\end{scope}

\begin{scope}[local bounding box=scope2,shift={($(scope1.east)+(1.5cm,-0.5cm)$)}]

\draw [draw=black, fill=gray,step=0.5] (0,0) grid  (2,2) rectangle (0,0);

\node[lightGrayBoxStyle] at (0.75,0.75) (b1) {};
\node[lightGrayBoxStyle] at (1.25,0.75) (b2) {};
\node[lightGrayBoxStyle] at (0.75,1.25) (b3) {};
\node[lightGrayBoxStyle] at (1.25,1.25) (b4) {};

\draw [dimen,-] ([yshift=1.6cm]b1.west) -- ([yshift=1.6cm]b2.east) node[above=2pt]{$s_2 = 2$};

\node[draw=none] at (1.00,-0.75)  {$(s_1,s_2) = (2,2)$};

\draw [dimen,<-] ([yshift=1.6cm]b1.west) -- ++(-7pt,0);
\draw [dimen,<-] ([yshift=1.6cm]b2.east) -- ++(+7pt,0);
\draw[dashed] ([yshift=1.25cm]b1.west) --([yshift=1.8cm]b1.west);
\draw[dashed] ([yshift=1.25cm]b2.east) --([yshift=1.8cm]b2.east);

\draw[dashed] ([xshift=-0.75cm]b3.north) --([xshift=-1.5cm]b3.north);
\draw[dashed] ([xshift=-0.75cm]b1.south) --([xshift=-1.5cm]b1.south);

\draw [dimen,-] ([xshift=-1.2cm]b1.south) -- ([xshift=-1.2cm]b3.north) node[rotate=90,yshift=0.40cm] {$s_1 = 2$};
\draw [dimen,<-,rotate=-90] ([yshift=-1.2cm]b3.north) -- ++(-7pt,0);
\draw [dimen,<-,rotate=90] ([yshift=1.2cm]b1.south) -- ++(-7pt,0);

\end{scope}

\begin{scope}[local bounding box=scope3,shift={($(scope2.east)+(1.0cm,-0.75cm)$)}]

\draw [draw=black, fill=gray,step=0.5] (0,0) grid  (1.5,3) rectangle (0,0);
\node[lightGrayBoxStyle] at (0.75,0.75) (b1) {};
\node[lightGrayBoxStyle] at (0.75,1.25) (b2) {};
\node[lightGrayBoxStyle] at (0.75,1.75) (b3){};
\node[lightGrayBoxStyle] at (0.75,2.25) (b4) {};

\node[yshift=1.15cm,rotate=90] at (b4.north) {$s_2=1$};
\draw[dashed] ([yshift=0.75cm]b4.west) --([yshift=1.85cm]b4.west);
\draw[dashed] ([yshift=0.75cm]b4.east) --([yshift=1.85cm]b4.east);

\draw [dimen,<->] ([xshift=-1.2cm]b1.south) -- ([xshift=-1.2cm]b4.north) node[rotate=90] {$s_1 = 4$};

\draw[dashed] ([xshift=-0.75cm]b4.north) --([xshift=-1.3cm]b4.north);
\draw[dashed] ([xshift=-0.75cm]b1.south) --([xshift=-1.3cm]b1.south);
\draw [dimen,<-] ([yshift=1.2cm]b4.west) -- ++(-7pt,0);
\draw [dimen,<-] ([yshift=1.2cm]b4.east) -- ++(+7pt,0);

\node[draw=none] at (0.7,-0.75) {$(s_1,s_2) = (4,1)$};

\end{scope}

\end{tikzpicture}
\end{document}


Now the problem is, the junction of the grid lines and the dimension lines I have circled in red seems to be mis-aligned. How do I make sure that these lines are aligned?

If there is a better way of drawing this figure, I would be glad to know. I would also like to add an addition question. Is it possible to align all the text at the bottom of every rectangle aligned?

## 1 Answer

The problem of the alignment in the lines is due to the line width. I propose a different approach to code your figure, which IMHO simplifies a lot the input of the "grid" and makes also simpler to add the required dimensions.

My solution uses a matrix of nodes to draw the grid. First, we define some styles for each kind of square:

\tikzset{
X/.style = {minimum width=.5cm, minimum height=.5cm, draw, fill=black!60},
!/.style = {X, fill=black!20},
}


Next the "grid" is specified by the following matrix (\usetikzlibrary{matrix} is required):

\begin{tikzpicture}
\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth] (M) {
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
};
\end{tikzpicture}


This produces:

In addition, it defines (M) as the name for the matrix, which causes that automatically each cell in that matrix is named with the scheme (M-row-column).

To draw the first vertical dashed line, a first idea could be to use:

\draw[dashed] (M-1-1.north east) -- +(0,0.5);


(note the use of relative coordinates with the + sign). However, this causes the same problem than in your original figure, i.e: the line width causes a little offset:

To be independent of the line width, we should use as starting point the one located at the middle between the first and second cell. You can use the interpolated coordinate system to specify it (requires \usetikzlibrary{calc}), as follows:

\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5);


This doesn't have undesired offsets:

In addition, we can name an auxiliar coordinate near the end of that dashed line, in order to facilitate later draw of the dimension line:

\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);


This draws a vertical line of 0.5 lenght, and defines a coordinate named (a) at 0.4 length of the start of the line. This will be the vertical distance between the grid and the dimension line.

Using these ideas, the complete code for the first figure is as follows:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\tikzset{
X/.style = {minimum width=.5cm, minimum height=.5cm, draw, fill=black!60},
!/.style = {X, fill=black!20},
dimen/.style={
<->,>=latex,thin,
every rectangle node/.style={fill=white,midway,font=\sffamily}
},
}

\begin{tikzpicture}
\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth] (M) {
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
};

\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-5.north)!.5!(M-1-6.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\draw[dimen] (a) -- (b) node {$s_2=4$};

\draw[dashed] ($(M-1-1.west)!.5!(M-2-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(a);
\draw[dashed] ($(M-2-1.west)!.5!(M-3-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(b);
\draw[dimen, <-] (a) -- +(0,.4);
\draw[dimen, <-] (b) -- +(0,-.4);
\node[left] at (M-2-1.west) {$s_1=1$};
\end{tikzpicture}
\end{document}


Which produces:

# Update

Note that when using \matrix, you can specify the coordinates at which you want that matrix to appear. This allows you to use matrix again for remaining grids, without needing scopes or x-shifts. To ensure proper bottom alignment, give each matrix anchor=south.

You can even reuse the name (M) for the second matrix, after the first one was completed. This simplifies copy&pasting code from the first one. So, for example:

\begin{tikzpicture}

%%%%% FIRST MATRIX

\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth, anchor=south] (M) {
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
};
\node[below] at (M.south) {$(s_1,s_2)=(1,4)$};

\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-5.north)!.5!(M-1-6.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\draw[dimen] (a) -- (b) node {$s_2=4$};

\draw[dashed] ($(M-1-1.west)!.5!(M-2-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(a);
\draw[dashed] ($(M-2-1.west)!.5!(M-3-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(b);
\draw[dimen, <-] (a) -- +(0,.4);
\draw[dimen, <-] (b) -- +(0,-.4);
\node[left] at (M-2-1.west) {$s_1=1$};

%%%%%%% SECOND MATRIX

\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth, anchor=south] at (4,0) (M) {
|[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| \\
};
\node[below] at (M.south) {$(s_1,s_2)=(2,2)$};

\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-3.north)!.5!(M-1-4.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\end{tikzpicture}


Also note how (M.south) was used to position the captions.

# Edit

According with the comments and a conversation in the chat, the OP has problems with the placement of the dimensions for the second matrix, so I'm providing a complete example for this one:

%%%%%%% SECOND MATRIX

\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth, anchor=south] at (4,0) (M) {
|[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| \\
};
\node[below] at (M.south) {$(s_1,s_2)=(2,2)$};

\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-3.north)!.5!(M-1-4.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\draw[dimen, <-] (a) -- +(-.4,0);
\draw[dimen, <-] (b) -- +(.4,0);
\node[above] at ($(a)!.5!(b)$) {$s_2=2$};

\draw[dashed] ($(M-1-1.west)!.5!(M-2-1.west)$) -- +(-.5,0) +(-.4,0) coordinate(a);
\draw[dashed] ($(M-3-1.west)!.5!(M-4-1.west)$) -- +(-.5,0) +(-.4,0) coordinate(b);
\draw[dimen, <-] (a) -- +(0,.4);
\draw[dimen, <-] (b) -- +(0,-.4);
\node[above,rotate=90] at ($(a)!.5!(b)$) {$s_1=2$};


• thanks! do you see the problem with dimensions in second matrix? – knowledge_seeker Jul 17 '14 at 22:28
• @raj No... what problem? – JLDiaz Jul 17 '14 at 22:30
• \draw[dashed] ($(N-1-1.north)!.5!(N-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(c); \draw[dashed] ($(N-1-3.north)!.5!(N-1-4.north)$) -- +(0,0.5) +(0,0.4) coordinate(d); \draw[dimen,-] (c) -- (d) node {$s_2=4$}; the dimension lines do not appear correctly. sorry, I just called the second matrix as N, – knowledge_seeker Jul 17 '14 at 22:31
• @Raj The text is too wide for the allowable space, and your dimen style has a white fill which will hide the dashed lines. You better do: \draw[dimen, <-] (c) -- +(-.4,0); \draw[dimen, <-] (d) -- +(.4,0); \node[above] at ($(c)!.5!(d)$) {$s_2=2$}; – JLDiaz Jul 17 '14 at 22:34
• but still the dashed lines are hidden, should it be done by resizing the text font somehow? – knowledge_seeker Jul 17 '14 at 22:36