1

I would like to colour every alternating row in every tabularx.

I know that I may gain this for all normal tabulars by using the following code in the preamble:

\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\let\oldtabular\tabular
\let\endoldtabular\endtabular
\renewenvironment{tabular}{\rowcolors{1}{}{lightgray}\oldtabular}{\endoldtabular}

I know that for a single tabularx, I may use

\rowcolors{1}{}{lightgray}
\begin{tabularx}{...}

Now I dont know how to combine these for a code in the preamble, to achieve the same as in the first block of code, just for tabularx.

Thanks!

1 Answer 1

1

The tricky part you need to know to make this work is that the internal macro \tabularx expects an argument while \tabular doesn't.

Here is a possible way

\documentclass{article}

\usepackage{tabularx}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}

\let\oldtabularx\tabularx
\renewcommand*{\tabularx}{\rowcolors{1}{}{lightgray}\oldtabularx}

\begin{document}

\begin{tabularx}{.5\linewidth}{lX}
foo & text text text text text text text \\
bar & text text text text text text text \\
baz & text text text text text text text \\
bla & text text text text text text text
\end{tabularx}

\end{document}

enter image description here

And now forget about this: a tabularx might be hidden in some package where you do not expect it. Instead, define a new environment

\newenvironment{ctabularx}[1]{% or whatever name you like
   \rowcolors{1}{}{lightgray}%
   \tabularx{#1}%
  }{%
   \endtabularx
}

You must log in to answer this question.

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