
我想知道是否有不同的方法來在多行表中執行替代顏色。人們到處都使用“\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}
你需要幾個編譯。