
我有一系列盒子,我想使用 TikZ 快速對它們進行編號。我的想法是使用這樣的東西:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[font=\tiny]
\foreach \x in {0,...,4}
\foreach \y in {0,...,4}
\draw (\x*2-.5,\y*2-.5) rectangle (\x*2+.5,\y*2+.5) node[midway] {\x+1+5*\y};
\end{tikzpicture}
\end{document}
然而,節點節點現在看起來就像0+1+5*2
一個例子。有什麼辦法可以讓 TikZ 轉換0+1+5*2
成 嗎11
?
答案1
您需要先計算 的值,\x+1+5*\y
然後使用結果作為節點的內容:
\documentclass[tikz, margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,4}
\foreach \y in {0,...,4}
\pgfmathtruncatemacro{\xy}{\x+1+5*\y} % macro which calculate value of \x+1+5*\y
\draw (\x*8-1,\y*8+1) rectangle (\x*8+1,\y*8-1) node[midway] {\xy};
\end{tikzpicture}
\end{document}
有關庫的使用pgfmath
(預設載入)請參閱章節數學和物件導向的引擎,tikz
文件中的第 1010 頁(4.1.4a 版本),特定部分94.1 解析表達式第 1012 頁。
答案2
如果我理解正確的話,您想要建造一張1 cm
寬方形盒子的桌子,並且彼此之間間隔1 cm
。
您可以nodes
與 a 一起minimum size
使用1cm
,該draw
選項預設將其輪廓繪製為 a rectangle
,此處為正方形。
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[font=\tiny]
\foreach \x in {0,...,4}{
\foreach \y [evaluate =\y as \xy using int(\x+1+5*\y)]in {0,...,4}
\node[draw,minimum size=1cm] at (2*\x,2*\y) {\xy};
}
\end{tikzpicture}
\end{document}