4 つの列が 2 つの大きな列にグループ化されたテーブルがあります。パッケージを使用しています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}