1

I've checked several questions (1, 2, 3, 4) about a problem with \if in a longtable (or tabular) environment but I haven't managed to get any smarter. I do have the following MWE:

\documentclass{article}

% some code normally residing in a .sty package \usepackage{longtable} \usepackage{array} \makeatletter \newif\if@test@testtrue % [[A % \newcommand{\thisline}[2]{\if@test\else{ \ #1 & #2 }\fi} % \newcommand{\thatline}[2]{\if@test \ #1 & #2 \else\fi} % ]] XOR [[B \newcommand{\thisline}[2]{\if@test\else \ #1 & #2 \fi} \newcommand{\thatline}[2]{\if@test{ \ #1 & #2 }\else\fi} @testfalse % ]]

% user-space code \begin{document} \begin{longtable}{ll} \thisline{a}{b} \thatline{c}{d} \end{longtable} \end{document}

The code works like this. However, if I set \@testfalse, I have to use block B instead of block A. Otherwise, I either get the "Missing } inserted" or the "Incomplete \iftrue; all text was ignored after line X" error. Obviously, I only want one of block A or B. So, what do I need to do in order to properly mask & (or \\, if that's the problem)?

mfg
  • 499
  • the error isn't from the \if really, in the false case you are generating a row { a & b \\} which would generate the error you show even without the conditional. What is the intention of adding the brace group? – David Carlisle Sep 17 '21 at 10:37
  • Thanks for your quick response, David. So, I'd like the code to work in both cases, with \@testfalse and without. It seemed to be the empty {}. If I remove all brace groups, it seems to work. Thanks. – mfg Sep 17 '21 at 10:46
  • the empty {} wouldn't error it's just adding an empty brace group in the same cell as a (although the space after it would affect the spacing) – David Carlisle Sep 17 '21 at 11:00
  • David: In fact, I abstracted the MWE from package code. I will replace the MWE by one that more closely matches the problem that remains after your recommendations. – mfg Sep 17 '21 at 11:19
  • ok but same answer you are adding brace groups (they are not delimiting arguments) and you can't have a brace group starting in one cell and finishing in another. – David Carlisle Sep 17 '21 at 11:28
  • Alright, I understand that, but how to I get the code working with \@testtrue and \@testfalse without using brace groups mutually in the unused/unexpanded if branch? – mfg Sep 17 '21 at 11:34
  • I'm happy to move the discussion to the chat. – mfg Sep 17 '21 at 11:41

1 Answers1

1

enter image description here

The error you show isn't from the conditional but from you starting a group with { in one row and trying to end it in the following row:

{ a & b \\}

Without the group you get the intended effect:

\documentclass{article}
\makeatletter
\newif\if@test\@testtrue
% \@testfalse
\begin{document}
\begin{tabular}{ll}
  % \if@test\else a & b \\ \fi
  \if@test\else a & b \\ \fi
  \if@test        c & d \\ \else\fi
\end{tabular}

@testfalse

\begin{tabular}{ll} % \if@test\else a & b \ \fi \if@test\else a & b \ \fi \if@test c & d \ \else\fi \end{tabular}

\end{document}


After the question was clarified I suspect you are looking for something like

with \@testfalse: enter image description here

without \@testfalse: enter image description here

\documentclass{article}
\usepackage{longtable}
\usepackage{array}
\makeatletter
\newif\if@test\@testtrue
\newcommand{\thisline}[2]{\if@test\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi{}{ \\ #1 & #2 }}
\newcommand{\thatline}[2]{\if@test\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi{ \\ #1 & #2 }{}}
\@testfalse % <-- un-/comment

\begin{document} \begin{longtable}{ll} \thisline{a}{b} \thatline{c}{d} \end{longtable} \end{document}

mfg
  • 499
David Carlisle
  • 757,742
  • 1
    this works in this simple case but the interaction of \if conditionals and expansion of the \halign template inplementing the table is "interesting" and I'd avoid this design if possible. It is inherently fragile. – David Carlisle Sep 17 '21 at 10:44