Цветные таблицы с закладками, makecell и cellspace

Цветные таблицы с закладками, makecell и cellspace

Экспериментируя с различными пакетами таблиц, я понял, что каждый пакет по сути представляет собой нечто само по себе и часто несовместим с другими хорошими пакетами.

Поэтому я попытался создать таблицу в стиле -пакета 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не загружен. Он мог быть загружен с тем же результатом.

Связанный контент