帶有 booktabs、makecell 和 cellspace 的彩色表格

帶有 booktabs、makecell 和 cellspace 的彩色表格

在嘗試不同的表包時,我意識到每個包基本上都代表自己,並且通常缺乏與其他優秀包的兼容性。

因此,我嘗試以 -package 的樣式booktabs(僅水平線)創建一個表格,但具有makecell-package 的功能(\thead, \makegapedcells, ...)以及為行著色的可能性(這對於 , 來說不是很直觀booktabs)正如所討論的這裡.)

我對不同的表配置進行了一些比較:

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

結果如下: 不同的桌子配置

正如您所看到的,幾乎每個軟體包組合都存在一些困難(或看起來不太好)。我最喜歡的是版本 3 和版本 6。

但我沒有找到旋轉細胞的解決方案,或透過使用\makegapedcells

您將如何建立書籤樣式的彩色表格? (booktabs除了引入新的水平線和改變垂直空間之外,-package 實際上做了什麼?)。為什麼要\makegapedcells刪除除內部之外的所有顏色\thead

答案1

至於單元格中的垂直間距,獲勝者是…(鼓聲)… cellspace,只要您以正確的方式使用它 - 您只是忘記指定單元格頂部和底部的最小間距值。

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

在此輸入影像描述

答案2

不幸的是,cellspace根據單元格內容,行的高度可能不均勻。對於所有軟體包,您都會遇到規則在瀏覽器中消失的問題,因為顏色會覆蓋它們。

套餐卡路里實際上,這是我發現的唯一可以解決大多數問題的軟體包。

另一種可能性是取消表格中單元格之間的所有間距 ( \tabcolsep{0pt} arraystretch > 1),然後使用窄列在列之間添加間距,並使用與行顏色相同的粗水平線和(最終)窄黑色的組合在行之間添加間距arrayrulesep

然後,當排版人員使用鉛支柱分隔列和行時,您可以模仿排版表格的舊方法。

[我打算舉一些例子,但我遇到了命令的一些副作用,需要先問一個問題! ]

答案3

對於nicematrix(≥ 5.6) 和booktabs,您可以直接得到以下結果。

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

上述程式碼的結果

命令\toprulemidrule\bottomrule由 提供booktabs\Block並由\rotate提供nicematrix。 (和) 中的關鍵字\CodeBeforeand\Body和指令由 提供。包未載入。它可能會載入相同的結果。\rowcolor\rowcolorsnicematrixcolortbl

相關內容