Drawing Lattice/Trellis Graphs using PGF/TikZ

I am trying to draw a graph containing lattice points and vectors. I wish it to look something like this: I am not sure how to go about it, however, I would ideally like to be able to use PGF/TikZ. Is this possible, or would I need to download and install a package like GeoGebra to create the graph, and export it into PGF/TikZ format? Again, I am not sure whether or not this is possible using GeoGebra.

Any suggestions would be appreciated.

Follow-up Question: Aligning x- and y-axis with lattice points on a graph using PGF/TikZ

• This is totally doable directly in TikZ. It comes down to how do you want to specify the points. I they can be specified within a loop that would be great. Then it is a simple matter of connecting the dots. If you make a start am sure that you can get help here if you get stuck. Jan 28 '12 at 3:50
• Thus far, I have a partial solution to the problem. The difficulty I am experiencing is aligning the axis with the lattice points. The question is whether to edit this question, or to raise it as a separate question. Please advise. Thanks.
– Bill
Jan 28 '12 at 6:06
• It depends on if the problem you are having is what was asked/answered here, or it is something more subtle. Sounds more like a math question to me. Jan 28 '12 at 8:23

Here is a little bit advanced but not so difficult to understand grid construction:

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

\begin{tikzpicture}
\begin{scope}
\clip (0,0) rectangle (10cm,10cm); % Clips the picture...
\pgftransformcm{1}{0.6}{0.7}{1}{\pgfpoint{3cm}{3cm}} % This is actually the transformation
%  matrix entries that gives the slanted
% unit vectors. You might check it on
% MATLAB etc. . I got it by guessing.

\draw[style=help lines,dashed] (-14,-14) grid[step=2cm] (14,14); % Draws a grid in the new coordinates.
\filldraw[fill=gray, draw=black] (0,0) rectangle (2,2); % Puts the shaded rectangle
\foreach \x in {-7,-6,...,7}{                           % Two indices running over each
\foreach \y in {-7,-6,...,7}{                       % node on the grid we have drawn
\node[draw,circle,inner sep=2pt,fill] at (2*\x,2*\y) {}; % Places a dot at those points
}
}
\end{scope}
\end{tikzpicture}
\end{document}

Here is the output: If you combine it with Peter's code it would be almost ready. Note that there is a scope environment around my code that keeps the transformation local to that scope. Cehck the manual for some intuition about the command \pgftransformcm

• Thanks for that percusse. I will try to integrate/merge this solution with the one that Peter suggested.
– Bill
Jan 28 '12 at 4:41
• @Bill Try, for example, adding \draw[->] (0,0) -- (2,-2); (inside and at the end of the scope) to see where the origin is and how to draw the vectors. Jan 28 '12 at 4:43
• Thanks for that! Will let you know if I have any problems.
– Bill
Jan 28 '12 at 5:28

As I commented, this is totally doable in tikz:

The simpler part is to pick the points where you want to draw in the vectors. For instance, once you have the coordinate the (Origin), (Bone) and (Btwo) defined you could simple draw the vector as \draw (Origin) -- (Bone);. Then the vector from the origin to 2b1+b2 would simply be \draw ($2*(Bone)+(Btwo)$).

The harder part is to figure out how you want to specify where the black circles are located. One way would be to do some sort of loop. Alternatively, you could specify a family of straight lines draw them dashed, and compute the intersections with the others to place the small circles. Or, it seems that the lattice points are draw along the sum of the vectors. So you could do something like:

\coordinate (vec) at ($(Bone)+(Btwo)$);
\foreach \i in {-1, 0, 1, 2, 3} {
\draw [fill = black]($\i*(vec)$) circle (3pt);
}

where the numbers were chosen to determine what multiples of (vec) that I wanted to place the circles.

Now just need to add some logic to determine how to draw the dashed lines. I'd recommend using a toggle from the etoolbox package to determine if you are at the first point in the list, and use a \draw [dashed] from the last point to the current point (if you are not at the first point in the sequence). There are many other ways to do conditionals, and a good reference is available at this question: LaTeX conditional expression Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}% neded for coordinate calculations

\begin{document}
\begin{tikzpicture}
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (-3,0);
\coordinate (XAxisMax) at (5,0);
\coordinate (YAxisMin) at (0,-2);
\coordinate (YAxisMax) at (0,5);

\coordinate (Bone) at (1,2);
\coordinate (Btwo) at (2,-1);

\draw [thin, gray,-latex] (XAxisMin) -- (XAxisMax);% Draw x axis
\draw [thin, gray,-latex] (YAxisMin) -- (YAxisMax);% Draw y axis

% Latice points along b1+b2
\coordinate (vec) at ($(Bone)+(Btwo)$);
\foreach \i in {-1, 0, 1, 2, 3} {
\draw [fill = black]($\i*(vec)$) circle (3pt);
}
% Draw the vectors
\draw [ultra thick,-latex,red] (Origin) -- (Bone) node [above left] {$b_1$};
\draw [ultra thick,-latex,red] (Origin) -- (Btwo) node [below right] {$b_2$};
\draw [ultra thick,-latex,red] (Origin) -- ($(Bone)+(Btwo)$) node [below right] {$b_1+b_2$};
\draw [ultra thick,-latex,red] (Origin) -- ($2*(Bone)+(Btwo)$) node [above left] {$2b_1+b_2$};
\draw [thin,-latex,red, fill=gray, fill opacity=0.3] (Origin) -- ($2*(Bone)+(Btwo)$) --
($3*(Bone)+2*(Btwo)$) -- ($(Bone)+(Btwo)$) -- cycle;
\end{tikzpicture}
\end{document}
• Thanks Peter! That has given me a start! If I get stuck, I will let you know. Sincerely,
– Bill
Jan 28 '12 at 4:16
• @Bill: See updated code: Started the lattice points. You should be able to take it from here. Jan 28 '12 at 4:29