![Tikzpicture 中的程序](https://linux22.com/image/353681/Tikzpicture%20%E4%B8%AD%E7%9A%84%E7%A8%8B%E5%BA%8F.png)
答案1
这本来是一个很好的练习,你可以自己弄清楚,然后你可以问问自己是否遇到了困难。一般来说,这样做可能会更好。如果你尝试过任何事情,给我们看一看。但因为周围有像我这样的白痴:
非常简单,请参阅代码中的一些注释。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\N{4} % define your value of N
\foreach [
% values to be printed along diagonal, save in \i
evaluate=\n as \i using int(2*\n-1),
% values printed below the diagonal, save in \j
evaluate=\n as \j using int(2*\n),
]
\n in {1,...,\N}
{
% upper left corner of the whole thing is (0,0)
% draw squares along diagonal, with number in the middle
\draw (\n-1,-\n+1) rectangle node{\i} (\n,-\n);
% No lower rectangle for the last value in the loop
% so only draw that if the loop variable is less than \N
\ifnum \n < \N
\draw (\n-1,-\N) rectangle node{\j} (\n,-\n);
\fi
}
% draw upper and right border
\draw (0,0) -| (\N,-\N);
\end{tikzpicture}
\end{document}
答案2
使用普通方法很容易做到这一点tabular
:
\documentclass[12pt,a4paper]{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|*4{c|}}
\hline
1 & \multicolumn{3}{c|}{} \\ \cline{1-2}
\multirow{3}{*}{2} & 3 & \multicolumn{2}{c|}{} \\ \cline{2-3}
& \multirow{2}{*}{4} & 5 & \\ \cline{3-4}
& & 6 & 7 \\ \hline
\end{tabular}
\end{document}
如果数量很大,那么使用 TikZ 是有意义的:
\documentclass[tikz,border=5pt]{standalone}
\begin{document}
\def\N{7}
\begin{tikzpicture}[sqr/.style={minimum width=1cm,draw},outer sep=0pt]
\foreach \i [evaluate=\i as\j using int(2*\i-1),
evaluate=\i as\t using int(2*\i)] in {1,...,\N}{
\node(c\j) at (\i,-\i)[sqr,minimum height=1cm, anchor=south]{\j};
\ifnum\i<\N\node at (\i,-\i)[sqr,minimum height=\N cm-\i cm, anchor=north]{\t};
\else\draw(c1.north east) -| (c\j.north east);\fi
}
\end{tikzpicture}
\end{document}