我怎样才能绘制一系列六边形,并且每个六边形内都有一个数字(乘法蜂窝)?我在logicpuzzle
包中没有找到该图案。
更新中:
我尝试写入$2x$
代替+2
、$-3x$
代替-3
和$-6x^2$
代替-6
,但不起作用。为什么?我发布了我的代码。六边形完全是白色的。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usepackage{amsbsy}
\usepackage{amsmath}
\newcommand{\hexmult}[2]{\begin{tikzpicture}[hexa/.style= {shape=regular polygon,regular polygon sides=6,minimum size=1cm, draw,inner sep=0,anchor=south,rotate=30}]
\foreach \j in {1,...,#1}{%
\foreach \i in {1,...,\j}{%
\node[hexa] (h\i;\j) at ({(\i-\j/2)*sin(60)},{\j*0.75}) {};} }
\begin{scope}[execute at begin node=$, execute at end node=$]
\foreach \k/\l in {#2}{\node at (h\k) {\l}; }
\end{scope}
\end{tikzpicture}}
\begin{document}
\hexmult{2}{1;2/$ +2 $,2;2/{$ -3 $},1;1/}
\end{document}
答案1
如果您不介意反向指定数字:
\documentclass[varwidth,border=5]{standalone}
\usepackage{tikz}
\tikzdeclarecoordinatesystem{hex}{%
\pgfmathsetmacro\y{int(floor(sqrt(2*(#1))+0.5))}%
\pgfmathsetmacro\x{int((\y-1)/2*\y))}%
\pgfpointxy{((#1)-\x-\y/2)* sin(60)}{\y*0.75}}
\tikzset{hex/.style={insert path={[every hex/.try]
(30:1/2) \foreach \j in { 1,...,5} {-- (30+\j*60:1/2) } -- cycle
(0,0) node {$#1$}}}}
\begin{document}
\tikz\foreach \n [count=\i]in {+24,-4,-6,+2,-2,-3,-1,-2,+1,-3}
\path [draw, shift=(hex cs:\i), hex=\n];
\tikz\foreach \n [count=\i]in {-6,+2,-3}
\path [draw, shift=(hex cs:\i), hex=\n];
\tikz[every hex/.style={fill=blue!50!white!50!cyan, draw=white, text=white}]
\foreach \n [count=\i]in {-2,+1,+3,-1,+1,+2}
\path [draw, shift=(hex cs:\i), hex=\n];
\end{document}
答案2
我定义了一个新命令\hexmult
,它接受 2 个参数。第一个参数是一个正整数,表示六边形的行数。第二个参数是一个逗号分隔的列表,其中每个条目的形式为i;j/k
。j
是行(从底部开始),i
是六边形编号(从左侧开始),是k
单元格内容,以数学模式解析(因此减号看起来正确)。您可以根据需要填充任意数量的单元格,或者通过将第二个参数留空来不填充任何单元格。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\newcommand{\hexmult}[2]{\begin{tikzpicture}[hexa/.style= {shape=regular polygon,regular polygon sides=6,minimum size=1cm, draw,inner sep=0,anchor=south,rotate=30}]
\foreach \j in {1,...,#1}{%
\foreach \i in {1,...,\j}{%
\node[hexa] (h\i;\j) at ({(\i-\j/2)*sin(60)},{\j*0.75}) {};} }
\begin{scope}[execute at begin node=$, execute at end node=$]
\foreach \k/\l in {#2}{\node at (h\k) {\l}; }
\end{scope}
\end{tikzpicture}}
\begin{document}
\hexmult{4}{1;4/-1,2;4/-2,3;4/+1,4;4/-3,1;3/+2,2;3/-2,3;3/-3,1;2/-4,2;2/+6,1;1/-24}
\hexmult{3}{1;3/-1,2;3/+1,3;3/+2,2;2/+2}
\hexmult{2}{1;2/+2,2;2/-3,1;1/-6}
\end{document}
请注意,您不限于在十六进制中使用数字。代码
\hexmult{2}{1;2/2x,2;2/-3x,1;1/-6x^2}
将产生输出:
答案3
这是一种使用以下方法“手工”绘制网格的方法元帖子。评论中的链接向您展示了 tikz 中的类似方法。
\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}
这是包含在内luamplib
以便编译lualatex
或适应普通 MP 或gmp
包。
2021 年更新
我知道这已经很老了,但是由于前几天它又获得了一次投票,所以我想自动版本可能会很有趣:
此处,所有乘积均从最上面一行单元格中为您计算。与之前一样,使用 进行编译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}