Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have the commands:

\def\tabfive{%
    \renewcommand{\arraystretch}{1.1}
    \begin{tabular}{p{0.18\linewidth}p{0.18\linewidth}p{0.18\linewidth}p{0.18\linewidth}p{0.18\linewidth}}
    \fiveoptions
    }
\def\tabfour{%
    \renewcommand{\arraystretch}{1.1}
    \begin{tabular}{p{0.23\linewidth}p{0.23\linewidth}p{0.23\linewidth}p{0.23\linewidth}}
    \fouroptions
    }
\def\tabend{\end{tabular}}

\def\fouroptions #1&#2&#3&#4\\ { a) #1&b) #2&c) #3&d) #4~}
\def\fiveoptions #1&#2&#3&#4&#5\\ { a) #1&b) #2&c) #3&d) #4&e) #5~}

The usage is:

\tabfour
    10 &
    12 &
    15 &
    20\\
\tabend


\tabfive
    10 &
    12 &
    15 &
    18 &
    20\\
\tabend

Is there a way to join this two commands in a commands named \mytab, with LaTeX choosing the right command in function of the number of alternatives?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

You can have a variable number of items by rethinking your usage of a tabular and using an inline list.

\documentclass{article}
\makeatletter
\newcounter{inline}
\def\inlinelist#1{%
 \setcounter{inline}{0}%
 \@for\next:=#1\do{%
   \stepcounter{inline}%
   \alph{inline})\thinspace\next\space}
}
\makeatother
\begin{document}
\inlinelist{one,two,three,four,five, six}

\inlinelist{one,two,three}
\end{document}

enter image description here

This is a shorter solution, more flexible and less typing.

share|improve this answer

You could define a starred and unstarred macro so that \mytabs uses \tabfour and \mytabs* uses \tabfive. A good reference for this is Defining starred versions of commands.

However, I would recommend you use the inline list feature from the enumitem package:

enter image description here

\documentclass{article}
\usepackage[inline]{enumitem}
\begin{document}
\begin{enumerate*}[label=\alph*), itemjoin={\hspace*{5.0ex}}]
  \item 10
  \item 12
  \item 15
\end{enumerate*}

\begin{enumerate*}[label=\alph*), itemjoin={\hspace*{5.0ex}}]
  \item 10
  \item 12
  \item 15
  \item 18
  \item 20
\end{enumerate*}
\end{document}
share|improve this answer

If you want to keep with your syntax, then something like

\newcounter{answers}
\renewcommand\theanswers{\alph{answers}}
\def\tabendi{\tabend}
\def\tabgen#1\\ \tabend{\setcounter{answers}{0}\noindent\tabgeni#1& & \tabend& }
\def\tabgeni#1& {%
  \def\tempa{#1}%
  \ifx\tempa\tabendi
    \tabend
  \else
    \if\relax\detokenize{#1}\relax
    \else
      \makebox[.18\linewidth][l]{\stepcounter{answers}\theanswers)~#1}\hfill
    \fi
  \expandafter\tabgeni
  \fi
}
\def\tabend{\par}
\let\tabfour\tabgen
\let\tabfive\tabgen

and

\tabgen
    10 &
    12 &
    15 &
    20\\
\tabend

\tabgen
    10 &
    12 &
    15 &
    18 &
    20\\
\tabend

The old syntax would continue to work because of the to \let statements.

However, a different approach might be handier and more liberal for input:

\usepackage{xparse}

\newcounter{answers}
\renewcommand\theanswers{\alph{answers}}
\ExplSyntaxOn
\NewDocumentCommand{\results}{m}
 {
  \seq_set_split:Nnn \l_results_a_seq {,}{#1}
  \par\nobreak\noindent\setcounter{answers}{0}
  \seq_map_inline:Nn \l_results_a_seq
   {
    \makebox[.18\linewidth][l]{\stepcounter{answers}\theanswers)~##1}\hfill
   }
   \par
 }
\seq_new:N \l_results_a_seq
\ExplSyntaxOff

Then you can input your results as

\results{  
  10,  
  12,
  15,
  20
}

\results{
  10,
  12,
  15,
  18,
  20
}

that will produce

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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