2

I'm trying to create a command to generate this type of tables:

enter image description here

where the number of columns changes (for example \preguntas{4} or \preguntas{5}). My first idea was

\newcommand{\preguntas}[1]
  \begin{tabular}{|c|*{#1}{c|}}\hline
    \multido{\i=1+1}{#1}{ & P\i }\\\hline
    Calificación \multido{}{#1}{& }\\\hline
\end{tabular}

but I realized that multido doesn't admit the use of &. How can I create this command?

1
  • 1
    Welcome to TeX.SE! This seems to be indeed a bit tricky. Could you please check if this post allows you to do what you want? Or that post?
    – user121799
    Sep 4, 2018 at 14:10

2 Answers 2

1

This answer is almost entirely taken from this answer. I'll be happy to remove it if someone thinks it is too close.

\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}

enter image description here

2

Using etoolbox's \docsvlist:

enter image description here

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .