表格中的交替行颜色(* | x)?

时间:2011-04-11 12:37:19

标签: latex latex-environment

我正在尝试在我的文档中创建一个类似于下图中表格的表格:

Example table with alternate row coloring

该表应该水平拉伸到\textwidth。我tabular*的第一次尝试看起来像这样:

\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}

结果是:

Example table with tabular*

好吧,备用行着色有效但tabular*在列之间插入空格以将整个表格拉伸到\textwidth。通过我的LaTeX伴侣浏览,我发现tabularx应该能够做我想要的。所以我改变了我的代码:

\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 & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
    Something & foo & bar & baz \\
  \end{tabularx}
\end{document}

现在,这看起来更像是它。但tabularx忽略着色的起始行,并从第一行开始。

Example table with tabularx

现在我的想法已经用完了。有什么建议吗?

1 个答案:

答案 0 :(得分:6)

不是修复,而是黑客攻击,将\ hiderowcolors添加到第一行,然后使用\ showrowcolors重新打开颜色。见代码:

\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}