[Tex/LaTex] Misplaced noalign when using rowcolor

colorerrorsrowcolorrulestables

I am trying to create a table with colored rows and colored hrules between the rows, but I am getting a "Misplaced \noalign. \rowcolor" error.

The following example reproduces this problem:

\documentclass{article}

\usepackage{color, colortbl}

\begin{document}
  \begin{tabular}{l}
                    x \\ \noalign{\color{red}\hrule height 1pt}
    \rowcolor{blue} x
  \end{tabular}
\end{document}

The error disappears if I either remove the \noalign{…} or the \rowcolor.

How can I use both (or get the same result in a different way)?

Best Answer

You need to keep the colour restore within the noalign so need {} but colortbl allows you to colour \hline, so do you need this?

\documentclass{article}

\usepackage{color, colortbl}

\begin{document}
  \begin{tabular}{l}
                    x \\ \noalign{{\color{red}\hrule height 1pt}}
    \rowcolor{blue} x
  \end{tabular}
\end{document}