答案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}