2

I have tried (and researched) many ways in getting this to work correctly, but have ultimately failed.

I am attempting to have different commands call new rows with each command giving a different color row (as well as spacing) depending on certain factors.

In one of the commands (very similar to another command that works or if placed in an \ifthenelse block) I get an error:

! Misplaced \noalign.
\rowcolor ->\noalign 
                     {\ifnum 0=`}\fi \global \let \CT@do@color \CT@@do@color...
l.56     \newSpecialItem[0]{d}{e}{f}

Updating code: The environment with command code is as follows:

\documentclass{article}

\usepackage{longtable,tabularx,ltxtable}
\usepackage{ifthen}
\usepackage[table]{xcolor}

\newcounter{sCounter}

\newenvironment{myEnviron}
{
    % Works
    \newcommand{\newBasicItem}[3] {
        \rowcolor{green}
        ##1 & ##2 & ##3 \\ \hline
    }

    % works as long as row color is in this order and outside the if block
    \newcommand{\newMainItem}[4] {
        \rowcolor{gray}       
        \setcounter{sCounter}{##1}
        \hspace{\arabic{sCounter}mm}##2 & ##3 & ##4 % \\ \hline
        \addtocounter{sCounter}{8}
        \\ \hline
     }

    \newcommand{\newSpecialItem}[4][0] {
        \rowcolor{blue}  % <-- Failing 
        \ifthenelse{\equal{##1}{0}} {} {
            \addtocounter{sCounter}{-8}
        }
        \hspace{\arabic{sCounter}mm}##2 & ##3 & ##4 \\ \hline
    }

    \renewcommand{\arraystretch}{1.5}
    \begin{longtable}[l]{|p{2in}|p{2in}|p{2in}|}
    \hline
    \rowcolor{green}
    \textbf{\color{white}Name} & \textbf{\color{white}Name2} & \textbf{\color{white}Name3} \\ \hline
    \endhead
}
{ % end environment (myEnviron)
    \end{longtable}
}


\begin{document}


\section{attempt}

\begin{myEnviron} 
    \newMainItem{0}{a}{b}{c}
    \newSpecialItem[1]{d}{e}{f}
    \newBasicItem{g}{h}{i}

\end{myEnviron}


\end{document}

The \newSpecialItem call is the one that always fails unless I remove the \rowcolor call,

Any advice is greatly appreciated, still new to LaTeX.

4
  • 1
    You can not have \ifthenelse (or any other command that does not work via expansion) before \rowcolor or \multicolumn or other similar commands that have to be at the start of a cell. Sep 11, 2019 at 18:57
  • 1
    It would be much easier to answer your question if you said in words what you wanted the code to do and provided a complete small test file not separate fragments that anyone testing answers has to piece together. Sep 11, 2019 at 19:20
  • @DavidCarlisle thank you for the info, I updated the question to hopefully better explain. I will look more into the expansion comment.
    – Wartree
    Sep 11, 2019 at 20:58
  • I was able to get this to work by removing the optional argument from the \newSpecialItem command and make it required. However I am unclear as the reasoning. I would prefer an optional argument if possible.
    – Wartree
    Sep 12, 2019 at 22:17

1 Answer 1

0

The issue, as you have discovered, is the optional argument in \newSpecialItem. This triggers some code for a check to see if the argument is present which causes the \noalign in \rowcolor to fail.

In the following the call to \rowcolor is replaced by the code from the implementation of \rowcolor but without the \noalign command (among other things). Removing this may have unwanted side effects though.

MWE:

\documentclass{article}

\usepackage{longtable,tabularx,ltxtable}
\usepackage{ifthen}
\usepackage[table]{xcolor}

\newcounter{sCounter}
\makeatletter
\newenvironment{myEnviron}
{
    % Works
    \newcommand{\newBasicItem}[3] {
        \rowcolor{green}
        ##1 & ##2 & ##3 \\ \hline
    }

    % works as long as row color is in this order and outside the if block
    \newcommand{\newMainItem}[4] {
        \rowcolor{gray}       
        \setcounter{sCounter}{##1}
        \hspace{\arabic{sCounter}mm}##2 & ##3 & ##4 % \\ \hline
        \addtocounter{sCounter}{8}
        \\ \hline
     }
    \newcommand{\newSpecialItem}[4][0] {%
        \let\CT@do@color\CT@@do@color\gdef\CT@row@color{\CT@color{blue}}%
        \ifthenelse{\equal{##1}{0}}{}{%
            \addtocounter{sCounter}{-8}%
        }%
        \hspace{\arabic{sCounter}mm}##2 & ##3 & ##4 \\ \hline
    }

    \renewcommand{\arraystretch}{1.5}
    \begin{longtable}[l]{|p{2in}|p{2in}|p{2in}|}
    \hline
    \rowcolor{green}
    \textbf{\color{white}Name} & \textbf{\color{white}Name2} & \textbf{\color{white}Name3} \\ \hline
    \endhead
}
{ % end environment (myEnviron)
    \end{longtable}
}
\makeatother

\begin{document}

\section{attempt}

\begin{myEnviron}
    \newMainItem{0}{a}{b}{c}
    \newSpecialItem[1]{d}{e}{f}
    \newBasicItem{g}{h}{i}

\end{myEnviron}

\end{document}

Result:

enter image description here

Note that the added code is full of @ symbols, which require the use of \makeatletter and \makeatother before and after the environment definition. Furthermore some end of line % symbols have been added to prevent unwanted extra space.

3
  • Thank you for the fix, that helps greatly and no side affects (yet)! Can I ask if there is a reason you put the \makeatletter and \makeatother surrounding the environment and not just surrounding the \let portion?
    – Wartree
    Sep 13, 2019 at 16:03
  • The position of the at commands is a bit tricky, the commands should be 'active' at the moment that you use the @ in your code. Here the @ is used during the environment definition, therefore you must change the category of @ to letter before you start defining the environment. If you put \makeatletter in the environment code itself then it is not executed during definition of the environment but only when the environment is actually executed, and then it is too late because the commands are collected/tokenized during definition.
    – Marijn
    Sep 13, 2019 at 19:27
  • Thank you very much for the explanation!
    – Wartree
    Sep 13, 2019 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .