我正在尝试使用带有表格选项的 xcolor 包将交替颜色应用于我的文档。据我所知,可以在序言中定义交替颜色,以便将它们应用于所有后续表格。
但如果我这样使用,就不会得到相同的表格颜色。这种方法哪里错了?
\documentclass{scrbook}
\usepackage[table]{xcolor}
\rowcolors{2}{gray}{white}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|ll|}
\hline
$1_1$ & $1_2$ \\
\hline
$2_1$ & $2_2$ \\
\hline
$3_1$ & $3_2$ \\
\hline
$4_1$ & $4_2$ \\
\hline
\end{tabular}
\end{table}
Text
\begin{table}[h]
\centering
\begin{tabular}{|ll|}
\hline
$1_1$ & $1_2$ \\
\hline
$2_1$ & $2_2$ \\
\hline
$3_1$ & $3_2$ \\
\hline
$4_1$ & $4_2$ \\
\hline
\end{tabular}
\end{table}
\end{document}
答案1
尝试这个:
\documentclass{scrbook}
\usepackage[table]{xcolor}
\rowcolors{2}{gray}{white}
\let\oldtabular\tabular
\def\tabular{\global\rownum=0\relax\oldtabular}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|ll|}
\hline
$1_1$ & $1_2$ \\
\hline
$2_1$ & $2_2$ \\
\hline
$3_1$ & $3_2$ \\
\hline
$4_1$ & $4_2$ \\
\hline
\end{tabular}
\end{table}
Text
\begin{table}[h]
\centering
\begin{tabular}{|ll|}
\hline
$1_1$ & $1_2$ \\
\hline
$2_1$ & $2_2$ \\
\hline
$3_1$ & $3_2$ \\
\hline
$4_1$ & $4_2$ \\
\hline
\end{tabular}
\end{table}
\end{document}