ifnum:表格中多列的问题

ifnum:表格中多列的问题

下一个代码有错误

\documentclass{article}
\newcommand{\numtest}{1}
\begin{document}
\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\ifnum \numtest=1 \multicolumn{2}{c}{Long Text} \else text E & text F \fi
\end{tabular}
\end{document}

这个想法是\multicolumn在某些情况下选择命令。此外,我希望\numtest命令有多个值,例如

\ifnum \numtest=1 OR \numtest=5 OR \numtest=17 \multicolumn{2}{c}{Long Text} \else text E & text F \fi

我知道我可以用嵌套来实现ifnum,但有人知道另一种更简单的方法吗?

答案1

您不能在一个单元格中开始一个条件而在另一个单元格中结束它,如果测试为假就会发生这种情况。

您可以\ifboolexpe使用etoolbox

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\numtest}{1}

\begin{document}

\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\ifboolexpe{
  test {\ifnumequal{\numtest}{1}}
  or
  test {\ifnumequal{\numtest}{5}}
  or
  test {\ifnumequal{\numtest}{17}}
}{\multicolumn{2}{c}{Long Text}}{text E & text F}
\end{tabular}

\bigskip
\renewcommand{\numtest}{17}

\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\ifboolexpe{
  test {\ifnumequal{\numtest}{1}}
  or
  test {\ifnumequal{\numtest}{5}}
  or
  test {\ifnumequal{\numtest}{17}}
}{\multicolumn{2}{c}{Long Text}}{text E & text F}
\end{tabular}

\bigskip
\renewcommand{\numtest}{42}

\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\ifboolexpe{
  test {\ifnumequal{\numtest}{1}}
  or
  test {\ifnumequal{\numtest}{5}}
  or
  test {\ifnumequal{\numtest}{17}}
}{\multicolumn{2}{c}{Long Text}}{text E & text F}
\end{tabular}
\end{document}

在此处输入图片描述

另一种方法是使用expl3

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\gitanotest}{mmmm}
 {
  \int_case:nnTF { #1 } { #2 } { #3 } { #4 }
 }
\ExplSyntaxOff

\newcommand{\numtest}{1}

\begin{document}

\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\gitanotest{\numtest}{
  {1}{}
  {5}{}
  {17}{}
}{\multicolumn{2}{c}{Long Text}}{text E & text F}
\end{tabular}

\bigskip
\renewcommand{\numtest}{17}

\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\gitanotest{\numtest}{
  {1}{}
  {5}{}
  {17}{}
}{\multicolumn{2}{c}{Long Text}}{text E & text F}
\end{tabular}

\bigskip
\renewcommand{\numtest}{42}

\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\gitanotest{\numtest}{
  {1}{}
  {5}{}
  {17}{}
}{\multicolumn{2}{c}{Long Text}}{text E & text F}
\end{tabular}
\end{document}

的论点\gitanotest

  1. 测试数量
  2. 匹配的案例;注意每个案例后面的空括号组
  3. 找到匹配项时执行的代码
  4. 如果没有找到匹配项则执行的代码

空组实际上可以包含针对特定匹配执行的代码;在这里并不是很有用,但在其他应用程序中它们可能会有用。

输出与以前完全相同。

答案2

您想将 if 分组保留在单个单元格内,您可以\if通过各种方式实现或嵌套,示例测试 1 或 17。

在此处输入图片描述

\documentclass{article}


\makeatletter
\newcommand\posmulcol[1]{%
\ifnum1=\numexpr0#1\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother

\begin{document}

\newcommand{\numtest}{1}
\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\posmulcol{\ifnum\numtest=1 1 \else\ifnum\numtest=17 1 \fi\fi}{\multicolumn{2}{c}{long text}}{text E &text F}
\end{tabular}

\bigskip

\renewcommand{\numtest}{8}
\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\posmulcol{\ifnum\numtest=1 1 \else\ifnum\numtest=17 1 \fi\fi}{\multicolumn{2}{c}{long text}}{text E &text F}
\end{tabular}

\bigskip

\renewcommand{\numtest}{17}
\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\posmulcol{\ifnum\numtest=1 1 \else\ifnum\numtest=17 1 \fi\fi}{\multicolumn{2}{c}{long text}}{text E &text F}
\end{tabular}

\end{document}

答案3

您可以将其存储在命令中

\documentclass{article}
\newcommand{\numtest}{1}
  \def\mt{\multicolumn{2}{c}{Long Text}}
  \def\mtt{text E & text F}
\begin{document}
\begin{tabular}{ll}
text A & text B\\
text C & text D\\
\multicolumn{2}{c}{Long Text}\\
\ifnum \numtest=1  \mt\else  \mtt\fi
\end{tabular}
\end{document}

相关内容