![如何将 CSV 文件导入我自己风格的表格?](https://linux22.com/image/304965/%E5%A6%82%E4%BD%95%E5%B0%86%20CSV%20%E6%96%87%E4%BB%B6%E5%AF%BC%E5%85%A5%E6%88%91%E8%87%AA%E5%B7%B1%E9%A3%8E%E6%A0%BC%E7%9A%84%E8%A1%A8%E6%A0%BC%EF%BC%9F.png)
我已经使用 LaTeX 撰写实验报告一年了,其中包括手动将数据输入表格。但是,现在我进行的实验包含的数据点要多得多,手动输入数据现在非常浪费时间。
我读过有关 csv 导入器命令的信息,其工作原理如下:
\documentclass{article}
\usepackage{csvsimple}
\begin{document}
\csvautotabular{data.csv}
\end{document}
但是,LaTeX 生成的表格并不符合我特别喜欢的风格。我使用的表格样式格式和样式如下所示:
\begin{center}
\captionof{table}[Short Caption for LoT]{Title of Table} % title of table
\begin{tabular}{c c}
\toprule
Heading 1 & Heading 2 \\ %heading for table
\midrule
Data1 & Data2 \\
Data3 & Data4 \\
\bottomrule
\label{table:tablename} \\
\end{tabular}
\end{center}
有没有办法将 CSV 文件导入这种格式?谢谢
答案1
csvtabular
已达\csvautobooktabular
到此目的。
\documentclass{article}
\usepackage{csvsimple,booktabs}
\usepackage{filecontents}
\begin{filecontents*}{mycsvdata.csv}
A, B
0.0, 1.0
0.0, -0.5
\end{filecontents*}
\begin{document}
\begin{table}[htb]
\centering
\caption[Short Caption for LoT]{Title of table}\label{table:tablename}
\csvautobooktabular{mycsvdata.csv}
\end{table}
\end{document}
您甚至可以使用S
来自的列类型siunitx
。(来自手册的示例)
\documentclass{article}
\usepackage{csvsimple,booktabs,array,siunitx}
\usepackage{filecontents}
\begin{filecontents*}{data_numbers.csv}
month, dogs, cats
January, 12.50,12.3e5
February, 3.32, 8.7e3
March, 43, 3.1e6
April, 0.33, 21.2e4
May, 5.12, 3.45e6
June, 6.44, 6.66e6
July, 123.2,7.3e7
August, 12.3, 5.3e4
September,2.3, 4.4e4
October, 6.5, 6.5e6
November, 0.55, 5.5e5
December, 2.2, 3.3e3
\end{filecontents*}
\begin{document}
\begin{table}[htb]
\centering
\caption[Short Caption for LoT]{Title of table}\label{table:tablename}
\csvloop{
file=data_numbers.csv,
head to column names,
before reading=\centering\sisetup{table-number-alignment=center},
tabular={lSS[table-format=2.2e1]},
table head=\toprule\textbf{Month} & \textbf{Dogs} & \textbf{Cats}\\\midrule,
command=\month & \dogs & \cats,
table foot=\bottomrule}
\end{table}
\end{document}
再举\csvreader
一个例子:
\documentclass{article}
\usepackage{csvsimple,booktabs,siunitx}
\usepackage{filecontents}
\begin{filecontents*}{data_headless.csv}
month, dogs, cats
January, 12.50,12.3e5
February, 3.32, 8.7e3
March, 43, 3.1e6
April, 0.33, 21.2e4
May, 5.12, 3.45e6
June, 6.44, 6.66e6
July, 123.2,7.3e7
August, 12.3, 5.3e4
September,2.3, 4.4e4
October, 6.5, 6.5e6
November, 0.55, 5.5e5
December, 2.2, 3.3e3
\end{filecontents*}
\begin{document}
\begin{table}[htb]
\centering
\caption[Short Caption for LoT]{Title of table}\label{table:tablename}
\csvreader[
tabular={lS[table-format=2.2e1]},
table head=\toprule\bfseries Month & \bfseries Cats\\\midrule,
table foot=\bottomrule]
{data_headless.csv}
{1=\month,3=\cats}
{\month & \cats}
\end{table}
\end{document}
答案2
一个简短的例子pgfplotstable
\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\pgfplotstableread{
A B
0.0 1.0
0.0 -0.5
}\datatable
\begin{document}
\begin{table}
\centering
\caption[Short Caption for LoT]{Title of table}
\pgfplotstabletypeset[%
every head row/.style={
before row=\toprule, after row=\midrule},
every last row/.style={
after row=\bottomrule},
]{\datatable}
\label{table:tablename}
\end{table}
\end{document}
答案3
只是:
\documentclass{article}
\usepackage{csvsimple}
\begin{document}
\csvautotabular{YOUR_CSV_FILE.csv}
\end{document}
这里了解更多信息