![如何使用 bookstyle 将大型 .csv 文件读取为表格?](https://linux22.com/image/456955/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20bookstyle%20%E5%B0%86%E5%A4%A7%E5%9E%8B%20.csv%20%E6%96%87%E4%BB%B6%E8%AF%BB%E5%8F%96%E4%B8%BA%E8%A1%A8%E6%A0%BC%EF%BC%9F.png)
我需要在我的硕士论文附录中插入大型表格,并且我想从.csv
文件中读取数据,而无需填写.tex
我写作的文件。我借助表生成器。让我们考虑以下剪切.csv
文件:
1, 459872 , 2459600.5, 0.245955429393969E+07, 0.150902180397680E+09, 0.700936137135535E-01, 0.444423490130398E+02, 0.509894719093894E+02, 0.449518800380460E+02, 0.972856801473429E+00
2, 1991 VG , 2459600.5, 0.245965301734296E+07, 0.154448562453878E+09, 0.524440272062934E-01, 0.981969077945011E+02, 0.305901193619353E+03, 0.310657729272394E+03, 0.939542405344313E+00
3, 2000 SG344, 2459600.5, 0.245967142235446E+07, 0.146215475380197E+09, 0.668906906224866E-01, 0.107259306095888E+03, 0.280186369328737E+03, 0.287658863754547E+03, 0.102000471918105E+01
4, 2003 YN107, 2459600.5, 0.245973765703815E+07, 0.147913409318582E+09, 0.138963447919681E-01, 0.352070216744272E+03, 0.221439015133514E+03, 0.222501180674322E+03, 0.100249189670507E+01
5, 2006 JY26 , 2459600.5, 0.245950771201134E+07, 0.151137260433216E+09, 0.831296262363333E-01, 0.317110762865456E+03, 0.995403784006208E+02, 0.900588991605521E+02, 0.970587901094867E+00
6, 2006 QQ56 , 2459600.5, 0.245948919576395E+07, 0.147356242165350E+09, 0.455957054639189E-01, 0.132721216278857E+03, 0.116944610798197E+03, 0.112215042556733E+03, 0.100818303543359E+01
我借助上述网站制作表格的代码是:
\documentclass{article}
\usepackage{siunitx}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{csvsimple}
\usepackage{graphicx}
\sisetup{
round-mode=places,
uncertainty-mode = separate,
}
\begin{table}[]
\centering
\caption{}
\label{tab:my-table}
\resizebox{1.1\textwidth}{!}{%
\begin{tabular}{
c
l
c
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
}
\toprule
\textbf{index} & \multicolumn{1}{c}{\textbf{pdes}} & \textbf{\begin{tabular}[c]{@{}c@{}}epoch \\ (JDTDB)\end{tabular}} & \multicolumn{1}{c}{\textbf{\begin{tabular}[c]{@{}c@{}}tp\\ (JDTDB)\end{tabular}}} & \multicolumn{1}{c}{\textbf{a\_km}} & \multicolumn{1}{c}{\textbf{e}} & \multicolumn{1}{c}{\textbf{w\_deg}} & \multicolumn{1}{c}{\textbf{ta\_deg}} & \multicolumn{1}{c}{\textbf{ma\_deg}} & \multicolumn{1}{c}{\textbf{n\_deg/d}} \\ \midrule
1 & 459872 & 2459600.5 & 0.245955429393969E+07 & 0.150902180397680E+09 & 0.700936137135535E-01 & 0.444423490130398E+02 & 0.509894719093894E+02 & 0.449518800380460E+02 & 0.972856801473429E+00 \\
2 & 1991 VG & 2459600.5 & 0.245965301734296E+07 & 0.154448562453878E+09 & 0.524440272062934E-01 & 0.981969077945011E+02 & 0.305901193619353E+03 & 0.310657729272394E+03 & 0.939542405344313E+00 \\
3 & 2000 SG344 & 2459600.5 & 0.245967142235446E+07 & 0.146215475380197E+09 & 0.668906906224866E-01 & 0.107259306095888E+03 & 0.280186369328737E+03 & 0.287658863754547E+03 & 0.102000471918105E+01 \\
4 & 2003 YN107 & 2459600.5 & 0.245973765703815E+07 & 0.147913409318582E+09 & 0.138963447919681E-01 & 0.352070216744272E+03 & 0.221439015133514E+03 & 0.222501180674322E+03 & 0.100249189670507E+01 \\
5 & 2006 JY26 & 2459600.5 & 0.245950771201134E+07 & 0.151137260433216E+09 & 0.831296262363333E-01 & 0.317110762865456E+03 & 0.995403784006208E+02 & 0.900588991605521E+02 & 0.970587901094867E+00 \\
6 & 2006 QQ56 & 2459600.5 & 0.245948919576395E+07 & 0.147356242165350E+09 & 0.455957054639189E-01 & 0.132721216278857E+03 & 0.116944610798197E+03 & 0.112215042556733E+03 & 0.100818303543359E+01 \\ \bottomrule
\end{tabular}
}
\end{table}
\end{document}
我读了一些有关该csvsimple
包的问题,您能帮助我读取.csv
文件以创建具有 booktab 样式的专业表格而无需在文件中输入数据.tex
吗?
答案1
如果您的表格在垂直方向上太大,并且可能跨越多个页面,您可能需要使用其中一个长表格:longtable
,xltabular
等等。
csvsimple
下面是使用和的示例xltabular
。landscape
由于表格比较宽,我使用\newgeometry
来稍微增加表格所占页面的边距。我还添加了解决方案表单,makecell
以简化处理多行时的代码
\documentclass{article}
\usepackage[pass]{geometry}
\usepackage{pdflscape}
\usepackage{csvsimple}
\usepackage{xltabular}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{makecell}
\sisetup{
round-mode=figures,
round-precision=3,
}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{c}
\begin{document}
Some text here
\newgeometry{margin=3cm,a4paper} % Starts new page
\begin{landscape}
% \setlength\tabcolsep{3pt}
\begin{xltabular}{0.93\linewidth}{
@{} c X
S[table-format=7.1, round-mode=none]
*7{S[table-format=1.3e1]}
@{}
}
\caption{The table\label{tab:label}}\\[-6pt]
\toprule
{\thead{idx.}}
& {\thead[l]{pdes}}
& {\thead{epoch\\(JDTDB)}}
& {\thead{tp\\(JDTDB)}}
& {\thead{a\_km}}
& {\thead{e}}
& {\thead{w\_deg}}
& {\thead{ta\_deg}}
& {\thead{ma\_def}}
& {\thead{n\_deg/d}} \\
\midrule \endhead
\bottomrule \endfoot
\csvreader[
no head,
late after line = \\,
]{sample.csv}{}%
{\csvcoli & \csvcolii & \csvcoliii
& \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii & \csvcolviii
& \csvcolix & \csvcolx
}
\end{xltabular}
\end{landscape}
\restoregeometry % Adds pagebreak
\end{document}
编辑。〜代码生成垂直表〜
编辑2。
\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{caption}
\usepackage{makecell}
\sisetup{
round-mode=figures,
round-precision = 3,
exponent-product = {\hspace{-1pt}\times\hspace{-2pt}},
}
\captionsetup[table]{position=top, skip=3pt}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{c}
\begin{document}
Some text
\begin{table}[tbh]
\small
\renewcommand*{\arraystretch}{1.25}
\setlength\tabcolsep{4pt}
\caption{The table}
\label{tab:label}
\centering
\begin{tabular}{
@{} c l
S[table-format=7.1, round-mode=none]
*3{S[table-format=1.3e1]}
@{\hspace{12pt}}
S[table-format=1.3e1]
@{}
}
\toprule
{\thead{idx.}}
& {\thead[l]{pdes}}
& {\thead{epoch\\(JDTDB)}}
& {\thead{a\_km}}
& {\thead{e}}
& {\thead{w\_deg}}
& {\thead{ta\_deg}} \\
\midrule
\csvreader[
no head,
late after line = \\,
]{sample.csv}{}{%
\csvcoli & \csvcolii & \csvcoliii & \csvcoliv
& \csvcolv & \csvcolvi & \csvcolvii
}
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
我建议不要缩放表格,而是使用较小的字体并减少列分隔符的长度。除此之外,假设您的数据位于名为的文件中data.csv
,则以下内容应该有效:
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{csvsimple}
\sisetup{
round-mode=places,
uncertainty-mode = separate,
}
\newcolumntype{Z}{S[table-format=-1.3e1, round-precision=3, scientific-notation=true]}
\begin{document}
\begin{table}
\centering
\caption{}
\label{tab:my-table}{%
\scriptsize%
\setlength{\tabcolsep}{2pt}%
\begin{tabular}{clc*{7}{Z}}
\toprule
\textbf{index} & \multicolumn{1}{c}{\textbf{pdes}} & \textbf{\begin{tabular}{@{}c@{}}epoch \\ (JDTDB)\end{tabular}} & \multicolumn{1}{c}{\textbf{\begin{tabular}{@{}c@{}}tp\\ (JDTDB)\end{tabular}}} & \multicolumn{1}{c}{\textbf{a\_km}} & \multicolumn{1}{c}{\textbf{e}} & \multicolumn{1}{c}{\textbf{w\_deg}} & \multicolumn{1}{c}{\textbf{ta\_deg}} & \multicolumn{1}{c}{\textbf{ma\_deg}} & \multicolumn{1}{c}{\textbf{n\_deg/d}} \\ \midrule
\csvreader[
no head,
late after line = \\
]{data.csv}{}{%
\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii & \csvcolviii & \csvcolix & \csvcolx
}%
\bottomrule
\end{tabular}
}
\end{table}
\end{document}