Экспериментируя с различными пакетами таблиц, я понял, что каждый пакет по сути представляет собой нечто само по себе и часто несовместим с другими хорошими пакетами.
Поэтому я попытался создать таблицу в стиле -пакета booktabs
(только горизонтальные линии), но с функциональностью -пакета makecell
( \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? (И что 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}
Команды \toprule
, midrule
и \bottomrule
предоставляются booktabs
; \Block
и \rotate
предоставляются nicematrix
. Ключевые слова \CodeBefore
и \Body
и команды внутри ( \rowcolor
и \rowcolors
) предоставляются nicematrix
. Пакет colortbl
не загружен. Он мог быть загружен с тем же результатом.