我有一个令人费解的错误,如果我以定义的最大整数来评估它,Incomplete \ifcase; all text was ignored after…
那么结果就很好,但如果我以较小的整数来评估它,就会出现错误。\ifcase
我展示了三个版本。除了 (a) 我是否注释掉案例“3”的两行代码和 (b) 评估的整数之外,所有版本都相同\ifcase
。
我的问题:为什么版本 2 会出现错误Incomplete \ifcase
?
版本 1:定义到情况“2”,并在“2”处进行评估。没问题。
\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}
版本 2:定义到情况“3”,但仍在“2”处评估=> 获取错误Incomplete \ifcase
。
\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}
版本 3:定义到情况“3”,并在“3”处进行评估。没问题。
\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}
答案1
考虑更简单的情况
\documentclass{article}
\begin{document}
\begin{tabular}{cc}
\ifcase 1 \or x&y\or x&y\fi
\end{tabular}
\end{document}
由于整数为 1,TeX 跳过零情况并留在输入流中
x&y\or x&y\fi
表格材料中的条件总是很复杂,因为&
为了跟踪单元格而扫描显式的,并且此代码的\fi
“距离”太远(在不同的单元格中),而 TeX 希望在\ifcase
当前单元格结束之前完成。
这是一个不同的解决方案,其中 TeX 只会“看到”与所选情况相对应的标记:
\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}
你还在隐藏你真正想做的事情。抛开forloop
是很好的一步。我确信这expl3
会对你更有帮助,但没有更多的信息……
答案2
如果值为 1,则&
跳过仍然结束表格单元格和当前组,其余的和or
匹配的\fi
直到后面的表格单元格才会显示。您需要隐藏宏或组中的表格标记,以便仅在使用时才可见。
\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}