
Estoy intentando crear una tabla con colores de fila alternos usando el xcolor
paquete con la [table]
opción. El siguiente ejemplo demuestra que el color de la fila (sin comentar \rowcolors..
) puede ocultar/cubrir el contenido de la tabla. En particular, parece haber un problema con las expresiones @.
\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}
- ¿Cómo podemos prevenir eso?
- Una segunda pregunta es si existe una manera fácil de cubrir toda la línea con color; se puede observar el relleno blanco en la línea gris.
Respuesta1
Para la primera pregunta, puede eliminar \tabcolsep
por separado esas dos columnas como
r<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
y por segundo, puedes definir un \bottomrulec
me gusta
\newcommand{\bottomrulec}{%
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
y usarlo en lugar de \bottomrule
. Aquí están las versiones en color por \toprule
si \midrule
las necesitas.
\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}
}
Tu código 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}
Respuesta2
El espacio en blanco proviene de booktabs
agregar algo de relleno vertical encima y debajo de las reglas de la tabla. Una solución alternativa consiste en establecer este relleno en $0$ pt y reemplazarlo con espaciado vertical con el cellspace
paquete: define unmínimo dicho espaciado vertical y es compatible con \rowcolors
(el especificador de columna debe estar precedido por la letra S
).
En cuanto al problema con @{}
, lo reemplazo agregando espacio horizontal negativo al salir de las celdas de la segunda columna y al ingresar a la tercera, con >{}
y <{}
.
\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}
Respuesta3
El entorno {NiceTabular}
de nicematrix
proporciona herramientas similares a las de colortbl
pero usando PGF/Tikz para el dibujo.
Usando ese entorno, tienes directamente lo que deseas (pero necesitas varias compilaciones ya que nicematrix
usa nodos 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}