4

I have a puzzling Incomplete \ifcase; all text was ignored after… error in that the same \ifcase is fine if I evaluate it at the largest integer for which it's defined, but if I evaluate it at a smaller integer, I get the error.

I show three versions. All are identical except (a) whether I comment out two lines of code for case "3" and (b) the integer at which the \ifcase is evaluated.

My question: Why does Version 2 give me the Incomplete \ifcase error?

Version 1: Defined up to case "2" and evaluated at "2". No problem.

\documentclass{article}
%   When the \ifcase is defined up to case 2, and evaluated at 2, all is well.
\newcommand{\expandedmulticolumn}[1]{
\ifcase#1
%Zero case. Nothing to see here.
\or %case 1
\multicolumn{2}{c}{A}%
\or %case 2
\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B}%
%\or %case 3
%\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} & \multicolumn{2}{c}{C}%
\else
You've blown my mind.\\
\fi
}

\begin{document}

\begin{tabular}{|cc|cc|cc|} 
1 & 2 & 3 & 4 & 5 & 6\\
\expandedmulticolumn{2} \\
\end{tabular}

\end{document}

Version 2: Defined up to case "3" but still evaluated at "2" => Get the Incomplete \ifcase error.

\documentclass{article}
%   When the \ifcase is defined up to case 3, and evaluated at 2, error: 
\newcommand{\expandedmulticolumn}[1]{
\ifcase#1
%Zero case. Nothing to see here.
\or %case 1
\multicolumn{2}{c}{A}%
\or %case 2
\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B}%
\or %case 3
\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} & \multicolumn{2}{c}{C}%
\else
You've blown my mind.\\
\fi
}

\begin{document}

\begin{tabular}{|cc|cc|cc|} 
1 & 2 & 3 & 4 & 5 & 6\\
\expandedmulticolumn{2} \\
\end{tabular}

\end{document}

enter image description here

Version 3: Defined up to case "3" and evaluated at "3". No problem.

\documentclass{article}
%   When the \ifcase is defined up to case 3, and evaluated at 3, all is well again. 
\newcommand{\expandedmulticolumn}[1]{
\ifcase#1
%Zero case. Nothing to see here.
\or %case 1
\multicolumn{2}{c}{A}%
\or %case 2
\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B}%
\or %case 3
\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} & \multicolumn{2}{c}{C}%
\else
You've blown my mind.\\
\fi
}

\begin{document}

\begin{tabular}{|cc|cc|cc|} 
1 & 2 & 3 & 4 & 5 & 6\\
\expandedmulticolumn{3} \\
\end{tabular}

\end{document}
4

Consider the simpler situation

\documentclass{article}

\begin{document}

\begin{tabular}{cc}
\ifcase 1 \or x&y\or x&y\fi
\end{tabular}

\end{document}

Since the integer is 1, TeX skips over the case zero and leaves in the input stream

x&y\or x&y\fi

Conditionals in tabular material are always complicated, because explicit & are scanned in order to keep track of cells and this code has the \fi too “far away” (in a different cell), whereas TeX wants to complete the \ifcase before the current cell ends.

Here's a different solution, in which TeX will only “see” the tokens corresponding to the chosen case:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\expandedmulticolumn}{m}
 {
  \int_case:nnF { #1 }
   {
    {1}{\multicolumn{2}{c}{A}}
    {2}{\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B}}
    {3}{\multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} & \multicolumn{2}{c}{C}}
   }
   {You've~blown~my~mind.}
}
\ExplSyntaxOff

\begin{document}

\begin{tabular}{|cc|cc|cc|}
1 & 2 & 3 & 4 & 5 & 6 \\
\expandedmulticolumn{3} \\
\end{tabular}

\end{document}

enter image description here

You're still hiding what you want to actually do. Leaving aside forloop is a good step. I'm certain that expl3 will help you much more, but without more information…

|improve this answer|||||
  • this is a perfect solution for what I need. I haven't been trying to "hide the ball" ("You're still hiding what you want to actually do…"), but to create a MWE I wound up abstracting away from what I wanted to do. This solution, however, is exactly what I need to do. – Jim Ratliff Jul 17 '18 at 14:18
9

if the value is 1 then the & which are being skipped over still end the table cell and the current group and the rest of the or and matching \fi are not seen until a later table cell. You need to hide the table markup in a macro or group so that it is only seen in the case that it is used.

\documentclass{article}
%   When the \ifcase is defined up to case 3, and evaluated at 2, error: 
\def\zz{&}
\newcommand{\expandedmulticolumn}[1]{
\ifcase#1
%Zero case. Nothing to see here.
\or %case 1
\multicolumn{2}{c}{A}%
\or %case 2
\multicolumn{2}{c}{A} \zz \multicolumn{2}{c}{B}%
\or %case 3
\multicolumn{2}{c}{A} \zz \multicolumn{2}{c}{B} \zz \multicolumn{2}{c}{C}%
\else
You've blown my mind.\\
\fi
}

\begin{document}

\begin{tabular}{|cc|cc|cc|} 
1 & 2 & 3 & 4 & 5 & 6\\
\expandedmulticolumn{2} \\
\end{tabular}

\end{document}
|improve this answer|||||

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.