开发者

Alternating row colors in tabular(*|x)?

I’m trying to create a table in my document that resembles more or less the table in the picture below:

Alternating row colors in tabular(*|x)?

This table is supposed to be stretched horizontally to \textwidth. My first attempt with tabular* looked like this:

\documentclass{scrartcl}
\usepackage[table]{xcolor}
\definecolor{tableShade}{gray}{0.9}

\begin{document}
  \rowcolors{3}{tableShade}{white}  %% start alternating shades from 3rd row
  \noindent\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lrrr}
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
  \end{tabular*}
\end{document}

The result was:

Alternating row colors in tabular(*|x)?

Well, the alternate row coloring works but tabular* inserts space between columns to stretch the whole table to \textwidth. Browsing through my LaTeX companion I found that tabularx should be able to do what I want. So I changed my code to look like that:

\documentclass{scrartcl}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\definecolor{tableShade}{gray}{0.9}

\begin{document}
  \rowcolors{3}{tableShade}{white}  %% start alternating shades from 3rd row
  \noindent\begin{tabularx}{\textwidth}{Xrrr}
    Something & foo &am开发者_如何学JAVAp; bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
  \end{tabularx}
\end{document}

Now, this looks more like it. But tabularx ignores the starting row for the coloring and starts with the first row.

Alternating row colors in tabular(*|x)?

Now I’ve run out of ideas. Any suggestions?


Not a fix but a hack around, add \hiderowcolors to the first row, then turn colors back on with \showrowcolors. See code:

\rowcolors{3}{tableShade}{white}  %% start alternating shades from 3rd row
  \noindent\begin{tabularx}{\textwidth}{X X X X}%this can be {Xrrr} too
    \hiderowcolors 
     Something & foo & bar & baz \\
    \showrowcolors 
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
\end{tabularx}


Luckily, such things are no longer a problem with the tabularray package:

\documentclass{scrartcl}

\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}

\noindent%
\begin{tblr}{
  colspec={XXXX},
  row{odd}={bg=lightgray},  
  row{1}={bg=black,fg=white},
}
  Something & foo & bar & baz \\
  Something & foo & bar & baz \\
  Something & foo & bar & baz \\
  Something & foo & bar & baz \\
  Something & foo & bar & baz \\
\end{tblr}  
  
\end{document}

Alternating row colors in tabular(*|x)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜