1

I want to create a table based on a comma separated data as follows:

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

However I always get an error:

Extra } or forgotten \endgroup \repetir. 

How can I do this?

  • Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE). that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. – Bobyandbob Apr 27 '17 at 12:58
  • You're starting the \foreach command in group ('cell') and want to end it in another group, this can't work this way – user31729 Apr 27 '17 at 13:16
  • \cases is already a defined macro, you shouldn't use that name. \mycases would be a lot better. And your code misses at least two packages (float and multirow). – Skillmon Apr 27 '17 at 13:17
2

You can't start \foreach in a table cell and end it in another one.

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

enter image description here

The key is \clist_map_inline:nn; each item in the first argument is passed as #1 to the code in the second argument.

Since we are in the body of a definition, #1 should become ##1, because #1 is the actual argument to the macro we're defining.

  • Thanks @egreg! Could you put some comments after \ExplSyntaxOn to understand what each command does? – Chüngel May 2 '17 at 11:38
  • @Chüngel Done; there are many other examples with \clist_map_inline:nn on the site. – egreg May 2 '17 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.