GeneralProblem using conditionals inside tabular & changing columns

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Problem using conditionals inside tabular & changing columns

Post by EmilioLazo »

Hello!

I wrote this code to expose the issue I'm facing:

Code: Select all

\documentclass[test]{article}

\makeatletter
 \DeclareOption{test}{\@testtrue}
 \ProcessOptions
 \newcommand{\Item}{\if@test t&t&t&t \else 21&22&23&24 \fi}
\makeatother

\begin{document}
\begin{tabular}{cccc}
11 & 12 & 13 & 14 \\
\Item
\end{tabular}
\end{document}
I would expect that if option 'test' is supplied to the class, a row with one 't' in every cell will be written, and nothing more; but it isn't the case, the 'else' condition is also evaluated. (?)

The errors were:
! Incomplete \iftrue; all text was ignored after line 12.
! Extra alignment tab has been changed to \cr
! Extra \fi.
! Missing \cr inserted.
...

The problem here is the presence of '&' inside \if conditional, but with ifthen or etoolbox the issue disappears:

Code: Select all

\documentclass[test]{article}

\usepackage{ifthen}
\usepackage{etoolbox}

\makeatletter
 \def\XXX{i}
 \newif\if@test \@testfalse
 \newtoggle{test}
 \DeclareOption{test}{\@testtrue\def\XXX{j}\toggletrue{test}}
 \ProcessOptions
 \newcommand{\Item}{\if@test t&t&t&t \else 21&22&23&24 \fi}
 \newcommand{\ItemI}{\ifthenelse{\equal{\XXX}{j}}{t&t&t&t}{31&32&33&34}}
 \newcommand{\ItemE}{\iftoggle{test}{t&t&t&t}{41&42&43&44}}
\makeatother

\begin{document}
\begin{tabular}{cccc}
11 & 12 & 13 & 14 \\
\Item \\
\ItemI \\
\ItemE
\end{tabular}
\end{document}
What is happening here? How this can be fixed using \if@test-construct? I did try inserting braces for both 'then' and 'else', and the whole \if inside braces but nothing changes!

Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply