Mesas de colores con booktabs, makecell y cellspace

Mesas de colores con booktabs, makecell y cellspace

Mientras experimentaba con diferentes paquetes de tablas, me di cuenta de que cada paquete básicamente se sostiene por sí mismo y, a menudo, carece de compatibilidad con otros paquetes excelentes.

Así que intenté crear una tabla al estilo del booktabs-paquete (solo líneas horizontales), pero con la funcionalidad del makecell-paquete ( \thead, \makegapedcells, ...) y la posibilidad de colorear las filas (lo cual no es muy intuitivo con booktabs, como se discutioaquí.)

Hice una pequeña comparación entre diferentes configuraciones de tablas:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[skip=10pt]{parskip}
\usepackage[table]{xcolor}
\usepackage{rotating}
\usepackage{array}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage[column=Q]{cellspace}    % changed to Q for simultaneous use of siunitx

\newcommand{\tableComment}[2]{
    \begin{tabular}{m{4.5cm}}
        #1\\[5pt]#2
    \end{tabular}}

\renewcommand{\theadfont}{\itshape}
\renewcommand{\theadgape}{}
\renewcommand{\theadalign}{cc}
\setcellgapes{1pt}
\renewcommand{\cellrotangle}{90}

\newcommand{\TopRule}{\Xhline{1pt}}
\newcommand{\MidRule}{\Xhline{.5pt}}
\newcommand{\BottomRule}{\Xhline{1pt}}


\begin{document}

\tableComment{1: Standard Lines, Head with \texttt{makecell}, no additional spacing}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{ll}\hline
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \hline
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \hline
    \end{tabular}
}
\tableComment{2: Standard Lines, Head with \texttt{makecell}, spacing with \texttt{cellspace}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\hline
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \hline
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \hline
    \end{tabular}
}
\tableComment{3: Lines with \texttt{Xhline}, Head with \texttt{makecell}, spacing with \texttt{cellspace}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\TopRule
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}\\

\tableComment{4: Lines with \texttt{Xhline}, Head with \texttt{thead}, spacing with \texttt{makegapedcells}}
{
    \rowcolors{1}{gray!20}{white}
    \makegapedcells
    \begin{tabular}{ll}\TopRule
        \rowcolor{gray!40}
        \thead{My Head} & \thead{My second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}
\tableComment{5: Lines with \texttt{booktabs}, Head with \texttt{makecell}, spacing with \texttt{booktabs}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{ll}\toprule
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \midrule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \bottomrule
    \end{tabular}
}
\tableComment{6: Lines with \texttt{Xhline}, Head with \texttt{thead}, spacing with \texttt{cellspace}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\TopRule
        \rowcolor{gray!40}
        \thead{My Head} & \thead{My second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}

\settowidth\rotheadsize{\theadfont second}
\tableComment{7: Lines with \texttt{Xhline}, Head with \texttt{rothead}, spacing with \texttt{callspace} }
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\TopRule
        \rowcolor{gray!40}
        \rothead{My\\Head} & \rothead{My\\second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}
\tableComment{8: Lines with \texttt{booktabs}, Head with \texttt{rothead}, spacing with \texttt{booktabs}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{ll}\toprule
        \rowcolor{gray!40}
        \rothead{My\\Head} & \rothead{My\\second\\ Head} \\\midrule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \bottomrule
    \end{tabular}
}
\tableComment{9: Lines with \texttt{Xhline}, Head with \texttt{rothead}, spacing with \texttt{makegapedcells}}
{
    \rowcolors{1}{gray!20}{white}
    \makegapedcells
    \begin{tabular}{ll}\TopRule
        \rowcolor{gray!40}
        \rothead{My\\Head} & \rothead{My\\second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}
\end{document}

Con el siguiente resultado: Diferentes configuraciones de mesa

Como puede ver, en casi todas las combinaciones de paquetes hay algunas dificultades (o simplemente no se ve bien). Mis favoritas son las versiones 3 y 6.

Pero no encontré soluciones para celdas rotadas, o usando\makegapedcells

¿Cómo crearías tablas de colores al estilo booktabs? (¿Y qué hace booktabsrealmente el paquete excepto introducir nuevas líneas horizontales y cambiar el espacio vertical?). ¿Y por qué se \makegapedcellseliminan todos los colores excepto el interior \thead?

Respuesta1

En cuanto al espaciado vertical en las celdas, el ganador es... (redoble de tambores)... cellspace, siempre que lo use de la manera correcta; simplemente olvidó especificar los valores del espaciado mínimo en la parte superior e inferior de las celdas.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[skip=10pt]{parskip}
\usepackage[table]{xcolor}
\usepackage{rotating}
\usepackage{array}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage[column=Q]{cellspace} % changed to Q for simultaneous use of siunitx
\setlength{\cellspacetoplimit}{5pt}
\setlength{\cellspacebottomlimit}{5pt}

\newcommand{\tableComment}[2]{
    \begin{tabular}{m{4.5cm}}
        #1\\[5pt]#2
    \end{tabular}}

\renewcommand{\theadfont}{\itshape}
\renewcommand{\theadgape}{}
\renewcommand{\theadalign}{cc}
\setcellgapes{1pt}
\renewcommand{\cellrotangle}{90}

\newcommand{\TopRule}{\Xhline{1pt}}
\newcommand{\MidRule}{\Xhline{.5pt}}
\newcommand{\BottomRule}{\Xhline{1pt}}


\begin{document}

\tableComment{1: Standard Lines, Head with \texttt{makecell}, no additional spacing}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{ll}\hline
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \hline
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \hline
    \end{tabular}
}
\tableComment{2: Standard Lines, Head with \texttt{makecell}, spacing with \texttt{\color{red}cellspace}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\hline
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \hline
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \hline
    \end{tabular}
}
\tableComment{3: Lines with \texttt{Xhline}, Head with \texttt{makecell}, spacing with \texttt{\color{red}cellspace}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\TopRule
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}\\

\tableComment{4: Lines with \texttt{Xhline}, Head with \texttt{thead}, spacing with \texttt{makegapedcells}}
{
    \rowcolors{1}{gray!20}{white}
    \makegapedcells
    \begin{tabular}{ll}\TopRule
        \rowcolor{gray!40}
        \thead{My Head} & \thead{My second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}
\tableComment{5: Lines with \texttt{booktabs}, Head with \texttt{makecell}, spacing with \texttt{booktabs}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{ll}\toprule
        \rowcolor{gray!40}
        My Head & \makecell{My second\\ Head} \\ \midrule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \bottomrule
    \end{tabular}
}
\tableComment{6: Lines with \texttt{Xhline}, Head with \texttt{thead}, spacing with \texttt{\color{red}cellspace}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\TopRule
        \rowcolor{gray!40}
        \thead{My Head} & \thead{My second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}

\settowidth\rotheadsize{\theadfont second}
\tableComment{7: Lines with \texttt{Xhline}, Head with \texttt{rothead}, spacing with \texttt{\color{red}cellspace} }
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{QlQl}\TopRule
        \rowcolor{gray!40}
        \rothead{My\\Head} & \rothead{My\\second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}
\tableComment{8: Lines with \texttt{booktabs}, Head with \texttt{rothead}, spacing with \texttt{booktabs}}
{
    \rowcolors{1}{gray!20}{white}
    \begin{tabular}{ll}\toprule
        \rowcolor{gray!40}
        \rothead{My\\Head} & \rothead{My\\second\\ Head} \\\midrule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \bottomrule
    \end{tabular}
}
\tableComment{9: Lines with \texttt{Xhline}, Head with \texttt{rothead}, spacing with \texttt{makegapedcells}}
{
    \rowcolors{1}{gray!20}{white}
    \makegapedcells
    \begin{tabular}{ll}\TopRule
        \rowcolor{gray!40}
        \rothead{My\\Head} & \rothead{My\\second\\ Head} \\ \MidRule
        some content & $\dfrac{1}{2}$ \\
        foo & bar \\ \BottomRule
    \end{tabular}
}

\end{document} 

ingrese la descripción de la imagen aquí

Respuesta2

Lamentablemente, cellspacees posible que la altura de las filas sea desigual, según el contenido de la celda. Con todos los paquetes, experimentará el problema de que las reglas desaparecen en el navegador porque el color las sobrescribe.

El paquetecaloríasDe hecho, es el único paquete que he encontrado que soluciona la mayoría de los problemas.

Otra posibilidad es cancelar todo el espacio entre celdas en la tabla ( \tabcolsep{0pt} arraystretch > 1) y luego agregar espacio entre columnas con columnas estrechas y entre filas con una combinación de reglas horizontales pesadas con el mismo color que la fila y (eventualmente) una estrecha, negra arrayrulesep.

Luego imitas la antigua forma de componer tabulares cuando el cajista espaciaba columnas y filas usando puntales de plomo.

[Planeo dar algunos ejemplos, pero me encontré con algunos efectos secundarios de un comando y primero necesito hacer una pregunta.]

Respuesta3

Con nicematrix(≥ 5,6) y booktabs, se tiene directamente el siguiente resultado.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{NiceTabular}{ll}[cell-space-limits=1pt]
\CodeBefore
  \rowcolor{gray!40}{1} 
  \rowcolors{2}{gray!20}{}
\Body
  \toprule
  My Head & \Block{}<\rotate>{My second\\ Head} \\ 
  \midrule
  some content & $\dfrac{1}{2}$ \\
  foo & bar \\ 
  \bottomrule
\end{NiceTabular}

\end{document}

Resultado del código anterior

Los comandos \toprule, midruley \bottomruleson proporcionados por booktabs; \Blocky \rotateson proporcionados por nicematrix. Las palabras clave \CodeBeforey \Bodyy los comandos dentro de ( \rowcolory \rowcolors) son proporcionados por nicematrix. El paquete colortblno está cargado. Podría haberse cargado con el mismo resultado.

información relacionada