4

I want to use row coloring with \multicolumn. The MWE below works, but if I change to the commented, colored line, I receive a "misplaced \noalign". What do I wrong?

\documentclass{article}
\usepackage{booktabs,colortbl,xcolor}
\begin{document}

\begin{tabular}{p{1.7cm}p{1.3cm}p{1.3cm}p{1.35cm}p{1.35cm}p{1.35cm}p{1.35cm}p{1.7cm}} %
\toprule
% \multicolumn{1}{>{\rowcolor{lightgray}}c}{}  &\multicolumn{7}{>{\rowcolor{lightgray}}c}{String length}\\
 \multicolumn{1}{c}{}  &\multicolumn{7}{c}{String length}\\
\rowcolor{lightgray} Function & 16,384 & 32,768& 65,536 & 130,072 & 262,144 & 524,288& 1,048,576\\
\midrule
lower1  & 0.19 & 0.77 & 3.08 & 12.24 & 49.39 & 198.42 & 791.22\\
lower2& 0.0000 &  0.0000 & 0.0001 & 0.0002 & 0.0004 & 0.0008 & 0.0015\\
\bottomrule
\end{tabular}

\end{document}

I was experimenting with a solution like this, because I receive strange table heading, like below. The code you suggested works fine in MWE, but in my real text, with copy&paste from the answer, it produces the strange heading. Any idea, what can change the behavior? enter image description here

  • Can you maybe accept an answer to mark the question as resolved, if any was helpful? – MERose Nov 14 '15 at 13:26
3

\rowcolor are for rows and is used at the beginning of a new row. When using \multicolumn, you are technically referring to cells. So, use \cellcolor. Also, I would create a new \toprule and \midrule to combine the header colour with the rules.

enter image description here

\documentclass{article}
\usepackage{booktabs,colortbl,xcolor}

\newcommand{\toprulec}{\arrayrulecolor{black}\specialrule{0.1em}{\abovetopsep}{0pt}%
            \arrayrulecolor{lightgray}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\midrulec}{\arrayrulecolor{lightgray}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}

\begin{document}

\begin{tabular}{ p{1.7cm} *{2}{p{1.3cm}} *{4}{p{1.35cm}} p{1.7cm} } %
  \toprulec
  \multicolumn{1}{c}{\cellcolor{lightgray}} &\multicolumn{7}{c}{\cellcolor{lightgray}String length} \\
  \rowcolor{lightgray} Function & 16,384 & 32,768& 65,536 & 130,072 & 262,144 & 524,288& 1,048,576 \\
  \midrulec
  lower1  & 0.19 & 0.77 & 3.08 & 12.24 & 49.39 & 198.42 & 791.22\\
  lower2& 0.0000 &  0.0000 & 0.0001 & 0.0002 & 0.0004 & 0.0008 & 0.0015\\
  \bottomrule
\end{tabular}

\end{document}
2

\rowcolor should be given at the start of a row, not in the way you're trying to use it.

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{p{1.7cm}p{1.3cm}p{1.3cm}p{1.35cm}p{1.35cm}p{1.35cm}p{1.35cm}p{1.7cm}}
\toprule
\rowcolor{lightgray}& \multicolumn{7}{c}{String length}\\
\rowcolor{lightgray}%
Function & 16,384 & 32,768& 65,536 & 130,072 & 262,144 & 524,288& 1,048,576\\
\midrule
lower1 & 0.19 & 0.77 & 3.08 & 12.24 & 49.39 & 198.42 & 791.22\\
lower2 & 0.0000 &  0.0000 & 0.0001 & 0.0002 & 0.0004 & 0.0008 & 0.0015\\
\bottomrule
\end{tabular}

\end{document}

enter image description here

I'd avoid the \midrule, which serves no purpose.

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.