Here's a way to draw your grids "by hand" using Metapost. The links in the comments show you similar approaches in tikz.
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/gImHB.png)
\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
pair u, v;
u = 23 right;
v = u rotated 120;
path gon;
gon = for i=0 upto 5: 0.57735026919 u rotated (30 + 60i) -- endfor cycle;
vardef mark(expr x, y, s) =
save p; pair p;
p = origin shifted (x*u) shifted (y*v);
fill gon shifted p withcolor 7/8[blue, white];
draw gon shifted p withcolor 3/4 blue;
label(s, p);
enddef;
mark(0, 0, "$+24$");
mark(0, 1, "$-4$");
mark(1, 1, "$-6$");
mark(0, 2, "$+2$");
mark(1, 2, "$-2$");
mark(2, 2, "$+3$");
mark(0, 3, "$-1$");
mark(1, 3, "$-2$");
mark(2, 3, "$+1$");
mark(3, 3, "$+3$");
endfig;
\end{mplibcode}
\end{document}
This is wrapped up in luamplib
so compile with lualatex
or adapt for plain MP or gmp
package.
Update 2021
I know this is old, but since it got another up vote the other day, I thought perhaps an automatic version would be interesting:
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/VpGnp.png)
Here the products are all calculated for you from the top row of cells. As before, compile with lualatex
.
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
path hexagon;
hexagon = for i=0 upto 5: 14 up rotated 60 i -- endfor cycle;
pair u, v;
u = point 9/2 of hexagon - point 3/2 of hexagon;
v = point 7/2 of hexagon - point 1/2 of hexagon;
vardef hexit(expr n) =
image(
fill hexagon withcolor 7/8[if n < 0: blue elseif n > 0: red else: white fi, white];
draw hexagon withcolor 2/3 blue;
picture t; t = thelabel("$" & if n > 0: "+" elseif n < 0: "-" else: "" fi & decimal(abs(n)) & "$", origin);
numeric wd; wd = xpart (urcorner t - llcorner t) + 4;
draw t scaled min(1, (abs(u) / wd));
)
enddef;
vardef trex(text t) =
save cell, i, cells; numeric cell[], i, cells;
image(
i = 0; for n=t:
cell[incr i] = n;
draw hexit(cell[i]) shifted (i * u);
endfor
cells = i;
for j=cells-1 downto 1:
for k=1 upto j:
cell[incr i] = cell[i-j-1] * cell[i-j];
draw hexit(cell[i]) shifted ((cells-j) * v) shifted (k * u);
endfor
endfor
)
enddef;
beginfig(1);
draw trex(-1,-2,1,3);
draw trex(-1,1,2) shifted 120 right;
draw trex(1,-2,1,-2,1) shifted (32, -100);
endfig;
\end{mplibcode}
\end{document}
$..$
. This code automatically parses entries in math mode. Just write\hexmult{2}{1;2/2x,2;2/-3x,1;1/-6x^2}
.