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

发布于 2024-10-31 17:00:52 字数 1700 浏览 1 评论 0原文

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

带有备用行的示例表格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

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

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

Example table with alternate row coloring

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:

Example table with tabular*

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 & 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.

Example table with tabularx

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-11-07 17:00:52

不是修复,而是一种破解,将 \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}

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}
浅语花开 2024-11-07 17:00:52

幸运的是,这些事情不再是 tabularray 包的问题:

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

在此处输入图像描述

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}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文