我想知道是否有其他方法可以在多行表中实现交替颜色。到处都有人使用“\multirow{-x}”,但我想知道是否有一种方法可以在不使用负数的情况下做到这一点,最好不要逐行写入。我想要一个这样的表格:
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with all cells in the same color.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
-&-\\
-&-\\
\cellcolor{lightgray}&Single-row\\
\cellcolor{lightgray}&Single-row\\
\multirow{-3}{*}{\cellcolor{lightgray}Multi-row (3)}&Single-row\\
-&-\\
-&-\\
\hline
\end{tabular}
\end{table}%
(不是我的代码,是在这里得到的:https://texblog.org/2014/05/19/coloring-multi-row-tables-in-latex/)
答案1
假设 a)\multirow
命令跨越奇数行且 b) 多行中的文本跨越单行,则可以使用以下命令:
\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with all cells in the same color.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
- & - \\
- & - \\
& Single-row \\
\cellcolor{lightgray}Multi-row (3) & Single-row \\
& Single-row \\
- & - \\
- & - \\
\hline
\end{tabular}
\end{table}%
\end{document}
答案2
您可以使用最新版本nicematrix
(2020-09-03 的 v. 5.3)来执行此操作。
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}
\begin{table}
\centering
\caption{Multirow table with all cells in the same color.}\label{tab:multi row}
\begin{NiceTabular}{p{5cm}p{5cm}}[code-before = \rowcolors{1}{}{lightgray}]
\hline
Column 1 & Column 2\\
\hline
- & - \\
- & - \\
\Block[l,fill=lightgray]{3-1}{Multi-row (3)} & Single-row \\
& Single-row \\
& Single-row \\
- & - \\
- & - \\
\hline
\end{NiceTabular}
\end{table}%
\end{document}
您需要多次编译。