我正在尝试添加多行对此使用 longtable 为行着色。
在此示例中,如何添加多行?
这是我的尝试:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\title{A random title}
% Add longtable and xcolor packages and define two colors
\usepackage{longtable}
\usepackage[dvipsnames,table]{xcolor}
\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}
% Add the multirow package
\usepackage{multirow, makecell}
\begin{document}
\maketitle
% Table
\rowcolors{1}{light-cyan}{light-gray}
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
\hiderowcolors
\caption{Fine caption}\\
\showrowcolors
\hline
\rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
\hline
\rowcolor{yellow}Head 1 & Head 2 & Head 3\\\hline\endhead
\hline\endfoot
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
% attempt to insert multirows
\multirow{2}{*}[0em]{Saturday} & \multirow{2}{*}[0em]{$17$} & \\
& & \\
% attempt to add a horizontal line
\cline{2-3}
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\end{longtable}
\end{document}
目前的情况如下:
答案1
将“底部”的单元格与负数单元格合并。似乎应用于行的颜色与文本重叠。如果移动\multirow
到底部,则可以在应用颜色后排版文本。
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\title{A random title}
% Add longtable and xcolor packages and define two colors
\usepackage{longtable}
\usepackage[dvipsnames,table]{xcolor}
\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}
% Add the multirow package
\usepackage{multirow, makecell}
\begin{document}
\maketitle
% Table
\rowcolors{1}{light-cyan}{light-gray}
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
\hiderowcolors
\caption{Fine caption}\\
\showrowcolors
\hline
\rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
\hline
\rowcolor{yellow}Head 1 & Head 2 & Head 3\\\hline\endhead
\hline\endfoot
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
& & \\
\multirow{-2}{*}[0em]{Saturday} & \multirow{-2}{*}[0em]{$17$} & \\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\end{longtable}
\end{document}
代码的附加变体用于保持“合并”单元格的相同背景颜色 - 仅提供代码片段,而其余代码相同。
重置计数器的变体rownum
(基于这个问题)
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
\hiderowcolors
\caption{Fine caption}\\
\hline
\rowcolor{magenta}%
Head 1 & Head 2 & Head 3\\\hline\endfirsthead
\hline
\rowcolor{yellow}%
Head 1 & Head 2 & Head 3\\\hline\endhead
\hline\endfoot
\showrowcolors
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\global\rownum=1\relax% applies to the row after this one
& & \\
\multirow{-2}{*}[0em]{Saturday} & \multirow{-2}{*}[0em]{$17$} & \\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\end{longtable}
具有内部的变体tabular
:
%
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
\hiderowcolors
\caption{Fine caption}\\
\showrowcolors
\hline
\rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
\rowcolor{yellow} Head 1 & Head 2 & Head 3\\\hline\endhead
\hline\endfoot
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Saturday & $17$ & \begin{tabular}{@{}l@{}}
A\\
B\\
C
\end{tabular} \\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\end{longtable}
通过支柱进行“拉伸”行的变体:
\begin{longtable}{|*{3}{p{.3\textwidth}|}}
\hiderowcolors
\caption{Fine caption}\\
\showrowcolors
\hline
\rowcolor{magenta}Head 1 & Head 2 & Head 3\\\hline\endfirsthead
\rowcolor{yellow} Head 1 & Head 2 & Head 3\\\hline\endhead
\hline\endfoot
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\rule[\dimexpr-\baselineskip+0.5ex]{0pt}{2\baselineskip}%
Saturday & $17$ & \\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\end{longtable}
答案2
- 正如我看到您的表格,您不需要多行单元格。
- 使用
\longtblr
包tabularray
启用编写简洁的表格代码:
\documentclass{article}
\usepackage[dvipsnames,table]{xcolor}
\definecolor{light-gray}{HTML}{E5E4E2}
\definecolor{light-cyan}{HTML}{E0FFFF}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{longtblr}[
caption = {Fine caption},
label = {tab:long}
]{vlines = {gray},
colspec = { *{3}{X[l]} },
row{odd} = {bg=light-cyan},
row{even}= {bg=light-gray},
row{1} = {bg=purple!80, c},
%
rowhead=1
}
\toprule[gray]
Head 1 & Head 2 & Head 3 \\
\midrule[gray]
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\SetRow{ht=2\baselineskip}
Saturday & 17 & \\
Content 1 & Content 2 & Content 3\\
Mondday & 19 & {A\\ B\\ C} \\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
Content 1 & Content 2 & Content 3\\
\bottomrule
\end{longtblr}
\end{document}