
Ich möchte die Zeilenfarben in einer tabellarischen Umgebung abwechseln. Ich fand colortbl nützlich. Um das Schreiben zu vereinfachen, habe ich die folgenden Makros zum Abwechseln der Zeilenfarben erstellt (siehe MWE unten). Ich verstehe jedoch nicht, warum das Makro nicht mit einem , während oder wie am Schnürchen laufen \crc
kann .\multicolumn
\rca
\rcb
Und, was noch wichtiger ist: Wie kann ich das Schreiben vereinfachen, indem ich beispielsweise für abwechselnde Farben immer den gleichen Makronamen verwende, selbst wenn ich verwende \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}
Antwort1
\multicolumn (und \rowcolor) muss das erste Element in einer Zelle sein. Etwas Inhalt davor ist möglich, muss aber erweiterbar sein und darf nur bis zu Leerzeichen erweitert werden.
Sie verwenden ein \gdef vor \multicolumn und dies ist nicht erweiterbar und funktioniert daher nicht.
\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}
Antwort2
Dank Ulrike Fischers Vorschlag zu \noalign
gibt es hier eine funktionierende Lösung:
\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}