1

For making play cards, I need to make a table of 5 by 5 cells. First row and first column would always be the same. But the other cells would depend off the card.

So it would be nice to gave a command like: \test{3,1,6,1,2,2,3,2,4,3,4,3,5,7,5,4} that would generate table.

\documentclass{article}
\begin{document}

\newcommand\test[3][]%
{\begin{table}
\begin{tabular}{ c | c | c | c | c }
\hline
 & lente & zomer & herfst & winter \\ 
\hline 
B & 3 & 1 & 6 & 1 \\ 
G & 2 & 2 & #2 & #3 \\ 
D & 4 & 3 & 4 & 3 \\ 
S & 5 & 7 & 5 & 4 \\ 
\hline
\end{tabular}
\end{table}
 }

\test{4}{6} 
\end{document}

I can do this with more parameters, but why do I have to make 3 options for only 2 values? So I tested with:

\newcommand\test[1][]%

and the

\test{4}    

but it didn't work. What is causing this problem? Thanks enter image description here

10
  • I don't really know what you want here. A macro that takes a comma-separated list that creates the table? But why would you want that if a large portion of the table remains fixed?
    – Werner
    Oct 16, 2015 at 20:42
  • Have a look at pgfplotstable :)
    – cmhughes
    Oct 16, 2015 at 20:42
  • @Werner I want a table with sixteen different values, each table would have 16 different values, depending on the card. AND, but I didn't want to make the code to cluttered, B - G - D and S, are pictures of country flags ;-) Oct 16, 2015 at 20:47
  • @Arne: Do \test{4}{6} like in your first example works without problem. What do you want different from this?
    – Werner
    Oct 16, 2015 at 20:48
  • 1
    @Arne: You need \newcommand{\test}[2]{...} not \newcommand{\test}[2][]{...}. Note that the first doesn't have an extra [].
    – Werner
    Oct 16, 2015 at 20:54

2 Answers 2

2

You may just be misinterpreting the way a command is constructed.

\newcommand{<cmd>}[<num>]{<stuff>}

creates a macro <cmd> with <num> mandatory arguments. So, \newcommand{\test}[2]{<stuff>} would be used \test{<first>}{<second>} where <first> is referenced as #1 in <stuff> and <second> as #2. If you use

\newcommand{<cmd>}[<num>][<something>]{<stuff>}

then <cmd> takes <num>-1 mandatory arguments, and a single, optional argument. So, \newcommand{\test}[2][something]{<stuff>} can be used in the following two ways

\test{else} % <------- Single mandatory argument; optional argument will
            %          default to "something". That is, #1={something},
            %          #2={else}.
\test[someone]{else} % Optional + mandatory argument. #1={someone},
                     % #2={else}.

For more information on command definitions, see Different command definitions with and without optional argument.

In your particular case, you would need

\newcommand\test[2]%
  {\begin{tabular}{ c | c | c | c | c }
     \hline
     & lente & zomer & herfst & winter \\ 
     \hline 
     B & 3 & 1 & 6 & 1 \\ 
     G & 2 & 2 & #1 & #2 \\ 
     D & 4 & 3 & 4 & 3 \\ 
     S & 5 & 7 & 5 & 4 \\ 
     \hline
   \end{tabular}%
 }

No need for a table floating environment (as far as I can see). Also, consider using booktabs for neat tabular representation.

1
1

For just sixteen positions, some brute force is sufficient; for longer sets of data a cleverer splitting of the list would be the way to go. In the example I show the “short” version and the longer one for comparison.

\documentclass{article}
\usepackage{booktabs}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\cards}{ m }
 {
  \arne_card_distribution:n { #1 }
 }

\cs_new_protected:Nn \arne_card_distribution:n
 {
  \begin{tabular}{ *{5}{c} }
  \toprule
  & lente & zomer & herfst & winter \\
  \cmidrule{2-5}
  B & \clist_item:nn { #1 } { 1 }
    & \clist_item:nn { #1 } { 2 }
    & \clist_item:nn { #1 } { 3 }
    & \clist_item:nn { #1 } { 4 } \\
  G & \clist_item:nn { #1 } { 5 }
    & \clist_item:nn { #1 } { 6 }
    & \clist_item:nn { #1 } { 7 }
    & \clist_item:nn { #1 } { 8 } \\
  D & \clist_item:nn { #1 } { 9 }
    & \clist_item:nn { #1 } { 10 }
    & \clist_item:nn { #1 } { 11 }
    & \clist_item:nn { #1 } { 12 } \\
  S & \clist_item:nn { #1 } { 13 }
    & \clist_item:nn { #1 } { 14 }
    & \clist_item:nn { #1 } { 15 }
    & \clist_item:nn { #1 } { 16 } \\
  \bottomrule
  \end{tabular}
 }
\ExplSyntaxOff

\begin{document}

\cards{3,1,6,1,2,2,3,2,4,3,4,3,5,7,5,4}

\bigskip

\begin{tabular}{ c | c | c | c | c }
\hline
 & lente & zomer & herfst & winter \\ 
\hline 
B & 3 & 1 & 6 & 1 \\ 
G & 2 & 2 & 3 & 2 \\ 
D & 4 & 3 & 4 & 3 \\ 
S & 5 & 7 & 5 & 4 \\ 
\hline
\end{tabular}

\end{document}

If you don't like the booktabs way, then it should be easy to provide the necessary changes.

enter image description here

You must log in to answer this question.

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