我想创建一个表格,其中左上角有一个空的单元格,并且在列和行标题上有一个背景颜色(左上角的空除外)。
我尝试将这两个答案结合起来:
仅为表格的一个单元格着色
删除左上角表格单元格的边框
但使用 时,背景颜色似乎绘制在上方规则之上\cline
。使用 时不会发生这种情况\hline
。
\cline
如何在背景颜色上绘图?
$ lualatex --version
This is LuaTeX, Version beta-0.80.0 (TeX Live 2015/Debian) (rev 5238)
麦格维:
\documentclass[24pt a6paper, landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\begin{document}
\newcommand{\sometext}{this is just to demonstrate line breaking}
\definecolor{headercolor}{RGB}{209,220,204}
\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}
This table has a line at the top of the gray cell (1-3) because of \verb|\hrule| :
\begin{tabular}{c|c|c|c|}\hline
\multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\cellDefinition{\sometext} & \cellDefinition{\sometext} & \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\end{tabular}
\vspace{1em}
How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?
\begin{tabular}{c|c|c|c|}\cline{2-4}\cline{2-4}
\multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{how do I bring the line at top before the background? } & \cellDefinition{\sometext} \\\hline
\cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{I want such top line at the other cell too.} & \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\end{tabular}
\end{document}
结果
答案1
您必须将\usepackage{hhline}
和 替换\cline{2-4}
为\hhline{~|-|-|-|}
。 的参数\hhline
是绘制内容的规范:~
对应于空列、-
对应于跨越一列的线,并|
用垂直线表示连接处。
\documentclass[24pt a6paper, landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\usepackage{hhline}
\begin{document}
\newcommand{\sometext}{this is just to demonstrate line breaking}
\definecolor{headercolor}{RGB}{209,220,204}
\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}
This table has a line at the top of the gray cell (1-3) because of \verb|\hrule| :
\begin{tabular}{c|c|c|c|}\hline
\multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\cellDefinition{\sometext} & \cellDefinition{\sometext} & \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\end{tabular}
\vspace{1em}
How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?
\begin{tabular}{c|c|c|c|}
\hhline{~|-|-|-|}
\multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{how do I bring the line at top before the background? } & \cellDefinition{\sometext} \\\hline
\cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{I want such top line at the other cell too.} & \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\end{tabular}
\end{document}
答案2
由于历史原因,该命令\cline
绘制的规则在下方单元格中突出(而\hline
不是突出)。在{NiceTabular}
中nicematrix
,该行为已得到纠正。
因此,通过使用{NiceTabular}
而不是{tabular}
,您可以直接获得预期的输出。
\documentclass[24pt a6paper, landscape]{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\usepackage{nicematrix}
\begin{document}
\newcommand{\sometext}{this is just to demonstrate line breaking}
\definecolor{headercolor}{RGB}{209,220,204}
\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}
How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?
\begin{NiceTabular}{c|c|c|c|}
\cline{2-4}
\multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{how do I bring the line at top before the background? } & \cellDefinition{\sometext} \\\hline
\cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{I want such top line at the other cell too.} & \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\end{NiceTabular}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
然而,尽管从数学上来说很完美,但使用工具生成的 PDFcolortbl
有时很难由 PDF 查看器呈现:在某些缩放级别,某些规则似乎消失了(即使在 Adobe Reader 中)。
这就是为什么nicematrix
提供工具来解决这些问题。使用密钥colortbl-like
(无需加载colortbl
),PDF 将在所有缩放级别的所有 PDF 查看器中完美显示。
\documentclass[24pt a6paper, landscape]{article}
\usepackage{xcolor}
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\usepackage{nicematrix}
\begin{document}
\newcommand{\sometext}{this is just to demonstrate line breaking}
\definecolor{headercolor}{RGB}{209,220,204}
\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}
How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?
\begin{NiceTabular}{c|c|c|c|}[colortbl-like]
\cline{2-4}
\multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{how do I bring the line at top before the background? } & \cellDefinition{\sometext} \\\hline
\cellDefinition{\sometext} & \cellcolor{headercolor} \cellDefinition{I want such top line at the other cell too.} & \cellDefinition{\sometext} & \cellDefinition{\sometext} \\ \hline
\end{NiceTabular}
\end{document}