我有一個包含四列的表,這些列分成兩個較大的列。我正在使用該包tabularx
。當我輸入\cline{2-5}
在這些列上方放置一條線時,當表格沒有顏色時它是可見的,但單元格中的顏色一旦存在就會隱藏它。我已經查看了有關該問題的許多帖子,但發現大多數帖子都在表格中更深入地關注該問題,而不是在表格的開頭,並且無法通過示例來解決我的問題。我感謝任何反饋和專業提示。下面是我的程式碼的一個工作範例。
\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage{tabularx,colortbl}
\usepackage{color, colortbl}
\usepackage[table,xcdraw]{xcolor}
\definecolor{Gray}{gray}{0.9}
\definecolor{yellow}{rgb}{1,0.98,0.46}
\usepackage{multirow, hhline}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{document}
\begin{tabularx}{\textwidth}{ |l| *{4}{Y|} }
\cline{2-5}
\multicolumn{1}{c|}{}
\rowcolor{Gray}
& \multicolumn{2}{c|}{\textbf{Number of Dogs}}
& \multicolumn{2}{c|}{\textbf{Dogs Per Household}}\\
\hline
\rowcolor{Gray}
\textbf{Group and Home} & \textbf{2022} & \textbf{2023} & \textbf{2022} & \textbf{2023} \\
\hline
{\cellcolor{yellow}1} & 2 & 2 & 1.24 & 1.42 \\ \hline
{\cellcolor{yellow}2} & 5 & 45 & 1.15 & 1.13 \\ \hline
{\cellcolor{yellow}3} & 3 & 3 & 1.03 & 1.01 \\ \hline
\end{tabularx}
\end{document}
答案1
與.{NiceTabular}
nicematrix
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}
\renewcommand{\arraystretch}{1.3}
\begin{NiceTabular}{>{\columncolor{yellow!50}}l*{4}{X[c]}}[hvlines,corners]
\RowStyle[nb-rows=2,rowcolor=gray!15]{\bfseries}
& \Block{1-2}{Number of Dogs}
&& \Block{1-2}{Dogs Per Household} \\
Group and Home & 2022 & 2023 & 2022 & 2023 \\
1 & 2 & 2 & 1.24 & 1.42 \\
2 & 5 & 45 & 1.15 & 1.13 \\
3 & 3 & 3 & 1.03 & 1.01 \\
\end{NiceTabular}
\end{document}
你需要幾個編譯。
答案2
詳細說明@DavidCarlisle 的建議:請考慮刪除這個小表中的所有垂直和水平規則。如果您認為沿右側邊緣提供垂直標尺和沿底部邊緣提供水平標尺絕對必要,則可以使用指令\arrayrulecolor
來渲染這些規則Gray
,從而從視覺上講,這些規則不那麼突出。
另外,由於您使用顏色作為突出的突出顯示設備,因此您可以刪除\textbf
標題中的指令。當然,請避免混合和不匹配的視覺隱喻。
\documentclass{article}
\usepackage{tabularx,ragged2e}
\newcolumntype{C}{>{\Centering}X}
\setlength\extrarowheight{2pt} % for a less-cramped "look"
\usepackage[table,xcdraw]{xcolor}
\definecolor{Gray}{gray}{0.9}
\definecolor{yellow}{rgb}{1,0.98,0.46}
\begin{document}
\arrayrulecolor{Gray}
\noindent % <-- don't forget this directive
\begin{tabularx}{\textwidth}{ l CCCC |}
\rowcolor{Gray}
Group and Home &
\multicolumn{2}{c}{Number of Dogs} &
\multicolumn{2}{c}{Dogs Per Household}\\
\rowcolor{Gray}
& 2022 & 2023 & 2022 & 2023 \\
\cellcolor{yellow} 1 & 2 & 2 & 1.24 & 1.42 \\
\cellcolor{yellow} 2 & 5 & 45 & 1.15 & 1.13 \\
\cellcolor{yellow} 3 & 3 & 3 & 1.03 & 1.01 \\
\cline{2-5}
\end{tabularx}
\end{document}
答案3
按照評論中的建議,使用\hhline
.
\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{hhline}
\definecolor{Gray}{gray}{0.9}
\definecolor{yellow}{rgb}{1,0.98,0.46}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}[htp]
\begin{tabularx}{\textwidth}{ |l| *{4}{Y|} }
\hhline{~|-|-|-|-|}
\rowcolor{Gray}
\multicolumn{1}{c|}{\cellcolor{white}}
& \multicolumn{2}{c|}{\textbf{Number of Dogs}}
& \multicolumn{2}{c|}{\textbf{Dogs Per Household}}\\
\hline
\rowcolor{Gray}
\textbf{Group and Home} & \textbf{2022} & \textbf{2023} & \textbf{2022} & \textbf{2023} \\
\hline
\cellcolor{yellow}1 & 2 & 2 & 1.24 & 1.42 \\ \hline
\cellcolor{yellow}2 & 5 & 45 & 1.15 & 1.13 \\ \hline
\cellcolor{yellow}3 & 3 & 3 & 1.03 & 1.01 \\ \hline
\end{tabularx}
\caption{A colorful table}
\end{table}
\begin{table}[htp]
\centering
\setlength{\tabcolsep}{0pt}
\begin{tabular*}{0.75\textwidth}{@{\extracolsep{\fill}} l c c c c}
\toprule
Groups and Home &
\multicolumn{4}{c}{Number of Dogs} \\
\cmidrule{2-5}
& \multicolumn{2}{c}{Absolute} & \multicolumn{2}{c}{\makebox[0pt]{per Household}} \\
\cmidrule{2-3} \cmidrule{4-5}
& 2022 & 2023 & 2022 & 2023 \\
\midrule
1 & 2 & 2 & 1.24 & 1.42 \\
2 & 5 & 45 & 1.15 & 1.13 \\
3 & 3 & 3 & 1.03 & 1.01 \\
\bottomrule
\end{tabular*}
\caption{A less fanciful table}
\end{table}
\end{document}
注意\rowcolor
應該去前 \multicolumn
。
正如你所看到的,我還提出了一個不那麼奇特的實現。唯一的技巧是零寬度框,以避免最後一列出現多餘的寬度。
最後的評論:程式碼
\usepackage{tabularx,booktabs}
\usepackage{tabularx,colortbl}
\usepackage{color, colortbl}
\usepackage[table,xcdraw]{xcolor}
完全一樣
\usepackage{tabularx,booktabs}
\usepackage[table,xcdraw]{xcolor}