3

The next code has an error

\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}

The idea is choose \multicolumn command in some cases. Furthermore, I would like to have several values for \numtest command, for example

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

I know I can do it with nested ifnum, but somebody knows another easier way?

0
4

You can't start a conditional in a cell and end it in another one, which would happen if the test is false.

You can use \ifboolexpe of 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}

enter image description here

A different approach uses 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}

The arguments to \gitanotest are

  1. The number to test
  2. The matching cases; note the empty brace group after each one
  3. The code to execute if a match is found
  4. The code to execute if no match is found

The empty groups can actually contain code to execute for the particular match; not really useful here, but in other applications they may be.

The output is exactly the same as before.

1
  • Thanks, the option with \ifboolexpe of etoolbox works perfect and meets my needs – Gitano Feb 4 '15 at 0:33
3

You want to keep the if grouping within a single cell, you can implement or by nesting \if in various ways, the example tests 1 or 17.

enter image description here

\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}
2

you can store it in commands

\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}

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.