
Estou tentando criar uma tabela com cores de linhas alternadas usando o xcolor
pacote com a [table]
opção. O exemplo a seguir demonstra que a coloração da linha (removendo o comentário \rowcolors..
) pode ocultar/cobrir o conteúdo da tabela. Em particular, parece haver um problema com as expressões @.
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\begin{document}
\begin{table}
\footnotesize
\centering
%\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
- Como podemos evitar isso?
- Uma segunda questão é se existe uma maneira fácil de cobrir toda a linha com cores; pode-se observar o preenchimento branco na linha cinza.
Responder1
Para a primeira pergunta, você pode remover \tabcolsep
separadamente essas duas colunas, como
r<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
e em segundo lugar, você pode definir um \bottomrulec
like
\newcommand{\bottomrulec}{%
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
e use-o em vez de \bottomrule
. Aqui estão as versões coloridas para \toprule
e \midrule
caso você precise delas.
\newcommand{\toprulec}{%
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\aboverulesep}{0pt}
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}
}
\newcommand{\midrulec}{%
\arrayrulecolor{gray!15}\specialrule{\aboverulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{\belowrulesep}
}
Seu código foi modificado:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\newcommand{\bottomrulec}{% Coloured \toprule
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
\begin{document}
\begin{table}
\footnotesize
\centering
\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrulec
\end{tabular}
\end{table}
\end{document}
Responder2
O espaço em branco vem da booktabs
adição de algum preenchimento vertical acima e abaixo das regras da tabela. Uma solução alternativa consiste em definir esse preenchimento para $0$ pt e substituí-lo por espaçamento vertical com o cellspace
pacote: ele define ummínimo tal espaçamento vertical e é compatível com \rowcolors
(o especificador de coluna deve ser precedido pela letra S
).
Quanto ao problema com @{}
, substituo-o pela adição de espaçamento horizontal negativo ao sair das células da segunda coluna e ao entrar na terceira, com >{}
e <{}
.
\documentclass[preview]{article}
\usepackage{mathtools}
\usepackage{tabularx, booktabs, caption, array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}
\begin{document}
\begin{table}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\rowcolors{2}{gray!15}{white}% <{\hskip-\arraycolsep}>{\hskip-\arraycolsep{}}
$ \begin{array}{Slr <{{}\hskip-\arraycolsep}>{\hskip-\arraycolsep\mkern-.5mu}lcc}
&& & \text{Col 1} & \text{Col 2} \\
\toprule
\text{Row 1} & (100 \times{} & 100) & 1 & 2 \\
\text{Row 2} & (100 \times{} &1000) & 3 & 4 \\
\bottomrule
\end{array} $
\end{table}
\end{document}
Responder3
O ambiente {NiceTabular}
de nicematrix
fornece ferramentas semelhantes às de colortbl
mas usando PGF/Tikz para o desenho.
Usando esse ambiente, você tem diretamente o que deseja (mas precisa de várias compilações, pois nicematrix
usa nós PGF/Tikz).
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}
\begin{document}
\begin{table}
\footnotesize
\centering
\begin{NiceTabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
\CodeBefore
\rowcolors{2}{gray!15}{}
\Body
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrule
\end{NiceTabular}
\end{table}
\end{document}