5

This table environment mymulticolumn has three arguments, the first of which sets an optional \arraystretch, the second of which sets the "apparent" number of columns (either 2 or 3), and the last of which sets the width of certain columns. The code generates the error indicated in the title of my question, which in turn prompts my question: why?

\documentclass{article} 

\usepackage{array}
\usepackage{ifthen}

%column specifications
\newcommand{\cs}{\hspace{.5em}}
\newcolumntype{x}{>{\raggedleft\hspace{0pt}}p{1.5em}}

%mymulticolumn
\newenvironment{mymulticolumn}[3][2]{%
\renewcommand{\arraystretch}{#1}
\begin{tabular}
\ifthenelse{#2=2}
{ {@{} x @{\cs} p{#3} x @{\cs} l} }
{ {@{} x @{\cs} p{#3} x @{\cs} p{#3} x @{\cs} l} } }%
{\end{tabular}}

\begin{document}

\begin{mymulticolumn}{2}{1.5in}
1. & $f(x)=x^2$ & 
2. & $f(x)=\sqrt{x}$ \\
3. & $f(x)=\ln x$ &
4. & $f(x)=\tan x$
\end{mymulticolumn}

\end{document} 

1 Answer 1

5

It can't work for several reason, the main one being that just \ifthenelse becomes the argument to \begin{tabular}. The second reason is that with array the argument to \begin{tabular} is not expanded, the third is that even if expansion was peformed, it wouldn't work, because \ifthenelse is not “fully expandable”.

Put the \ifthenelse outside:

\newenvironment{mymulticolumn}[3][2]
 {%
  \renewcommand{\arraystretch}{#1}%
  \ifthenelse{#2=2}
    {%
     \begin{tabular}{@{} x @{\cs} p{#3} x @{\cs} l}%
    }
    {%
     \begin{tabular}{@{} x @{\cs} p{#3} x @{\cs} p{#3} x @{\cs} l}%
    }%
 }%
 {\end{tabular}}

You must log in to answer this question.

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