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

2 Answers2

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.

Werner
  • 603,163
  • Now I understand the command principe! May way of learning LaTeX is more the "copy-way". Seeing, copying, adapting, understanding... but sometimes the last step is missing. Didn't knew booktabs... Thanks for the tip and https://www.inf.ethz.ch/personal/markusp/teaching/guides/guide-tables.pdf opened my eyes – Arne Timperman Oct 17 '15 at 07:35
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

egreg
  • 1,121,712