答案1
示例,其中同时使用智能内容(grid
)、\foreach
循环和手动元素:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw
(0, 0) grid[step=1cm] (3, 3)
(0, 2) -- (1, 3)
(0, 1) -- (2, 3)
(0, 0) -- (3, 3)
(1, 0) -- (3, 2)
(2, 0) -- (3, 1)
;
\fill[radius=3pt]
\foreach \x in {0, ..., 3} {
\foreach \y in {0, ..., 3} {
(\x, \y) circle[]
}
}
;
\path[above left]
\foreach \y in {0, 3} {
\foreach[count=\x] \v in {a, b, c, a} {
(\x - 1, \y) node {$\v$}
}
}
\foreach \p/\v in {
{0, 2}/e,
{1, 2}/f,
{2, 2}/g,
{3, 2}/e,
{0, 1}/h,
{1, 1}/j,
{2, 1}/k,
{3, 1}/h%
} {
(\p) node {$\v$}
}
;
\end{tikzpicture}
\end{document}
答案2
一个(希望是准确的)可扩展到任意大小的版本(不能说它在数学上是否有用,但制作起来很有趣)。
alphalph
用于生成任意数量的标签,如果您要超出 5x5 网格,则可能需要增加一些间距,以便为双字母标签留出更多空间。代码基本上使用了很多,foreach
并进行了一些数学运算,以确保每个标签都有适当的关联数字。
\documentclass{article}
\usepackage{tikz}
\usepackage{alphalph}
\newcommand{\triangulation}[1]{
\pgfmathsetmacro{\triangulationdimension}{#1}
\pgfmathsetmacro{\triangulationdim}{\triangulationdimension-1}
\begin{scope}[yscale=-1]
\node [circle,draw,fill=black,label=above left:{\alphalph{1}}] at (\triangulationdimension,\triangulationdimension) {};
\foreach \i [evaluate=\i as \l using int(\triangulationdimension*(\i-1)+1)] in {1,...,\triangulationdim} {
\node [circle,draw,fill=black,label=above left:{\alphalph{\i}}] at (\i,\triangulationdimension) {};
\node [circle,draw,fill=black,label=above left:{\alphalph{\l}}] at (\triangulationdimension,\i) {};
\foreach \j [evaluate=\j as \k using int(\triangulationdimension*(\j-1)+\i)] in {1,...,\triangulationdim} {
\node [circle,draw,fill=black,label=above left:{\alphalph{\k}}] at (\i,\j) {};
\foreach \x in {0,1} {
\draw (\i+\x,\j) -- (\i+\x,\j+1);
\draw (\j,\i+\x) -- (\j+1,\i+\x);
}
\draw (\j,\i+1) -- (\j+1,\i);
}
}
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\triangulation{3}
\end{tikzpicture}
\begin{tikzpicture}
\triangulation{4}
\end{tikzpicture}
\begin{tikzpicture}
\triangulation{8}
\end{tikzpicture}
\end{document}
答案3
一种程序员风格的答案,类似于@HeikoOberdiek 的答案:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,..., 3}{
\foreach \y in {0,..., 3}{
\fill (\x, \y) circle (3pt);
\draw (0,0) rectangle (\x,\y);
}
}
\foreach \x in {0, ..., 2}{
\foreach \y in {2, ..., 0}{
\draw (\x, \y) -- (\x + 1, \y + 1);
}
}
\path[above left]
\foreach \a in {0, 3}{
\foreach[count=\x] \b in {a, b, c, a}{
(\x - 1, \a) node {$\b$}
}
}
\foreach \a/\b in {
{0, 2}/e,
{1, 2}/f,
{2, 2}/g,
{3, 2}/e,
{0, 1}/h,
{1, 1}/j,
{2, 1}/k,
{3, 1}/h
} {
(\a) node {$\b$}
}
;
\end{tikzpicture}
\end{document}
答案4
这是另一种选择元帖子, 使用luamplib
接口。用 进行编译lualatex
。
MP 构造forsuffixes
提供了一种使用标签控制循环的方法。
\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
numeric u; u = 1.618cm;
numeric x,y; x = 0; y = 0;
forsuffixes @=a,b,c,a,
h,j,k,h,
e,f,g,e,
a,b,c,a:
if (x>0): draw ((x,y) -- (x-u,y )); fi
if (y>0): draw ((x,y) -- (x, y-u)); fi
if (x>0) and (y>0): draw ((x,y) -- (x-u,y-u)); fi
dotlabel.ulft("$" & str @ & "$", (x,y));
x := x + u;
if x > 3u:
x := 0;
y := y + u;
fi
endfor
endfig;
\end{mplibcode}
\end{document}