colortbl と tabular によるマクロ拡張

colortbl と tabular によるマクロ拡張

表形式の環境で行の色を交互に変更したいと思います。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) は、セル内の最初の要素である必要があります。その前に何らかのコンテンツを配置することは可能ですが、拡張可能で、スペースのみに拡張する必要があります。

\multicolumn の前に \gdef を使用していますが、これは拡張できないため機能しません。

\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

についての Ulrike Fischer の提案のおかげで\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}

関連情報