# How to draw a diagonally-split grid with TikZ?

To draw the diagram

I have written the following code:

\documentclass[11pt]{article}
\usepackage{amsmath,tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,0)--(0,8)--(10,8)--(10,0)--(0,0);
\draw (0,7)--(10,7);
\draw (0,6)--(10,6);
\draw (0,5)--(10,5);
\draw (0,4)--(10,4);
\draw (0,3)--(10,3);
\draw (0,2)--(10,2);
\draw (0,1)--(10,1);
\draw (1,0)--(1,8);
\draw (2,0)--(2,8);
\draw (3,0)--(3,8);
\draw (4,0)--(4,8);
\draw (5,0)--(5,8);;
\draw (6,0)--(6,8);
\draw (7,0)--(7,8);
\draw (8,0)--(8,8);
\draw (9,0)--(9,8);
\draw (0,-1)--(10,9);
\end{tikzpicture}

\end{document}


but this produces

How can I draw the actual picture using TikZ?

-
The documentation may be a good place to start. (Search thick.) – Sean Allred Apr 13 '13 at 9:08
While I have no idea how to omit some of your grid, please note, that your line, which i think should be diagonal, is not. I thinkn the best would be to set the end point to (10,9). – Ronny Apr 13 '13 at 9:23
Thank you. The end point will be (10,9). I have updated my post with the end point (10,9). How can I draw the complete diagram ? – Soumitra Sen Apr 13 '13 at 9:39
Since you have some responses that seem to answer your historical questions, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Claudio Fiandrino Jul 11 '13 at 9:50

Do you mean something like this?

The code:

\documentclass[png,border=10pt,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.shapes}

\tikzset{
decorate with/.style args={#1 separated by #2}{
fill,
decorate,decoration={shape backgrounds,shape=#1,shape size=1.5mm,
shape sep={#2, between borders}}
}
}

\pgfkeys{/tikz/.cd,
}

\begin{document}

\begin{tikzpicture}[x=0.5025cm,y=0.5025cm,line cap=round]
\node [draw, minimum size=0.5cm,outer sep=0pt,inner sep=0pt] (u-\xi\yi) at (\xi,-\yi) {};
}
}

\begin{scope}[xshift=\shiftx cm,yshift=-\shifty cm]
\node [draw, minimum size=0.5cm,outer sep=0pt,inner sep=0pt] (d-\xi\yi) at (-\xi,\yi) {};
}
}
\end{scope}

\begin{scope}[xshift=0.25 cm]
\path[decorate with=circle separated by 0.125cm] (\halfnumquad,0.35)--($(\halfnumquad,0.35)+(0,1.5)$) ;
\end{scope}

\begin{scope}[xshift=\shiftx cm,yshift=-0.25cm]
\path[decorate with=circle separated by 0.125cm] (0.35,-\halfnumquad)--($(0.35,-\halfnumquad)+(1.5,0)$) ;
\end{scope}

\end{tikzpicture}

\end{document}


This solution allows to select the number of squares with the key num quad: automatically, after this selection, the shifts are computed. However, they are designed for num quad=5 so perhaps some adjustment should be done.

-
The diagram is incomplete unless the dots in the right and top are drawn. – Soumitra Sen Apr 13 '13 at 9:46
@SoumitraSen It is not that difficult to add the dots. Perhaps you can do that on your own for practice. – hpesoj626 Apr 13 '13 at 9:50
@ClaudioFiandrino your given program shows the error "'standalone.cls' not found.\usepackage in my PC. I have compile it with 'kile' in 'ubuntu' " – Soumitra Sen Apr 13 '13 at 10:10
@SoumitraSen: I guess you have as first line of the log file (TeX Live 2009/Debian), so your distribution isn't the complete, but the one it is possible to install with apt-get. To install the complete version, I'd suggest you to see How to install “vanilla” TeXLive on Debian or Ubuntu?; and remember: once you have installed it, update the distribution via the manager. – Claudio Fiandrino Apr 13 '13 at 10:32
@SoumitraSen don't forget How do I update my TeX distribution? – cmhughes Apr 13 '13 at 14:29

With PSTricks.

## Single:

\documentclass[pstricks]{standalone}
\usepackage{pst-node,multido}
\psset{unit=5mm,dimen=middle,nodesep=-1}

\def\Atom#1{%
\multido{\ix=1+1,\iy=#1+-1}{#1}{%
\psframe(\ix,\iy)
\multips(!-.5 #1 2 div)(-.5,0){3}{\qdisk(0,0){2pt}}}}

\def\Molecule#1{%
\begin{pspicture}(-1,-1)(\numexpr#1+5,\numexpr#1+2)
\rput{-90}(!0 #1){\Atom{#1}}
\end{pspicture}}

\begin{document}
\Molecule{4}
\end{document}


## Multiple:

\documentclass{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{multicol}
\pagestyle{empty}

\usepackage{pst-node,multido}
\psset{unit=5mm,dimen=middle,nodesep=-1}

\def\Atom#1{%
\multido{\ix=1+1,\iy=#1+-1}{#1}{%
\psframe(\ix,\iy)
\multips(!-.5 #1 2 div)(-.5,0){3}{\qdisk(0,0){2pt}}}}

\def\Molecule#1{%
\begin{pspicture}(-1,-1)(\numexpr#1+5,\numexpr#1+2)
\rput{-90}(!0 #1){\Atom{#1}}
\end{pspicture}}

\begin{document}
\begin{multicols}{2}
\enumerate
\multido{\i=1+1}{11}{\item \raisebox{-0.5\height}{\Molecule{\i}}}
\endenumerate
\end{multicols}
\end{document}

-

A light code (some parts of the next code come from Qrrbrbirlbel's answer). The main idea is to use a transformation with cm={a,b,c,d,(e,f)}. Here a reflection, this method avoids to duplicate (extend) the code.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand\mysubfig[1]{%
\begin{scope}[#1]
\foreach \Row in {1,...,8}{\draw (0,\Row-1) --++(\Row,0) --++(0,8+1-\Row);}
\draw (8,8) -| (0,0);
\foreach \Dot in {0,1,2}{\fill (4,9+\Dot/2) circle [radius=.125cm];}
\end{scope}}

\begin{tikzpicture}
\draw (0,0) -- (10,10);
\mysubfig{shift={(0,2)}}% or cm={1,0,0,1,(0,2)}
\mysubfig{cm={0,1,1,0,(4,2)}]}% exchange x and y then apply a translation (4,2)
\end{tikzpicture}
\end{document}


-

# Solution 1

A combination of grid and clip.

If you want to change the line width of the grid change it in the scope (so that the shifting uses the correct \pgflinewidth).

line cap=rect makes a nice grid at the corners. The shifting makes sure that the clip doesn't cut anything from the grid’s line width.

## Code

\documentclass{article}
%\usepackage{tikz}
\tikzset{Shift/.style 2 args={shift={(+#1\pgflinewidth,+#2\pgflinewidth)}}}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip ([Shift=--] 1,0) -- + ([Shift=22] \numOfRows,\numOfRows) -| +(-1,0) -- cycle;
\draw[line cap=rect] (0,0) grid ++ (\numOfRows,\numOfRows);
\end{scope}
\begin{scope}
\clip ([Shift=--] 2,0) -- + ([Shift=22] \numOfRows,\numOfRows) -| +([Shift=20] \numOfRows+1,0) -- cycle;
\draw[line cap=rect] (3,0) grid ++ (\numOfRows,\numOfRows);
\end{scope}

\draw (0,-1.5) -- + (\numOfRows+3,\numOfRows+3);

\foreach \Dot in {0,1,2}{
}
\end{tikzpicture}
\end{document}


# Solution 2

Good ol’ lines. The right part is obtained by rotating the left part around the middle point of that diagonal line. (There are other ways.)

## Code

\documentclass{article}
\usepackage{tikz}
\newcommand*{\numOfRows}{8}
\begin{document}
\begin{tikzpicture}
\foreach \Rotate in {0,180}{
\begin{scope}[rotate around={\Rotate:(\numOfRows/2+1.5,\numOfRows/2)}]
\foreach \Row in {1,...,\numOfRows}{
\draw[line cap=rect] (0,\Row-1) -- ++ (\Row,0) -- ++ (0,\numOfRows+1-\Row);
}
\draw (\numOfRows,\numOfRows) -| (0,0);
\end{scope}
}
\draw (0,-1.5) -- + (\numOfRows+3,\numOfRows+3);

\foreach \Dot in {0,1,2}{
}
\end{tikzpicture}
\end{document}


# Output

-

Just for the fun of it a matrix version.

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[row sep=-0.8pt,column sep=-0.8pt,
small boxes/.style={minimum size=3mm,outer sep=0,anchor=base,thick},
execute at empty cell={
\ifnum\numexpr\pgfmatrixcurrentrow+\pgfmatrixcurrentcolumn\relax<12%
\ifnum\numexpr\pgfmatrixcurrentrow+\pgfmatrixcurrentcolumn\relax>9%
\node[small boxes](m-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn){};
\else
\node[small boxes,draw](m-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn){};
\fi
\else
\node[small boxes,draw](m-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn){};
\fi
}] (m) {
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
};
\draw ([yshift=-3mm]m-8-1.south) -- ([yshift=3mm]m-1-11.north);
\foreach \x in {1,2,3}{
\fill ([yshift=2mm+3*\x mm]m-1-4.north east) circle (3pt);
\fill ([xshift=2mm+3*\x mm]m-4-11.south east) circle (3pt);
}
\end{tikzpicture}
\end{document}


-