我正在尝试使多列表格采用从 CSV 文件中读取的动态值。当前表如下所示,并且已使用 手动格式化tabularx
,没有任何多列包。
\begin{tabularx}{\textwidth}{|l l X|l l X|}
\hline
name1 & nr1 & & name4 & nr4 & \\
name2 & nr2 & & name5 & nr5 & \\
name3 & nr3 & & name6 & nr6 & \\
\hline
\end{tabularx}
重要的是,第三列和第六列是空的,并且尽可能大,同时大小相同;表格应该跨越整个页面的宽度。
我尝试了多种方法从 CSV 文件动态创建此表,但我甚至无法重现我能够静态创建的内容。这是一个使用包datatool
与的示例方法multicol
,它可能最接近我想要实现的目标,尽管仍然相差甚远:
\documentclass[a4paper]{extarticle}
\usepackage{lmodern,datatool,tabularx,multicol}
\setlength{\columnsep}{0pt}
\begin{filecontents*}{data.csv}
Name,Number
name1,nr1
name2,nr2
name3,nr3
name4,nr4
name5,nr5
name6,nr6
name7,nr7
name8,nr8
name9,nr9
name10,nr10
name11,nr11
name12,nr12
name13,nr13
name14,nr14
name15,nr15
name16,nr16
name17,nr17
name18,nr18
name19,nr19
name20,nr20
name21,nr21
name22,nr22
name23,nr23
name24,nr24
name25,nr25
name26,nr26
name27,nr27
name28,nr28
name29,nr29
name30,nr30
name31,nr31
name32,nr32
name33,nr33
name34,nr34
name35,nr35
name36,nr36
name37,nr37
name38,nr38
name39,nr39
name40,nr40
name41,nr41
name42,nr42
name43,nr43
name44,nr44
name45,nr45
name46,nr46
name47,nr47
name48,nr48
name49,nr49
name50,nr50
name51,nr51
name52,nr52
name53,nr53
name54,nr54
name55,nr55
name56,nr56
name57,nr57
name58,nr58
name59,nr59
name60,nr60
\end{filecontents*}
\begin{document}
\DTLloaddb{Data}{data.csv}
\noindent
\begin{multicols}{2}
\begin{tabularx}{\columnwidth}{|l l X|}
\hline
\DTLforeach*{Data}{\Name=Name,\Number=Number}{
\Name & \Number & \\
}
\hline
\end{tabularx}
\end{multicols}
\end{document}
由于最终结果显然与预期相差甚远,我寻找替代方案并找到了该csvsimple
软件包,但我也无法将其与结合使用tabularx
。如果有人能帮助我弄清楚这里出了什么问题以及如何修复它,我将不胜感激。
答案1
如果您准备使用,pgfplotstable
那么可以这样做。它包含一个构造,用于将列分成相等的部分以模拟多列表,即select equal part entry of
。将分成两列是通过将第一列赋予样式select equal part entry of={0}{2}
,第二列赋予样式来实现的select equal part entry of={1}{2}
。
tabularx
你可以使用以下选项设置输出
begin table=\begin{tabularx}{\textwidth},
end table=\end{tabularx}
到\pgfplotstabletypeset
,然后组合
columns={Name,Number,XXX,Name,Number,XXX},
display columns/0/.style={select equal part entry of={0}{2},column type=|c},
display columns/1/.style={select equal part entry of={0}{2}},
display columns/2/.style={column type=X|},
display columns/3/.style={select equal part entry of={1}{2},column type=c},
display columns/4/.style={select equal part entry of={1}{2}},
display columns/5/.style={column type=X|}
为划分的列提供适当的格式。空XXX
列由
create on use/XXX/.style={create col/set={}}
您需要通过以下方式指定数据为逗号分隔的字符串类型
col sep=comma,string type
最后,在下面的例子中,我扔掉了标题并添加了与您的问题相对应的水平规则。
\documentclass[a4paper]{article}
\usepackage{lmodern,pgfplotstable,tabularx}
\pgfplotsset{compat=1.12}
\begin{filecontents*}{data.csv}
Name,Number
name1,nr1
name2,nr2
name3,nr3
name4,nr4
name5,nr5
name6,nr6
name7,nr7
name8,nr8
name9,nr9
name10,nr10
name11,nr11
name12,nr12
name13,nr13
name14,nr14
name15,nr15
name16,nr16
name17,nr17
name18,nr18
name19,nr19
name20,nr20
name21,nr21
name22,nr22
name23,nr23
name24,nr24
name25,nr25
name26,nr26
name27,nr27
name28,nr28
name29,nr29
name30,nr30
name31,nr31
name32,nr32
name33,nr33
name34,nr34
name35,nr35
name36,nr36
name37,nr37
name38,nr38
name39,nr39
name40,nr40
name41,nr41
name42,nr42
name43,nr43
name44,nr44
name45,nr45
name46,nr46
name47,nr47
name48,nr48
name49,nr49
name50,nr50
name51,nr51
name52,nr52
name53,nr53
name54,nr54
name55,nr55
name56,nr56
name57,nr57
name58,nr58
name59,nr59
name60,nr60
\end{filecontents*}
\begin{document}
\pgfplotstableset{create on use/XXX/.style={create col/set={}}}%
\noindent
\pgfplotstabletypeset[col sep=comma,string type,
begin table=\begin{tabularx}{\textwidth},
end table=\end{tabularx},
every head row/.style={output empty row,after row=\hline},
every last row/.style={after row=\hline},
columns={Name,Number,XXX,Name,Number,XXX},
display columns/0/.style={select equal part entry of={0}{2},column type=|c},
display columns/1/.style={select equal part entry of={0}{2}},
display columns/2/.style={column type=X|},
display columns/3/.style={select equal part entry of={1}{2},column type=c},
display columns/4/.style={select equal part entry of={1}{2}},
display columns/5/.style={column type=X|}
]{data.csv}
\end{document}
答案2
这不是你想要的但也许它有帮助
\documentclass[a4paper]{extarticle}
\usepackage{lmodern,datatool,tabularx,multicol,ifthen}
\setlength{\columnsep}{0pt}
\begin{filecontents*}{data.csv}
Name,Number
name1,nr1
name2,nr2
name3,nr3
name4,nr4
name5,nr5
name6,nr6
name7,nr7
name8,nr8
name9,nr9
name10,nr10
name11,nr11
name12,nr12
name13,nr13
name14,nr14
name15,nr15
name16,nr16
name17,nr17
name18,nr18
name19,nr19
name20,nr20
name21,nr21
name22,nr22
name23,nr23
name24,nr24
name25,nr25
name26,nr26
name27,nr27
name28,nr28
name29,nr29
name30,nr30
name31,nr31
name32,nr32
name33,nr33
name34,nr34
name35,nr35
name36,nr36
name37,nr37
name38,nr38
name39,nr39
name40,nr40
name41,nr41
name42,nr42
name43,nr43
name44,nr44
name45,nr45
name46,nr46
name47,nr47
name48,nr48
name49,nr49
name50,nr50
name51,nr51
name52,nr52
name53,nr53
name54,nr54
name55,nr55
name56,nr56
name57,nr57
name58,nr58
name59,nr59
name60,nr60
\end{filecontents*}
\def\mtodd{\Name & \Number & &}
\def\mteven{\mtevenL \\}
\def\mtoddL{\mtodd & & }
\def\mtevenL{\Name & \Number & }
\begin{document}
\DTLloaddb{Data}{data.csv}
\begin{multicols}{2}
\begin{tabularx}{\textwidth}{|l l X|l l X|}
\hline
\DTLforeach*{Data}{\Name=Name,\Number=Number}{%
\DTLifoddrow{\DTLiflastrow{\mtoddL}{\mtodd}}{%
\DTLiflastrow{\mtevenL}{\mteven}}}
\\\hline
\end{tabularx}
\end{multicols}
\end{document}
更新:
\documentclass[a4paper]{extarticle}
\usepackage{lmodern,datatool,tabularx,multicol,ifthen}
\setlength{\columnsep}{0pt}
\begin{filecontents*}{data.csv}
Name,Number
name1,nr1
name2,nr2
name3,nr3
name4,nr4
name5,nr5
name6,nr6
name7,nr7
name8,nr8
name9,nr9
name10,nr10
name11,nr11
name12,nr12
name13,nr13
name14,nr14
name15,nr15
name16,nr16
name17,nr17
name18,nr18
name19,nr19
name20,nr20
name21,nr21
name22,nr22
name23,nr23
name24,nr24
name25,nr25
name26,nr26
name27,nr27
name28,nr28
name29,nr29
name30,nr30
name31,nr31
name32,nr32
name33,nr33
name34,nr34
name35,nr35
name36,nr36
name37,nr37
name38,nr38
name39,nr39
name40,nr40
name41,nr41
name42,nr42
name43,nr43
name44,nr44
name45,nr45
name46,nr46
name47,nr47
name48,nr48
name49,nr49
name50,nr50
name51,nr51
name52,nr52
name53,nr53
name54,nr54
name55,nr55
name56,nr56
name57,nr57
name58,nr58
name59,nr59
name60,nr60
\end{filecontents*}
\begin{document}
\DTLloaddb{Data}{data.csv}
\begin{multicols}{2}
\begin{tabularx}{\textwidth}{|l l X|l l X|}
\hline
\DTLforeach*{Data}{\Name=Name,\Number=Number}{%
\DTLiffirstrow{}{\DTLifoddrow{\\}{&}}%
\Name & \Number &}%
\\\hline
\end{tabularx}
\end{multicols}
\end{document}