在较大的项目中,我们很难制作出看起来“恰到好处”的表格,因为使用 colorcell 会在彩色单元格区域之间留下很少的垂直线。我制作了一个最小的表格来演示,如以下屏幕截图所示:
我们读了很多关于这个问题的帖子,但都没有明确的答案。我想我对垂直线问题的理解越来越全面了。我发布了 LaTeX 示例代码,演示了几种变体。在某些 PDF 查看器中,灰色区域看起来是平滑的,而其他一些仍然显示出淡淡的白色重影。
最后,如果不将 tabcolsep 设置为 0,我就无法消除小白线,这似乎很不幸/英勇/不正确。
我们还遇到了一个问题,即水平线有时也会在使用 colortbl 的其他表格中显示,但我认为这实际上是一个单独的问题(如果我读得正确的话:\url{彩色表格中非常细的白线})。如果我确实得到了令人满意的前后对比,我将在这里开始另一个话题。
这是我使用这段代码末尾的建议得到的最佳结果:
这是 Ubuntu Linux 中 Evince 查看器拍摄的快照,在我看来,仍有一丝淡淡的白色。在不同的显示器上看起来有点不同。我在不同的操作系统的不同显示器上查看过这个,这似乎是一个棘手的 PDF 问题。
这是我目前能做的最好的事情了,如果有更好、更简单的方法,我希望你能告诉我。我并不完全讨厌将 tabcolsep 设置为 0,但我确实讨厌在表格左侧手动插入缩进空间。我不在乎右侧是否一直延伸到边缘。
请不要让您的答案是“重新设计您的桌子以符合我们对美观的看法”。这是教授们想要使用的答案。
在我的代码中,我保留了我们使用的所有包,因为较小的 MWE 不一定以相同的方式重现相同的问题。array 包的非常重要的作用是我仍然加载其他东西的证据。
\documentclass[letterpaper,landscape,11pt,english]{article}
\usepackage{lmodern}
\usepackage[scaled=0.9]{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[landscape]{geometry}
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\setlength{\parskip}{10bp}
\setlength{\parindent}{0pt}
\usepackage{array}
\usepackage{calc}
\usepackage{rotating}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{hhline}
\makeatletter
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage{moresize}
\usepackage{verbatim}
\usepackage{color}
\usepackage{colortbl}
\usepackage{adjustbox}
\usepackage{url}
\definecolor{gr}{RGB}{240,240,240}
\makeatother
\usepackage{babel}
\begin{document}
%\normalsize{}
Paul Johnson
March 26, 2016
We have fought a battle with coloring in LaTeX tables. The problem is
that when one colors a cell, one either obliterates the lines of the
border or one is unable to get the line colors correctly. The
ordinary line drawing functions like \\hline and \\cline are not
comfortable with colored cells, one is urged to change to hhline to
draw the lines. With hhlines, the colors of the cell backgrounds do not
destroy the border lines. But there's a new problem, the accidental
white line border.
The trouble case for this is illustrated in the first table, where you
should see tiny white vertical lines between the cells of the colored row.
% Am using guide in "colortbl.pdf" to use hhline to adjust color of
% column separator. However, it appears tabular* just ignores this
\setlength\tabcolsep{0pt}
\begin{tabular*}{11cm}{@{\extracolsep{\fill}}>{\centering}p{2.1cm}|> {\centering}m{2.9cm}|>{\centering}m{3cm}|>{\centering}m{2.9cm}|}
\multicolumn{4}{c}{\textbf{A Frustrating Table with white lines}}\tabularnewline
\hhline{~---}
& \multicolumn{1}{>{\centering}m{2.9cm}}{{\cellcolor{gr}}Here's a box\\
Score}& \multicolumn{1}{>{\centering}m{3cm}}{{\cellcolor{gr}}Another
Box with two\\
Rows} &{\cellcolor{gr}}Student's\\
Proficiency Level\tabularnewline
\hhline{----}
\multicolumn{1}{|>{\centering}p{2.1cm}|}{Science} & 28 & 26 or above & Proficient\tabularnewline
\hline
\end{tabular*}
Those are not actually white lines. They are more correctly described as the absence of gray lines.
Study this:
\url{https://tex.stackexchange.com/questions/65604/hhline-adds-white- line-where-no-line-is-intended}
The white line is the absence of a line caused by my use
of hhline, but I don't see how to change the color of that one little
line.
Here I try to set the row color as gray, but place the
first cell as white.
But the white lines show unless we tune up the rowcolor overlap,
which puts some gray outside the table on the right
\setlength\tabcolsep{0pt}
\begin{tabular*}{11cm}{@{\extracolsep{\fill}}
>{\centering}p{2.1cm}|
>{\centering}m{2.9cm}|
>{\centering}m{3cm}|
>{\centering}m{2.9cm}|}
\multicolumn{4}{c}{\textbf{A Frustrating Table with white
lines}}\tabularnewline
\hhline{~---}
\rowcolor{gr}[0mm][1mm]
\multicolumn{1}{>{\centering}m{2.1pt}|}{\cellcolor{white}}
& \multicolumn{1}{>{\centering}m{2.9cm}}{Here's a box\\
Score}& \multicolumn{1}{>{\centering}m{3cm}}{{\cellcolor{gr}}Another
Box with two\\
Rows} &{\cellcolor{gr}}Student's\\
Proficiency Level\tabularnewline
\hhline{----}
\multicolumn{1}{|>{\centering}p{2.1cm}|}{Science} & 28 & 26 or above & Proficient\tabularnewline
\hline
\end{tabular*}
The colortbl.pdf file has lots of suggestions, none I
understand very well. On p. 5 suggests it suggests:
Replace "|" with vline or vrule, like this:
\begin{verbatim}
!{\color{gr}\vrule width 5pt}
\end{verbatim}
or maybe
\begin{verbatim}
!{\color{gr}\vline}
\end{verbatim}
However, that causes the whole table to break because
bottom right corner lines don't connect anymore. Look here:
\setlength\tabcolsep{0pt}
\begin{tabular*}{11cm}{@{\extracolsep{\fill}}>{\centering}p{2.1cm}|
>{\centering}m{2.9cm}|
>{\centering}m{3cm}|
>{\centering}m{2.9cm}|}
\multicolumn{4}{c}{\textbf{A Frustrating Table with
white lines}}\tabularnewline
\hhline{~---}
& \multicolumn{1}{>{\centering}m{2.9cm}%
!{\color{gr}\vrule width 1pt}%% where a vert bar would have been
}{{\cellcolor{gr}}Here's a box\\
Score}&
\multicolumn{1}{>{\centering}m{3cm}!{\color{gr}\vrule width 5pt}}
{{\cellcolor{gr}}Another
Box with two\\
Rows} &{\cellcolor{gr}}Student's\\
Proficiency Level\tabularnewline
\hhline{----}
\multicolumn{1}{|>{\centering}p{2.1cm}|}{Science} & 28 & 26
or above & Proficient\tabularnewline
\hline
\end{tabular*}
I believe this problem relates to the use of the array package and
some assumptions it makes about the sum of columns inside a table. This
fine answer on SE explains it.
\url{https://tex.stackexchange.com/questions/116917/why-is-my-tabular-wider-than-the-sum-of-its-columns-widths}
The total width of a table is the sum of the columns, plus the
2 x tabcolsep, plus arrayrulewidth. But the tabular* is
specified with the total width, and that's confusing
So now, instead of specifying a
column width in cm, we have to do some ridiculous dance.
Replace a column width like "2.1cm" with
\begin{verbatim}
{\dimexpr 2.1cm-2\tabcolsep-1.5\arrayrulewidth}
\end{verbatim}
Same calculation has to be applied to multicols as well.
\setlength\tabcolsep{0pt}
\begin{tabular*}{11cm}{@{\extracolsep{\fill}}
>{\centering}p{\dimexpr 2.1cm-2\tabcolsep-1.5\arrayrulewidth}|
>{\centering}m{\dimexpr 2.9cm-2\tabcolsep-1.5\arrayrulewidth}|
>{\centering}m{\dimexpr 3cm-2\tabcolsep-1.5\arrayrulewidth}|
>{\centering}m{\dimexpr 2.8cm-2\tabcolsep-1.5\arrayrulewidth}|}
\multicolumn{4}{c}{\textbf{A Frustrating Table with
white lines}}\tabularnewline
\hhline{~---}
& \multicolumn{1}{
>{\centering}m{\dimexpr 2.9cm-2\tabcolsep-1.5\arrayrulewidth}%
!{\color{gr}\vrule width 1pt}%% where a vert bar would have been
}{{\cellcolor{gr}}Here's a box\\
Score}&
\multicolumn{1}{>{\centering}m
{\dimexpr 3cm-2\tabcolsep-1.5\arrayrulewidth}
!{\color{gr}\vrule width 5pt}}
{{\cellcolor{gr}}Another
Box with two\\
Rows} &{\cellcolor{gr}}Student's\\
Proficiency Level\tabularnewline
\hhline{----}
\multicolumn{1}{|>{\centering}p{2.1cm}|}{Science}
& 28
& 26 or above
& Proficient\tabularnewline
\hline
\end{tabular*}
\end{document}
答案1
用tabularx
代替tabular*
,并将overhang
的参数设置columncolor
为合适的值,细白色垂直线就会消失:
\documentclass[letterpaper,landscape,11pt,english]{article}
\usepackage[scaled=0.9]{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[landscape]{geometry}
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\usepackage{array}
\usepackage{hhline}
\usepackage{color}
\usepackage{colortbl}
\definecolor{gr}{RGB}{240,240,240}
\usepackage{tabularx}
\usepackage{babel}
\begin{document}
\renewcommand\arraystretch{1.25}
\begin{tabularx}{11cm}{
|>{\centering}X|
>{\centering}m{2.4cm}|
>{\centering}m{2.4cm}|
>{\centering}m{2.8cm}|}
\multicolumn{4}{c}{\textbf{A Frustrating Table with
white lines}}\tabularnewline[1ex]
\hhline{~|---}
\multicolumn{1}{c|}{} & \multicolumn{1}{>{\centering\columncolor{gr}}m{2.4cm}}%
{Here's a box\\ Score}
& \multicolumn{1}{>{\centering\columncolor{gr}[\dimexpr\tabcolsep + 1pt\relax]}m{2.4cm}}%
{Another Box\\ with two Rows} %
& \multicolumn{1}{>{\centering\columncolor{gr}[\tabcolsep]}m{2.8cm}|}%
{Student's\\ Proficiency Level}
\tabularnewline%
\hline
Science
& 28
& 26 or above
& Proficient\tabularnewline
\hline
\end{tabularx}
\end{document}
答案2
当两个相邻的单元格用两个不同的指令( PDF 流中调用的 PostScriptfill
运算符)着色时,某些 PDF 引擎(例如 SumatraPDF 中使用的 muPDF)中会出现细白线。fill
f
使用时,即使使用或colortbl
,每个单元格都会根据自己的指令进行着色。fill
\columncolor
\rowcolor
使用nicematrix
,一条指令或命令\rowcolor
键的使用将导致PDF 中只有一条指令。fill
\Block
fill
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{geometry}
\begin{document}
\begin{NiceTabular}[hvlines,corners = NW,first-row]{cp[c]{3cm}p[c]{3cm}p[c]{3cm}}
\Block{1-4}{\textbf{A not so frustrating table}} \\
& \Block[fill=gray!15]{1-3}{}
Here's a box Score & Another Box with two rows & Student's Proficiency Level \\
Science & 28 & 26 or above & Proficient
\end{NiceTabular}
\end{document}
此外,在 中{NiceTabular}
,您可以使用键hvlines
来绘制除空角、所谓的“第一行”和块之外的所有规则。块由命令创建\Block
。
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
答案3
处理自定义线条时有时会出现白线,也可以使用以下方法进行纠正tabularray
\documentclass{article}
\usepackage[landscape]{geometry}
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\usepackage{lmodern}
\usepackage[scaled=0.9]{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\definecolor{gr}{RGB}{240,240,240}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\noindent%
\begin{tblr}{
width = 11cm,
colspec = {Q[2.1cm,l,m] X[c,m] X[c,m] X[c,m]},
cell{2}{2-Z} = {bg=gr},
hline{2} = {2-Z}{}, hline{3-Z},
vline{1,3,4} = {3-Z}{},
vline{3,4} = {2-2}{gr},
vline{2,Z} = {2-Z}{},
}
\SetCell[c=4]{font=\bfseries,halign=center}
A Frustrating Table with white lines \\
& {Here's a box\\Score}
& {Another Box with two\\Rows}
& {Student's\\Proficiency Level} \\
Science & 28 & 26 or above & Proficient \\
\end{tblr}
\end{document}