Цвет чередующихся строк в табличном (* | x)? - PullRequest
5 голосов
/ 11 апреля 2011

Я пытаюсь создать таблицу в своем документе, которая более или менее напоминает таблицу на картинке ниже:

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 Ответ

6 голосов
/ 12 апреля 2011

Не исправить, а взломать, добавить \ 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}
...