答案1
这个答案几乎完全取自这个答案。如果有人认为距离太近,我会很乐意将其移除。
\documentclass{article}
\usepackage{pgffor,etoolbox}
% based on https://tex.stackexchange.com/a/349378/121799
\newcommand{\preguntas}[1]{
\newcommand*\mytablecontentsUpper{}
\newcommand*\mytablecontentsLower{}
\foreach \X in {1,...,#1}{
{\xappto\mytablecontentsUpper{ & P\X}
\xappto\mytablecontentsLower{ & }
}
}
\begin{tabular}{|c|*{#1}{c|}}\hline
Calificaci\'on \mytablecontentsUpper \\
\hline
\mytablecontentsLower \\
\hline
\end{tabular}
}
\begin{document}
\preguntas{5}
\end{document}
答案2
使用etoolbox
的\docsvlist
:
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\newcommand{\preguntas}[1]{%
\begingroup
\count@\z@
\def\@tempa{}%
\def\@ddcomma{}%
\loop
\ifnum\count@<#1
\advance\count@\@ne
\edef\@tempa{\@tempa\@ddcomma\the\count@}%
\def\@ddcomma{,}%
\repeat
\global\def\@PorGobble@##1{P##1}%
\def\@tempb{}%
\def\do##1{\appto\@tempb{&\@PorGobble@{##1}}}%
\expandafter\docsvlist\expandafter{\@tempa}%
\begin{tabular}{|c|*{#1}{c|}}\hline
\@tempb\\\hline
\global\let\@PorGobble@\@gobble
Calificación \@tempb\\\hline
\end{tabular}%
\global\let\@PorGobble@\@undefined
\endgroup}
\makeatother
\begin{document}
\preguntas{0}
\preguntas{1}
\preguntas{2}
\preguntas{5}
\end{document}