我正在尝试绘制一个数组,但数组下方也有索引。目前,我只有这个
这是我的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\coordinate (s) at (0,0);
\foreach \num in {3, 1, 4, 1, 5}{
\node[minimum size=6mm, draw, rectangle] at (s) {\num};
\coordinate (s) at ($(s) + (1,0)$);
}
\end{tikzpicture}
\label{fig:testArray}
\end{figure}
\end{document}
任何帮助将不胜感激!
答案1
这可能会产生预期的结果:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcounter{index}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\setcounter{index}{0}
\coordinate (s) at (0,0);
\foreach \num in {3, 1, 4, 1, 5}{
\node[minimum size=6mm, draw, rectangle] at (s) {\num};
\node at ($(s)-(0,0.5)$) {\theindex};
\stepcounter{index}
\coordinate (s) at ($(s) + (1,0)$);
}
\end{tikzpicture}
\label{fig:testArray}
\end{figure}
\begin{figure}
\centering
\begin{tikzpicture}
\setcounter{index}{0}
\coordinate (s) at (0,0);
\foreach \num in {2, 7, 1, 8, 2}{
\node[minimum size=6mm, draw, rectangle] at (s) {\num};
\node at ($(s)-(0,0.5)$) {\theindex};
\stepcounter{index}
\coordinate (s) at ($(s) + (1,0)$);
}
\end{tikzpicture}
\label{fig:testArray2}
\end{figure}
\end{document}