Tenho uma tabela com quatro colunas que se agrupam em duas colunas maiores. Estou usando o pacote tabularx
. Quando eu insiro \cline{2-5}
para colocar uma linha acima dessas colunas, ela fica visível quando a tabela está sem cor, mas a cor na célula a oculta uma vez presente. Eu vi muitas postagens sobre o assunto, mas descobri que a maioria das postagens trata desse problema mais profundamente em uma tabela e não no início de uma tabela e não consegui manipular exemplos para corrigir meu problema. Agradeço qualquer feedback e dicas profissionais. Abaixo está um exemplo funcional do meu código.
\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}
Aqui está uma foto. Você verá que a linha superior acima das colunas 2 a 5 está ausente:
Responder1
Com {NiceTabular}
de 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}
Você precisa de várias compilações.
Responder2
Para elaborar a sugestão de @DavidCarlisle: considere se livrar de todas as regras verticais e horizontais nesta pequena mesa. Se você acha que é absolutamente essencial fornecer uma regra vertical ao longo da borda direita e uma regra horizontal ao longo da borda inferior, você pode usar uma \arrayrulecolor
diretiva para tornar essas regras Gray
e, portanto, muito menos proeminentes, visualmente falando.
Além disso, como você está usando cores como um dispositivo de destaque de destaque, você pode se livrar das \textbf
diretivas no cabeçalho. Com certeza, evite misturar e incompatibilidade de metáforas visuais.
\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}
Responder3
Conforme sugerido nos comentários, use \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}
Observe que \rowcolor
deve irantes \multicolumn
.
Proponho também, como você vê, uma realização menos fantasiosa. O único truque é a caixa de largura zero para evitar que o excesso de largura vá para a última coluna.
Um comentário final: o código
\usepackage{tabularx,booktabs}
\usepackage{tabularx,colortbl}
\usepackage{color, colortbl}
\usepackage[table,xcdraw]{xcolor}
é exatamente igual a
\usepackage{tabularx,booktabs}
\usepackage[table,xcdraw]{xcolor}