![如何使用 `\whiledo` 以编程方式制作表格行?](https://linux22.com/image/233653/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20%60%5Cwhiledo%60%20%E4%BB%A5%E7%BC%96%E7%A8%8B%E6%96%B9%E5%BC%8F%E5%88%B6%E4%BD%9C%E8%A1%A8%E6%A0%BC%E8%A1%8C%EF%BC%9F.png)
下面的代码片段不起作用:
\documentclass{minimal}
\usepackage{longtable}
\usepackage{ifthen}
\newcounter{xyz}
\setcounter{xyz}{0}
\begin{document}
\begin{longtable}{|*2{c|}}
\hline
\whiledo{\value{xyz}<10}%
{%
\thexyz & something\stepcounter{xyz} \\\hline%
}%
\end{longtable}
\end{document}
答案1
\documentclass{article}
\usepackage{longtable}
\newcounter{xyz}\newcounter{uvw}
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand*\resettabtoks{\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\pagestyle{empty}
\begin{document}
\resettabtoks
\loop\ifnum\theuvw<10\relax
\stepcounter{uvw}
\addtabtoks{\thexyz & something\stepcounter{xyz} \\\hline}%
\repeat
\begin{longtable}{|*2{c|}}\hline
\printtabtoks
\end{longtable}
\end{document}
答案2
尝试这个:
\documentclass{article}
\usepackage{longtable}
\usepackage{ifthen, xspace}
\newcounter{xyz}
\setcounter{xyz}{0}
\begin{document}
\def\and{&\xspace}
\begin{longtable}{|*2{c|}}
\hline
\whiledo{\value{xyz}<10}%
{%
\thexyz \and something\stepcounter{xyz} \\\hline%
}%
\end{longtable}
\end{document}
编辑从最小类别更改为文章(参见评论)。