colortbl 및 테이블 형식을 사용한 매크로 확장

colortbl 및 테이블 형식을 사용한 매크로 확장

표 형식 환경에서 행 색상을 번갈아 사용하고 싶습니다. 나는 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}

관련 정보