这里是我的代码的一个最小工作示例。当我使用 overleaf 在线 TeX 编辑器编译它时,我得到了预期的结果,并且一切正常,而当我使用 texstudio 编译它时,我得到了以下结果
无论我使用哪种 PDF 查看器,我总是遇到边框与颜色重叠的问题。如果我一直放大到 300% 以上,我可以看到可能类似于边框的东西。我尝试了在 tex.stackexchange 上找到的所有方法,但我没能让它看起来像我想要的那样。知道这可能是查看器的错误并没有帮助,我需要一种方法来解决这个问题,比如获得更粗的边框(我试过但没有成功)或使用缩放到与内部单元格大小匹配的纯色图像(即没有边框)。任何建议都非常感谢,这让我很抓狂。
我愿意考虑任何其他方式来表示我想只考虑用蓝色填充的单元格和用黄色填充的单元格(总是被挑选,而蓝色的单元格可以是剩余单元格的任何随机子集。TIA
编辑:我将从链接中复制 MWE 代码,以防它失效,以供未来的访问者使用:
\documentclass[12pt]{book}
\usepackage{colortbl} % \cellcolor
\usepackage{hhline} % trying to keep borders visible using \cellcolor
\usepackage{xcolor}
\usepackage{mathtools}
\usepackage{multicol}
\usepackage{pifont}% "don't care"
\newcommand*{\eleminvalid}{\ensuremath{\text{\scriptsize{\ding{54}}}}} % "don't care"
\usepackage{caption}
\captionsetup[table]{labelformat=empty} %Caption table without Table text and number
\begin{document}
\begin{multicols}{2}\noindent
\begin{center}
\captionof{table}{Stato obiettivo}\noindent
\begin{tabular}{|c|c|c|c|}\hline
\cellcolor{yellow!25} & 1 & 2 & 3 \\ \hhline{|-|-|-|-|}
4 & 5 & 6 & 7 \\ \hhline{|-|-|-|-|}
8 & 9 & 10 & 11 \\ \hhline{|-|-|-|-|}
12 & 13 & 14 & 15 \\ \hhline{|-|-|-|-|}
\end{tabular}
\end{center}\columnbreak\noindent
\begin{center}
\captionof{table}{Generico sottoproblema}\noindent
\begin{tabular}{|c|c|c|c|}\hline
\cellcolor{yellow!25} & $\eleminvalid$ & $\eleminvalid$ & \cellcolor{blue!25}\textcolor{blue!25}{3}\\ \hhline{|-|-|-|-|}
$\eleminvalid$ & $\eleminvalid$ & $\eleminvalid$ & \cellcolor{blue!25}\textcolor{blue!25}{7} \\ \hhline{|-|-|-|-|}
$\eleminvalid$ & $\eleminvalid$ & $\eleminvalid$ & \cellcolor{blue!25}\textcolor{blue!25}{11} \\ \hhline{|-|-|-|-|}
\cellcolor{blue!25}\textcolor{blue!25}{12} & \cellcolor{blue!25}\textcolor{blue!25}{13} & \cellcolor{blue!25}\textcolor{blue!25}{14} & \cellcolor{blue!25}\textcolor{blue!25}{15} \\ \hhline{|-|-|-|-|}
\end{tabular}
\end{center}\noindent
\end{multicols}\noindent
\end{document}
答案1
您可以使用tabularray
没有这些问题的包。它还提供了 keyval 语法,可简化表的创建。
\documentclass[12pt]{book}
\usepackage{colortbl} % \cellcolor
\usepackage{hhline} % trying to keep borders visible using \cellcolor
\usepackage{xcolor}
\usepackage{mathtools}
\usepackage{multicol}
\usepackage{pifont}% "don't care"
\newcommand*{\eleminvalid}{\ensuremath{\text{\scriptsize{\ding{54}}}}} % "don't care"
\usepackage{caption}
\captionsetup[table]{labelformat=empty} %Caption table without Table text and number
\usepackage{tabularray}
\begin{document}
\begin{multicols}{2}\noindent
\begin{center}
\captionof{table}{Stato obiettivo}\noindent
\begin{tblr}{
colspec=cccc,
hlines, vlines, rowsep=0pt,
}
\SetCell{yellow!25} & 1 & 2 & 3 \\
4 & 5 & 6 & 7 \\
8 & 9 & 10 & 11 \\
12 & 13 & 14 & 15 \\
\end{tblr}
\end{center}\columnbreak\noindent
\begin{center}
\captionof{table}{Generico sottoproblema}\noindent
\begin{tblr}{
colspec=cccc,
hlines, vlines, rowsep=0pt,
column{4}={blue!25}, row{4}={blue!25},
}
\SetCell{yellow!25} & $\eleminvalid$ & $\eleminvalid$ & 3 \\
$\eleminvalid$ & $\eleminvalid$ & $\eleminvalid$ & 7 \\
$\eleminvalid$ & $\eleminvalid$ & $\eleminvalid$ & 11 \\
12 & 13 & 14 & 15 \\
\end{tblr}
\end{center}\noindent
\end{multicols}\noindent
\end{document}
答案2
使用,输出将在所有 PDF 查看器中完美(因为规则是在彩色面板后使用 Tikz 绘制的){NiceTabular}
。nicematrix
\documentclass[12pt]{book}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{mathtools}
\usepackage{multicol}
\usepackage{pifont}
\newcommand*{\eleminvalid}{\ensuremath{\text{\scriptsize{\ding{54}}}}}
\usepackage{caption}
\captionsetup[table]{labelformat=empty} %Caption table without Table text and number
\begin{document}
\begin{multicols}{2}\noindent
\begin{center}
\captionof{table}{Stato obiettivo}\noindent
\begin{NiceTabular}{cccc}[colortbl-like,hvlines]
\cellcolor{yellow!25} & 1 & 2 & 3 \\
4 & 5 & 6 & 7 \\
8 & 9 & 10 & 11 \\
12 & 13 & 14 & 15 \\
\end{NiceTabular}
\end{center}\columnbreak\noindent
\begin{center}
\captionof{table}{Generico sottoproblema}\noindent
\begin{NiceTabular}{cccc}[colortbl-like,hvlines]
\cellcolor{yellow!25} & $\eleminvalid$ & $\eleminvalid$ & \cellcolor{blue!25}\textcolor{blue!25}{3}\\
$\eleminvalid$ & $\eleminvalid$ & $\eleminvalid$ & \cellcolor{blue!25}\textcolor{blue!25}{7} \\
$\eleminvalid$ & $\eleminvalid$ & $\eleminvalid$ & \cellcolor{blue!25}\textcolor{blue!25}{11} \\
\cellcolor{blue!25}\textcolor{blue!25}{12} & \cellcolor{blue!25}\textcolor{blue!25}{13} & \cellcolor{blue!25}\textcolor{blue!25}{14} & \cellcolor{blue!25}\textcolor{blue!25}{15} \\
\end{NiceTabular}
\end{center}\noindent
\end{multicols}\noindent
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
答案3
作为@marv 答案的补充。代码更简洁一些...
\documentclass[12pt]{book}
\usepackage{pifont}% "don't care"
\usepackage{caption}
\usepackage{xcolor}
\usepackage{tabularray}
\begin{document}
\begin{table}
\centering
\begin{minipage}{0.4\linewidth}\centering
\caption*{Stato obiettivo}
\begin{tblr}{hlines, vlines,
colspec = {*{4}{Q[c,2ex]}},
abovesep= 4pt
}
\SetCell{yellow!25}
& 1 & 2 & 3 \\
4 & 5 & 6 & 7 \\
8 & 9 & 10 & 11 \\
12 & 13 & 14 & 15 \\
\end{tblr}%
\end{minipage}\begin{minipage}{0.4\linewidth}\centering
\caption*{Generico sottoproblema}
\begin{tblr}{hlines, vlines,
colspec = {*{4}{Q[c,2ex]}},
column{4} = {blue!25},
row{4} = {blue!25},
abovesep = 4pt
}
\SetCell{yellow!25}
& \ding{54} & \ding{54} & \\
\ding{54} & \ding{54} & \ding{54} & \\
\ding{54} & \ding{54} & \ding{54} & \\
& & & \\
\end{tblr}
\end{minipage}
\end{table}
\end{document}