4

I'm trying to create the following table, but the first cell has a black background. How can I get rid of it? I need alternate colors and multiple rows feature provided by tabularx.

enter image description here

\documentclass[]{article}

\usepackage[table]{xcolor}
\usepackage{tabularx}  

\begin{document}

\begin{table}[h]
\centering
\caption{Points} 
\label{tab:Points}
\begin{tabularx}{.9\textwidth}{cccX}
  \hline
\textbf{Heading 1} & \textbf{Heading 2} & \textbf{Heading 3} & \textbf{Heading 4} \\ 
  \hline
    1 & a & 10 & Some text, Some text, Some text, Some text, Some text  \\ 
   \rowcolor[gray]{0.90}1 & b & 4 &  \\ 
  1 & c & 4 &  \\ 
   \rowcolor[gray]{0.90}1 & d & 4 & Some other text, Some other text \\ 
  1 & e & 4 &  \\ 
   \hline
Total &  & 26 &  \\ 
   \rowcolor[gray]{0.90} \hline
\end{tabularx}
\end{table}

\end{document}
  • 2
    It comes from the last, strangely-located, \rowcolor[gray]{0.90}... – Werner Mar 4 '16 at 17:38
3

The problem is cause by the \rowcolor command in the last line of your {tabular}. Just remove it ;-)

By the way. You could use \rowcolors to automatically change the colors of the rows. Furthermore I recommend to use booktabs and \(top|mid|bottom)rule instead of \hline.

\documentclass[]{article}

\usepackage{tabularx}
\usepackage[table]{xcolor}

% fix for \rowcolors in {tabularx}, see https://tex.stackexchange.com/a/297357/4918
\newcounter{tblerows}
\expandafter\let\csname c@tblerows\endcsname\rownum

\usepackage{booktabs}

\begin{document}

\begin{table}[h]
   \centering
   \caption{Points} 
   \label{tab:Points}
   \rowcolors{2}{}{black!10}
   \begin{tabularx}{.9\textwidth}{cccX}
      \toprule
      \textbf{Heading 1} & \textbf{Heading 2} & \textbf{Heading 3} & \textbf{Heading 4} \\ 
      \midrule
      1 & a & 10 & Some text, Some text, Some text, Some text, Some text  \\ 
      1 & b & 4 &  \\ 
      1 & c & 4 &  \\ 
      1 & d & 4 & Some other text, Some other text \\ 
      1 & e & 4 &  \\ 
      \midrule
      Total &  & 26 &  \\ 
      \bottomrule
   \end{tabularx}
\end{table}

\end{document}

For some reason with {tabularx} the {2} argument of \rowcolors is ignored, which is not the case for {tabluar} … I ask a new question regarding this issue: Why is the start row of \rowcolors ignored in tabularx?

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.