我试图同时获得一个自动列出行号但颜色与命令类似的列xcolor
。\rowcolors
也就是说,我试图获得类似于以下内容的输出:
\usepackage[table]{xcolor}
\begin{document}
\rowcolors{2}{green!60!yellow!40}{green!40!yellow!20}
\begin{tabular}{|p{2em}|p{1in}|p{1in}|}
\hline
\cellcolor{violet!20} ID & \cellcolor{violet!20} Description & \cellcolor{violet!20} Location\\
1 & Lorem & ipsum\\
2 & Dolor & dolor\\
3 & Foo & bar\\
\hline
\end{tabular}
\end{document}
使用类似于以下的实现:
\usepackage[table]{xcolor}
\begin{document}
\newcounter{rownum}
\setcounter{rownum}{1}
\newcommand{\rownumber}{\arabic{rownum} \stepcounter{rownum}}
\rowcolors{2}{green!60!yellow!40}{green!40!yellow!20}
\begin{tabular}{|@{\makebox[2em][r]{\rownumber~}}|p{1in}|p{1in}|}
\hline
\multicolumn{1}{|@{\makebox[2em][r]{ID~}} | l |}{\cellcolor{violet!20}Description} & \cellcolor{violet!20} Location\\
Lorem & ipsum\\
Dolor & dolor\\
Foo & bar\\
\hline
\end{tabular}
\end{document}
我怎样才能从一开始就获得颜色,而不需要手动输入数字(或在每行前面添加命令)?
答案1
这是一种可能的方法:
>{command}
允许包含格式化命令,这些命令将在每个单元格中执行。此外,您还需要在\cellcolor{...}
内部定义单元格颜色\multicolumn
\documentclass{standalone}
\usepackage{array}
\usepackage[table]{xcolor}
\newcounter{rownum}
\setcounter{rownum}{0}
\rowcolors{2}{green!60!yellow!40}{green!40!yellow!20}
\begin{document}
\begin{tabular}{|>{\stepcounter{rownum}\therownum}p{1em}|p{0.75in}|p{0.75in}|}
\hline
\multicolumn{1}{|p{1em}|}{\cellcolor{violet!20}{ID}} & \cellcolor{violet!20}Description & \cellcolor{violet!20} Location\\
&Lorem & ipsum\\
&Dolor & dolor\\
&Foo & bar\\
\hline
\end{tabular}
\end{document}