LaTeX forum ⇒ GeneralProblem using conditionals inside tabular & changing columns

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

Problem using conditionals inside tabular & changing columns

Postby EmilioLazo » Sat Mar 30, 2013 11:19 pm

Hello!

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

  1. \documentclass[test]{article}
  2.  
  3. \makeatletter
  4. \DeclareOption{test}{\@testtrue}
  5. \ProcessOptions
  6. \newcommand{\Item}{\if@test t&t&t&t \else 21&22&23&24 \fi}
  7. \makeatother
  8.  
  9. \begin{document}
  10. \begin{tabular}{cccc}
  11. 11 & 12 & 13 & 14 \\
  12. \Item
  13. \end{tabular}
  14. \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:

  1. \documentclass[test]{article}
  2.  
  3. \usepackage{ifthen}
  4. \usepackage{etoolbox}
  5.  
  6. \makeatletter
  7. \def\XXX{i}
  8. \newif\if@test \@testfalse
  9. \newtoggle{test}
  10. \DeclareOption{test}{\@testtrue\def\XXX{j}\toggletrue{test}}
  11. \ProcessOptions
  12. \newcommand{\Item}{\if@test t&t&t&t \else 21&22&23&24 \fi}
  13. \newcommand{\ItemI}{\ifthenelse{\equal{\XXX}{j}}{t&t&t&t}{31&32&33&34}}
  14. \newcommand{\ItemE}{\iftoggle{test}{t&t&t&t}{41&42&43&44}}
  15. \makeatother
  16.  
  17. \begin{document}
  18. \begin{tabular}{cccc}
  19. 11 & 12 & 13 & 14 \\
  20. \Item \\
  21. \ItemI \\
  22. \ItemE
  23. \end{tabular}
  24. \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.

Return to “General”

Who is online

Users browsing this forum: No registered users and 8 guests