![csvsimple 用一个条目创建多行](https://linux22.com/image/304558/csvsimple%20%E7%94%A8%E4%B8%80%E4%B8%AA%E6%9D%A1%E7%9B%AE%E5%88%9B%E5%BB%BA%E5%A4%9A%E8%A1%8C.png)
数据如下:
name,job,age
John,student,21
Frederik,student,18
Johnson,professor,49
我想生成一个表格,当第一次出现新工作时(列表按工作排序),它会生成一个新的表格行,其中只有job
,并且一行有两列name & age
,否则只有两列。
\begin{tabular}{|c|c|}
\hline
\multicolumn{2}{ |c| }{student} \\
\hline
John & 21 \\
Frederik & 18 \\
\hline
\multicolumn{2}{ |c|}{professor}\\
\hline
Johnson & 49\\
\hline
\end{tabular}
知道 csvsimple 是否支持这一点吗?
答案1
简短回答:是的 ;-)
这是我建议的完整解决方案。它不使用预定义选项,而是直接tabular
插入环境设置:tabular
\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}
\begin{filecontents*}{mylist.csv}
name,job,age
John,student,21
Frederik,student,18
Johnson,professor,49
\end{filecontents*}
\begin{document}
\csvreader[
head to column names,
before line={%
\ifthenelse{\equal{\job}{\myjob}}
{\\}%
{\xdef\myjob{\job}%
\\\hline\multicolumn{2}{|c|}{\myjob}\\\hline}%
},
before first line={%
\xdef\myjob{\job}%
\begin{tabular}{|c|c|}\hline%
\multicolumn{2}{|c|}{\myjob}\\\hline%
},
late after line=,
late after last line={\\\hline\end{tabular}},
]{mylist.csv}{}{\name & \age}
\end{document}
以下代码产生相同的结果,但是使用\csvloop
而不是\csvreader
:
\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}
\begin{filecontents*}{mylist.csv}
name,job,age
John,student,21
Frederik,student,18
Johnson,professor,49
\end{filecontents*}
\begin{document}
\csvloop{
file=mylist.csv,
head to column names,
before line=%
\ifthenelse{\equal{\job}{\myjob}}{%
\\%
}{%
\xdef\myjob{\job}%
\\\hline\multicolumn{2}{|c|}{\myjob}\\\hline%
},
before first line=%
\xdef\myjob{\job}%
\begin{tabular}{|c|c|}\hline%
\multicolumn{2}{|c|}{\myjob}\\\hline,
command=\name & \age,
late after line=,
late after last line=\\\hline\end{tabular},
}
\end{document}