表格中的单元格着色重现:在大多数视图/缩放中垂直和水平条被隐藏

表格中的单元格着色重现:在大多数视图/缩放中垂直和水平条被隐藏

使用 \cellcolor 或 \rowcolor 为表格中的单元格(或单元格行)添加阴影时,查看 PDF 时垂直和水平分隔符会随机变得模糊或隐藏。Chrome、Acrobat 和 TexStudio 的内置 PDF 查看器都存在这种情况。

我必须遵守样式指南,并且可以使用 Word (!) 或 Tex,但产品必须是 PDF。样式指南以 Word 格式提供,其中有一个表格,表格顶部一行单元格带阴影。我试图用 LaTeX 重新创建它,但 PDF 渲染效果始终不佳。阴影单元格周围的垂直和顶部水平条会根据缩放级别出现和消失。例如,查看下面的图像,来自三个不同的缩放级别:

缩放级别 1:

缩放级别 1

缩放级别 2:

缩放级别 2

缩放级别 3:

缩放级别 3

在网上搜索并查看了其他几篇关于此问题的帖子后,我采用并汇编了其中的许多内容到这个 MWE 中。六个选项中没有一个在所有缩放级别上都能正常工作或一致地呈现。

我曾尝试过:tabular、tabularx,通过单列“多列”、hhline 等使用 hack。请参阅下面的 MWE 代码。

如果有人知道如何为表格中的单元格着色而不会出现这些渲染问题,请帮忙!

梅威瑟:

\documentclass[twocolumn]{article}
\usepackage{lipsum,tabularx,booktabs,tabularx,hhline}
\usepackage[table]{xcolor}
%opening

\begin{document}

Acrobat, Chrome, and TexStudio's native PDF viewer have issues showing the 
vertical bars between and above cells which specify either cell color or row 
color.  Example tables follow:

\begin{table}
    \centering\setlength{\aboverulesep}{0pt}\setlength{\belowrulesep}{0pt}
    \setlength{\extrarowheight}{2pt}
    \caption{As can be seen, the vertical bars between X, Y, and Z are 
    invisible depending on the level of zoom.  The horizontal bar above is also 
    invisible, depending on zoom.}
    \begin{tabular}{|l|l|l|}
        \toprule
        \rowcolor[gray]{0.85}
        X & Y & Z \\
        \midrule
        0 & 1 & 2\\\hline
        0.1 & 0.2 & 0.3\\
        \bottomrule
    \end{tabular}
\end{table}

\begin{table}
    \centering
    \caption{TabularX and the X column type do not help, though this is what I 
    need to use.  Both toprule and hline exhibit this behavior, regarding the 
    top line.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hline
        \rowcolor[gray]{0.85}
        X & Y & Z \\\hline
        0 & 1 & 2\\\hline
        0.1 & 0.2 & 0.3\\\hline
    \end{tabularx}
\end{table}

\begin{table}
    \centering
    \caption{Multicolumn doesn't help either.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hline
        \rowcolor[gray]{0.85}
        \multicolumn{1}{|X|}{X} & Y & Z \\\hline
        0 & 1 & 2\\\hline
        0.1 & 0.2 & 0.3\\\hline
    \end{tabularx}
\end{table}

\begin{table}
    \centering
    \caption{hhline is also ineffective at treating this issue.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hhline{---}
        \rowcolor[gray]{0.85}
        \multicolumn{1}{|X|}{X} & Y & Z \\\hhline{---}
        0 & 1 & 2\\\hhline{---}
        0.1 & 0.2 & 0.3\\\hhline{---}
    \end{tabularx}
\end{table}

\begin{table}
    \centering
    \caption{hhline does not help in regular tabular mode either.}
    \begin{tabular}{|l|l|l|}
        \hhline{---}
        \rowcolor[gray]{0.85}
        X & Y & Z \\\hhline{---}
        0 & 1 & 2\\\hhline{---}
        0.1 & 0.2 & 0.3\\\hhline{---}
    \end{tabular}
\end{table}

\begin{table}[tbph]
    \centering
    \caption{Just to be clear, rowcolor and cellcolor have the same issue, and 
    specifying vertical bars with hhline does not help. Another post suggested 
    putting cellcolor in brackets, which seemed pointless, but I tried.  It was 
    indeed pointless.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hhline{|-|-|-|}
        {\cellcolor[gray]{.87}}X & {\cellcolor[gray]{.87}}Y & 
        {\cellcolor[gray]{.87}}Z\tabularnewline\hhline{|-|-|-|}
        0 & 1 & 2\\\hhline{---}
        0.1 & 0.2 & 0.3\\\hhline{---}
    \end{tabularx}

\end{table}

\end{document}

答案1

感谢 Leandriis 为我指明了 calstable 的正确方向。我从其中一个链接中获取了代码并对其进行了修改,使其适用于此特定示例。人们可以轻松地对其进行扩展,因为这是一个漂亮、干净、极简的示例表。

最终结果与我预期的完全一致:一个带有阴影单元格的简单表格,以及在任何 PDF 查看器中正确显示的规则。

calstable 工作示例

\begin{calstable}[c]
    % Defining columns relative to each other and relative to the margins
    \colwidths{{\dimexpr(\columnwidth)/3\relax}
        {\dimexpr(\columnwidth)/3\relax}
        {\dimexpr(\columnwidth)/3\relax}
    }

    % Set up the tabular
    \makeatletter
    \def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
    \def\cals@framecs@width{0.4pt}
    \def\cals@bodyrs@width{0.4pt}
    \cals@setpadding{Ag}
    \cals@setcellprevdepth{Al}
    \def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
    \def\cals@rs@width{0.4pt}
    \def\cals@bgcolor{}

    \def\gray{\ifx\cals@bgcolor\empty     % "Switch" to turn on and off colour
        \def\cals@bgcolor{gray!20}
        \else \def\cals@bgcolor{} \fi}

    % Header row, can add alignL/C/R to each cell if necessary
    \thead{%\bfseries
        \brow
        \gray\alignL\cell{\vfil X}
        \cell{\vfil Y}
        \cell{\vfil Z}\gray
        \erow
        \mdseries
    }
    \tfoot{\lastrule\strut}

    % Row 1 (below header)
    \brow
    \alignL\cell{\vfil 0}
    \cell{\vfil 1}
    \cell{\vfil 2}
    \erow

    % Row 2
    \brow
    \alignL\cell{\vfil 0.1}
    \cell{\vfil 0.2}
    \cell{\vfil 0.3}
    \erow

    \makeatletter
\end{calstable}
\end{table}

答案2

有了nicematrix,你就有了一个解决方案。颜色面板在规则之前绘制(但你需要多次编译)。

\documentclass[twocolumn]{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{table}
\centering
\caption{With NiceTabularX}
\begin{NiceTabularX}{\columnwidth}{XXX}[colortbl-like,hvlines]
    \rowcolor[gray]{0.85}
    X   & Y   & Z \\
    0   & 1   & 2\\
    0.1 & 0.2 & 0.3\\
\end{NiceTabularX}
\end{table}

\end{document}

上述代码的输出

相关内容