如何使用 csvloop 获取条纹表?

如何使用 csvloop 获取条纹表?

是否可以使用csvloop命令更改行颜色以获得条纹长表?相当于 late after line=\csvifoddrow{\\\rowcolor{white}{\\\rowcolor{gray!20}},我们使用csvreader

举例来说,是否可以调整以下 MWE?

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{csvsimple}
\usepackage{hyperref}



\begin{document}

\begin{filecontents*}{test.csv}
  a|b|c
  1|2|3
  4|5|6
  7|8|9
  10|11|12
  13|14|15
  16|17|18  
\end{filecontents*}

\csvloop{
  file=test.csv,
  respect all,
  separator=pipe,
  no head,
  before reading={
    \begin{longtable}{lll}
    \toprule
  },
  command={\csviffirstrow
    {\textbf{\csvcoli} & \textbf{\csvcolii} & \textbf{\csvcoliii} }
    {\csvcoli & \csvcolii & \csvcoliii }
  },
  late after line={\\},
  late after first line={\\\midrule\endhead},
  late after last line={\\\bottomrule},
  after reading={
    \end{longtable}
  }
}


\end{document}

答案1

xcolor(带有table包选项)提供了\rowcolors命令,用于设置交替行颜色。此命令在启动表格之前使用,因此可以包含在中before reading

梅威瑟:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{csvsimple}
\usepackage{hyperref}

\begin{document}

\begin{filecontents*}{test.csv}
  a|b|c
  1|2|3
  4|5|6
  7|8|9
  10|11|12
  13|14|15
  16|17|18  
\end{filecontents*}

\csvloop{
  file=test.csv,
  respect all,
  separator=pipe,
  no head,
  before reading={
    \rowcolors{2}{gray!25}{white}
    \begin{longtable}{lll}
    \toprule
  },
  command={\csviffirstrow
    {\textbf{\csvcoli} & \textbf{\csvcolii} & \textbf{\csvcoliii} }
    {\csvcoli & \csvcolii & \csvcoliii }
  },
  late after line={\\},
  late after first line={\\\midrule\endhead},
  late after last line={\\\bottomrule},
  after reading={
    \end{longtable}
  }
}


\end{document}

结果:

在此处输入图片描述

相关内容