Comandos Macros em ambientes tabulares -Automação

Comandos Macros em ambientes tabulares -Automação

Quero criar uma tabela com base em dados separados por vírgula da seguinte forma:

\documentclass{article}  
\usepackage{pgffor}
\usepackage{multirow}
\usepackage{float}

\newcommand{\cases}{a , b}

\newcommand{\repetir}{
\foreach \case in {\cases}{
    \case &  &  &  &  &  &  &  &  &  &  &  &  &  &  & \\\hline
}
} 

\begin{document}
\begin{table}[H]
\centering
\scriptsize
\begin{tabular}{ | p{4cm} | p{3cm} |l| l|l|l| l | l | l | l | l| l | l | l | l | p{3cm} | }
    \hline
    \multirow{3}{*}{Case name} &    \multirow{3}{*}{Description} & \multicolumn{14}{c|}{Further Details} \\\cline{3-16} 
    &  & \multicolumn{3}{c|}{O} & \multicolumn{3}{c|}{A}  & \multicolumn{2}{c|}{F} & \multicolumn{2}{c|}{B} & & \multirow{2}{*}{G} & \multirow{2}{*}{C} & \multirow{2}{*}{Other} \\ \cline{3-13} 
    & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 \\ \hline

\repetir

\end{tabular}
\end{table}
\end{document}

No entanto, sempre recebo um erro:

Extra } or forgotten \endgroup \repetir. 

Como posso fazer isso?

Responder1

Você não pode começar \foreachem uma célula da tabela e terminar em outra.

\documentclass{article}
\usepackage[a4paper,landscape]{geometry}
\usepackage{multirow}
\usepackage{xparse}

% a command for doing the repetitive things:
% 1. open center, 2. choose the font size,
% 3. start the tabular, 4. print the header
% Next the command processes the argument and
% ends the job
\NewDocumentCommand{\chungeltable}{m}
 {%
  \begin{center}\scriptsize
  \begin{tabular}{ | p{4cm} | p{3cm} | *{13}{l|} p{3cm} | }
  \hline
  \multirow{3}{*}{Case name} &
  \multirow{3}{*}{Description} &
  \multicolumn{14}{c|}{Further Details} \\
  \cline{3-16} 
  &  & \multicolumn{3}{c|}{O} & \multicolumn{3}{c|}{A}  & \multicolumn{2}{c|}{F} &
       \multicolumn{3}{c|}{B} & \multirow{2}{*}{G} & \multirow{2}{*}{C} & \multirow{2}{*}{Other} \\
  \cline{3-13} 
  & & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & & & \\ \hline
  \chungeltablebody{#1}
  \end{tabular}
  \end{center}
 }

\ExplSyntaxOn
\NewDocumentCommand{\chungeltablebody}{m}
 {
  % clear the variable that will contain the table body
  \tl_clear:N \l_tmpa_tl
  % for each item in the comma separated list
  \clist_map_inline:nn { #1 }
   {
    % add the item followed by the necessary number of &'s
    % to the variable containing the table body
    \tl_put_right:Nn \l_tmpa_tl { ##1 &  &  &  &  &  &  &  &  &  &  &  &  &  &  & \\\hline }
   }
   % deliver the table body
   \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}

\chungeltable{a,b}

\end{document}

insira a descrição da imagem aqui

A chave é \clist_map_inline:nn; cada item no primeiro argumento é passado como #1o código no segundo argumento.

Como estamos no corpo de uma definição, #1deveria se tornar ##1, porque #1é o argumento real para a macro que estamos definindo.

informação relacionada