![用字符串从 CSV 文件创建表?](https://linux22.com/image/301300/%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BB%8E%20CSV%20%E6%96%87%E4%BB%B6%E5%88%9B%E5%BB%BA%E8%A1%A8%EF%BC%9F.png)
我想在 LaTex 中从 CSV 文件创建表格。我可以用数字来做到这一点这里但创建诸如缩写之类的字符串的表不起作用。问题是什么?
血管内皮生长因子
此示例触发错误“包 PGF 数学错误:无法将输入‘...’解析为浮点数,抱歉。”
\documentclass{standalone} \usepackage{pgfplotstable,filecontents} \pgfplotsset{compat=1.9}% supress warning \begin{filecontents*}{test.csv} Abbreviation, Description ACG, Azeri Chirag Guneshli bcm, Billion cubic meters BTC, Baku Tbilisi Ceyhan CIA, Central Intelligence Agency Btu, British thermal unit CAC, Central Asia Center EU, European Union LNG, Liquified Natural Gas NATO, North Atlantic Treaty Organization OMV, Österreichische Mineralölverwaltung \end{filecontents*} \begin{document} \pgfplotstabletypeset[col sep=comma, columns={Abbreviation,Description}]{test.csv} \end{document}
答案1
添加string type
选项:
笔记:
为了控制每行的对齐方式,您可以指定每行的样式。
columns/Abbreviation/.style={column type=l}, columns/Description/.style={column type=l},
或者,你可以设置对齐方式全部列通过
column type=l
。为了添加水平线,我使用了这
booktabs
以及every head row
和every last row
键。
代码:
\documentclass[border=5pt]{standalone}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.9}% supress warning
%\usepackage{filecontents}% <-- commented to prevent overwriting fuel
\begin{filecontents*}{test.csv}
Abbreviation, Description
ACG, Azeri Chirag Guneshli
bcm, Billion cubic meters
BTC, Baku Tbilisi Ceyhan
CIA, Central Intelligence Agency
Btu, British thermal unit
CAC, Central Asia Center
EU, European Union
LNG, Liquified Natural Gas
NATO, North Atlantic Treaty Organization
OMV, Österreichische Mineralölverwaltung
\end{filecontents*}
\begin{document}
\pgfplotstabletypeset[
string type,
col sep=comma,
columns={Abbreviation,Description},
columns/Abbreviation/.style={column type=l},
columns/Description/.style={column type=l},
every head row/.style={before row=\toprule,after row=\cmidrule(lr){1-1}\cmidrule(lr){2-2}},
every last row/.style={after row=\bottomrule}
]{test.csv}
\end{document}
答案2
这是经过轻微修改的版本某人的回答
\documentclass{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
Abbreviation, Description
ACG, Azeri Chirag Guneshli
bcm, Billion cubic meters
BTC, Baku Tbilisi Ceyhan
CIA, Central Intelligence Agency
Btu, British thermal unit
CAC, Central Asia Center
EU, European Union
LNG, Liquified Natural Gas
NATO, North Atlantic Treaty Organization
OMV, Österreichische Mineralölverwaltung
\end{filecontents*}
\begin{document}
\begin{tabular}{ll}%
\toprule
\bfseries Abbreviation & \bfseries Description% specify table head
\csvreader[%
head to column names,
after head=\\\midrule,
late after line=\\,
]{\jobname.csv}{}% use head of csv as column names
{\Abbreviation & \Description}% specify your coloumns here
\bottomrule
\end{tabular}
\end{document}
head to column names
让您说\Abbreviation
而\Description
不是使用内部\csvcoli
等等。- 如果从主定义中去掉规则和新行,则可以更好地调整格式。
after head
设置标题行结尾的代码 (\\\midrule
)late after line
设置表格每行末尾的代码 (\\
)- 然后,您可以在表格的头部和脚部使用
\toprule
和\bottomrule
,而不会出现对齐字符放错位置或表格最后一行与最后一条规则之间垂直间距过大的问题。