Mesas coloridas com booktabs, makecell e cellspace

Mesas coloridas com booktabs, makecell e cellspace

Ao experimentar diferentes pacotes de tabelas, percebi que cada pacote basicamente representa si mesmo e muitas vezes carece de compatibilidade com outros pacotes excelentes.

Então tentei criar uma tabela no estilo do booktabs-package (só linhas horizontais), mas com a funcionalidade do makecell-package ( \thead, \makegapedcells, ...) e a possibilidade de colorir as linhas (o que não é muito intuitivo com booktabs, conforme discutidoaqui.)

Fiz uma pequena comparação entre diferentes configurações de tabelas:

\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}

Com o seguinte resultado: Diferentes configurações de tabela

Como você pode ver, em quase todas as combinações de pacotes existem algumas dificuldades (ou simplesmente não parecem boas). Meus favoritos são as versões 3 e 6.

Mas não encontrei soluções para células giradas ou usando\makegapedcells

Como você criaria tabelas coloridas no estilo booktabs? (E o que o booktabs-package realmente faz, exceto introduzir novas linhas horizontais e alterar o espaço vertical?). E por que \makegapedcellsexclui todas as cores, exceto dentro do \thead?

Responder1

Quanto ao espaçamento vertical nas células, o vencedor é… (drum roll)… cellspace, desde que você use da maneira correta – você simplesmente esqueceu de especificar os valores do espaçamento mínimo na parte superior e inferior das células.

\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} 

insira a descrição da imagem aqui

Responder2

Infelizmente, cellspacepode ocorrer altura irregular das linhas, dependendo do conteúdo da célula. Com todos os pacotes, você enfrentará o problema de as regras desaparecerem no navegador, porque a cor as sobrescreve.

O pacotechamadasé na verdade o único pacote que descobri que supera a maioria dos problemas.

Outra possibilidade é cancelar todo o espaço entre as células da tabela ( \tabcolsep{0pt} arraystretch > 1) e depois adicionar espaçamento entre colunas com colunas estreitas e entre linhas com uma combinação de réguas horizontais pesadas com a mesma cor da linha e (eventualmente) uma régua preta estreita arrayrulesep.

Você então imita a maneira antiga de compor tabelas, quando o compositor espaçava colunas e linhas usando suportes de chumbo.

[Pretendo dar alguns exemplos, mas encontrei alguns efeitos colaterais de um comando e preciso primeiro fazer uma pergunta!]

Responder3

Com nicematrix(≥ 5,6) e booktabs, você tem diretamente o seguinte 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 do código acima

Os comandos \toprule, midrulee \bottomrulesão fornecidos por booktabs; \Blocke \rotatesão fornecidos por nicematrix. As palavras-chave \CodeBeforee \Bodyos comandos dentro de ( \rowcolore \rowcolors) são fornecidos por nicematrix. O pacote colortblnão está carregado. Poderia ter sido carregado com o mesmo resultado.

informação relacionada