Farbige Tabellen mit Booktabs, Makecell und Cellspace

Farbige Tabellen mit Booktabs, Makecell und Cellspace

Beim Experimentieren mit verschiedenen Tabellenpaketen wurde mir klar, dass jedes Paket grundsätzlich für sich steht und es ihm oft an Kompatibilität mit anderen großartigen Paketen mangelt.

Ich habe also versucht, eine Tabelle im Stil des booktabs-Pakets (nur horizontale Linien) zu erstellen, aber mit der Funktionalität des makecell-Pakets ( \thead, \makegapedcells, ...) und der Möglichkeit, die Zeilen einzufärben (was bei nicht sehr intuitiv ist booktabs, wie besprochenHier.)

Ich habe einen kleinen Vergleich zwischen verschiedenen Tischkonfigurationen gemacht:

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

Mit folgendem Ergebnis: Verschiedene Tischkonfigurationen

Wie man sieht, gibt es bei fast jeder Paketkombination einige Schwierigkeiten (oder es sieht einfach nicht gut aus). Meine Favoriten sind die Versionen 3 und 6.

Ich habe jedoch keine Lösung für gedrehte Zellen gefunden oder durch die Verwendung von\makegapedcells

Wie erstellt man farbige Tabellen im Booktabs-Stil? (Und was macht das booktabs-Paket eigentlich, außer neue horizontale Linien einzuführen und den vertikalen Abstand zu ändern?). Und warum werden \makegapedcellsalle Farben außer innerhalb des gelöscht \thead?

Antwort1

Was den vertikalen Abstand in Zellen betrifft, ist der Gewinner … (Trommelwirbel) … cellspace, vorausgesetzt, Sie verwenden ihn richtig – Sie haben einfach vergessen, die Werte für den Mindestabstand oben und unten in den Zellen anzugeben.

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

Bildbeschreibung hier eingeben

Antwort2

Leider cellspacekann es je nach Zellinhalt zu einer ungleichmäßigen Höhe der Zeilen kommen. Bei allen Paketen tritt das Problem auf, dass die Regeln im Browser verschwinden, weil sie durch die Farbe überschrieben werden.

Das PaketKalorienist tatsächlich das einzige Paket, das ich gefunden habe, das die meisten Probleme löst.

Eine andere Möglichkeit besteht darin, den gesamten Abstand zwischen den Zellen in der Tabelle aufzuheben ( \tabcolsep{0pt} arraystretch > 1) und dann mit schmalen Spalten Abstände zwischen den Spalten und mit einer Kombination aus dicken, horizontalen Linien in der gleichen Farbe wie die Zeile und (eventuell) einem schmalen, schwarzen Abstand zwischen den Zeilen hinzuzufügen arrayrulesep.

Sie ahmen dann die alte Methode zum Setzen von Tabellen nach, bei der der Schriftsetzer die Abstände zwischen Spalten und Zeilen mithilfe von Bleistreben festlegte.

[Ich habe vor, einige Beispiele zu geben, bin aber auf einige Nebenwirkungen eines Befehls gestoßen und muss zuerst eine Frage stellen!]

Antwort3

Mit nicematrix(≥ 5,6) und booktabserhalten Sie direkt folgendes Ergebnis.

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

Ergebnis des obigen Codes

Die Befehle \toprule, midruleund \bottomrulewerden bereitgestellt von booktabs; \Blockund \rotatewerden bereitgestellt von nicematrix. Die Schlüsselwörter \CodeBeforeund \Bodyund die Befehle innerhalb von ( \rowcolorund \rowcolors) werden bereitgestellt von nicematrix. Das Paket colortblwurde nicht geladen. Es hätte mit demselben Ergebnis geladen werden können.

verwandte Informationen