2011-04-11 144 views
5

我想在我的文檔,在下面的圖片類似或多或少表中創建一個表:表格中的交替行顏色(* | x)?

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

現在我已經江郎才盡。有什麼建議麼?

回答

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} 
+0

我想這是我們目前能做的最好的,而不用爲tabularx寫一個替代品。 – 2011-04-18 05:44:56