0

When I write the template below, where:

  • the first parameter has been designed for optional headers:
  • the second has been designed to pass to tabularx.

I get the following error: Forbidden control sequence found while scanning use of @tempc. \end{extabularx}

\newenvironment{extabularx}[2][]{%
    \rowcolors{1}{gray!25}{white}%
    \rownum=0%
    \tabularx{\linewidth}{#2}%
        \toprule%
        %\ifx\empty#1\relax\else(\rowcolor{gray!50} #1\tabularnewline)\fi%
        \ifthenelse{\empty#1}{%
            \relax
        }{%
            \rowcolor{gray!50}%
            #1\tabularnewline%
            \midrule%
        }%
}{%
        \bottomrule%
    \endtabularx%
}

Without IF statements it works fine, while with the \if or \ifx or \ifthenelse statements it doesn't work.

  • 1
    You're beginning an \if - statement in one table cell and try to finish it in another one, either by using & in #1 or ultimately, when \tabularnewline is issued. This can't work, at least not this way – user31729 Jan 6 at 10:10
  • @ChristianHupfer, Exists another way? – BnG Jan 6 at 10:12
  • Yes, Black TeX Magic ... with \noalign and constructing a token list containing the \if.... test and then inserting the whole token list as first table row. There are examples of this here at TeX.SE – user31729 Jan 6 at 10:16
  • Latex is really complicated. With C there are less problems :-) – BnG Jan 6 at 10:17
  • 1
    Not if you try to do everything with preprocessor directives which would be closer to TeX processing than this strange language ;) – TeXnician Jan 6 at 10:18
5

As you have provided no test file or example of how the command is used it is hard to help in detail but

%\ifx\empty#1\relax\else(\rowcolor{gray!50} #1\tabularnewline)\fi%

This is syntactically correct but tests if the first token in #1 is the token \empty if it is, that token is removed but remaining tokens of # are executed, followed by \relax. If #1 does not start with \empty then it typesets a ( followed by \rowcolor which will always give an error as it has to be the first command in the row, but is following ( here.

That was probably not the test intended (you mention it doesn't work but leave us to guess what you wanted it to do)

\ifthenelse{\empty#1}{%
    \relax
}{%
    \rowcolor{gray!50}%
    #1\tabularnewline%
    \midrule%
}%

\ifthenelse does not work via expansion so you do not want to use this here. the test clause with \empty is wrong but even if that were fixed, you would not be able to use \rowcolor or \multicolumn etc that need to be at the start of the row.

Possibly you want to replace all of the code I highlighted by

    \if\relax\detokenize{#1}\relax
     \expandafter\@gobble
   \else
     \expandafter\@firstofone
    \fi
    {\rowcolor{gray!50} #1\tabularnewline\midrule}%

assuming @ is a letter when you make the definitiuon (so in a package or after \makeatletter in the preamble)

That would use an expandable test to check if #1 is empty, and use it to make a grey heading row if not empty.

  • No TeX Black Magic with \noalign here? – user31729 Jan 6 at 17:26
  • @ChristianHupfer that is supplied by \rowcolor – David Carlisle Jan 6 at 17:31
  • @DavidCarlisle I have no words. It's incomprehensible, for now, but it works! The next step is to understand what this macro does and how using \expandafter. Thanks. – BnG Jan 7 at 21:05

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.