
Я хотел бы чередовать цвета строк в табличной среде. Я нашел colortbl полезным. Чтобы упростить написание, я создал следующие макросы для чередования цветов строк (см. MWE ниже). Однако я не могу понять, почему макрос \crc
не может сосуществовать с \multicolumn
, тогда как \rca
или \rcb
работают как по волшебству.
И, что еще важнее, как мне упростить написание, например, если я всегда использую одно и то же имя макроса для чередования цветов, даже если я использую \multicolumn
?
\documentclass {article}
\usepackage {colortbl}
\begin {document}
% define row colors A and B
\def\rca{\rowcolor [rgb] {0.90, 0.95, 1.0}}
\def\rcb{\rowcolor [rgb] {0.80, 0.90, 1.0}}
% This one works like a charm
\begin {tabular} {|l|l|} \hline
\rca \multicolumn {1} {|c|} {one} & 1 \\ \hline
\rcb two & 2 \\ \hline
\rca three & 3 \\ \hline
\end {tabular}
% use current row color and set color for next row
\def\cseta{\gdef\crc{\rca\csetb}}
\def\csetb{\gdef\crc{\rcb\cseta}}
\cseta % (re)set \crc to the row color A
% This one does not want to compile!
% ! Misplaced \omit.
% \multispan ->\omit
% \@multispan
% l.23 \crc \multicolumn {1} {|c} {one}
% & 1 \\ \hline
\begin {tabular} {|l|l|} \hline
\crc \multicolumn {1} {|c} {one} & 1 \\ \hline
\crc two & 2 \\ \hline
\crc three & 3 \\ \hline
\end {tabular}
\end {document}
решение1
\multicolumn (и \rowcolor) хочет быть первым в ячейке. Некоторое содержимое перед ним возможно, но оно должно быть расширяемым и расширяться только до пробелов.
Вы используете \gdef перед \multicolumn, а он не расширяется и поэтому не будет работать.
\documentclass{article}
\usepackage{colortbl}
\newcommand\CAred{\rowcolor{red}}
\DeclareRobustCommand\CBred{\rowcolor{red}}
\newcommand\CCred[1][]{\rowcolor{red}}
\newcommand\CD{ }
\newcommand\CE{\,}
\begin{document}
\begin{tabular}{l}
\rowcolor{red}\multicolumn{1}{r}{blub}\\ %work
\CAred\multicolumn{1}{r}{blub} \\ %work
%\CBred\multicolumn{1}{r}{blub} \\ %fails (robust command)
%\CCred\multicolumn{1}{r}{blub} \\ %fails (optional argument -> not expandable
\CD\multicolumn{1}{r}{blub} \\ %works
%\CE\multicolumn{1}{r}{blub} \\ %fails (content)
%\relax\multicolumn{1}{r}{blub} \\ %fails
%\def\xy{blub}\multicolumn{1}{r}{blub}\\ %fails (definition)
%\parindent=10pt \multicolumn{1}{r}{blub}\\ %fails (assignment)
%blub\multicolumn{1}{r}{blub} \\ %fails (content)
\end{tabular}
\end{document}
решение2
Благодаря предложению Ульрики Фишер \noalign
, вот работающее решение:
\documentclass {article}
\usepackage {colortbl}
\begin {document}
% define row colors A and B
\def\rca{\rowcolor [rgb] {0.90, 0.95, 1.0}}
\def\rcb{\rowcolor [rgb] {0.80, 0.90, 1.0}}
\begin {tabular} {|l|l|} \hline
\rca \multicolumn {1} {|c|} {one} & 1 \\ \hline
\rcb two & 2 \\ \hline
\rca three & 3 \\ \hline
\end {tabular}
% use current row color and set color for next row
\def\cseta{\noalign {\gdef\crc{\rca\csetb}}}
\def\csetb{\noalign {\gdef\crc{\rcb\cseta}}}
\gdef\crc{\rca\csetb} % (re)set \crc to the row color A
\begin {tabular} {|l|l|} \hline
\crc \multicolumn {1} {|c} {one} & 1 \\ \hline
\crc two & 2 \\ \hline
\crc three & 3 \\ \hline
\end {tabular}
\end {document}