如何通过从.csv 文件加载来设计专业表格?

如何通过从.csv 文件加载来设计专业表格?

我发现了一个类似的问题(有用的问题) 我按照它来构建我的表格,但我无法获得预期的结果,即下表,但带有 booktabs 样式(我无法在用于构建表格的编辑器中重现): 在此处输入图片描述

这是我的.csv文件(Results_test.csv):

L-point,Traj. no.,Candidate NEA (id.no. - Pdes),DVmin-TOF(km/s-day),TOFmin-DV(day-km/s),Dmin-E(km-deg),Emin-D(deg-km)
1,940,4,31,2018 PN22,5,0.495786955408242,321.691007914094,321.691007914094,0.495786955408242,1304.55816893965,0.00766274786981772,0.00766274786981772,1304.55816893965
1,940,4,40,2020 HO5,618,0.296499238470772,563.204937799172,460.61389541864,0.434673022794247,44.1321628087056,0.0043440927667307,1.06333561065898e-06,13334.9589039407
1,940,4,47,2021 GM1,207,0.321225083304834,579.001176279967,458.977213170648,0.494635569509203,272.790892644318,0.000205574640657646,8.75927739836456e-05,16162.8584126721
1,940,4,48,2021 LF6,110,0.331415081004236,547.555702810118,426.019758493117,0.494810189855507,1635.52986610854,0.00604210949390045,0.000659773039856017,15282.6573179668
2,3285,9,36,2020 CD3,188,0.441087426101912,598.687300827277,560.12998365025,0.498671715102102,226.148958936483,0.00124800816293013,4.00652387797866e-05,720.645461262528
2,3285,9,37,2020 FA1,150,0.363833486907914,617.815224738438,532.434130120624,0.477043833738971,542.710633669257,0.00217371251138835,0.000151649146744098,4570.03421246898
2,3285,9,39,2020 HF4,277,0.325869742616378,620.47407865956,495.241518020061,0.490112884848913,316.676948047764,0.00396990356158546,3.56238695777137e-05,9221.76907418505
2,3285,9,41,2020 MU1,30,0.462893611205412,597.439526870574,543.014823292621,0.496958058852358,1772.22939016773,0.00367089047771314,0.000488508198589889,13825.1797358252
2,3285,9,44,2020 WY,373,0.258567622278901,592.325909750424,457.543641043238,0.486364970362706,12.7440192334999,0.000402576106298293,5.77400555816601e-05,4916.39967823924
2,3285,9,45,2021 AK5,823,0.150275430032146,690.288217497001,553.108214337696,0.490073643361243,168.651739286611,0.000968271504982416,1.7294477167511e-05,2102.12134797594
2,3285,9,49,2021 RZ3,106,0.422216662637048,503.142619476216,412.861217455181,0.497744537876508,1010.29185702609,0.00513171449630041,3.17739132722177e-05,15816.5130302721
2,3285,9,50,2021 RG12,1161,0.0600534121618513,469.018823203997,310.079915873532,0.48383013103322,24.9984223871349,0.000221411870262754,3.9138194517227e-06,1211.60680842878
2,3285,9,52,2021 VX22,177,0.428923740467692,370.203538028964,346.732351012621,0.496774800147814,155.225383516442,9.50215401351551e-05,9.50215401351551e-05,155.225383516442

这是我的代码尝试:

\documentclass{article}
\usepackage{siunitx}
\usepackage{array}
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{caption}

\begin{document}

\begin{table}[ht!]
        \caption{test}
        % \centering\tiny\renewcommand*{\arraystretch}{1.4} 
        \csvloop{file=Chapter_6/tables/Results_test.csv, no head, 
            before reading=\centering%\sisetup{table-number-alignment=center}
            ,
            tabular={c@{}*8{S[table-format=1.4]}S[table-format=2.4]},
            table head=\toprule & \textbf{L-point} & \textbf{Tot. no. sol.} & \textbf{Accesible NEA} & \multicolumn{2}{c}{\textbf{NEA name}} & \textbf{No. sol.} & \multicolumn{2}{c}{\textbf{DeltaV}} & \multicolumn{2}{c}{\textbf{TOF}} & \multicolumn{2}{c}{\textbf{dist}} & \multicolumn{2}{c}{\textbf{err om}}\\
            \cmidrule(lr){1-4} \cmidrule(lr){5-7} \cmidrule(lr){8-10}, command=&\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii & \csvcolviii & \csvcolix & \csvcolxi & \csvcolxii & \csvcolxiii & \csvcolxiv, table foot=\bottomrule}
    \end{table}

\end{document}

我得到的结果很糟糕,此外我需要将表格旋转 90 度才能使其适合页面: 草稿结果

你能帮助我得到想要的结果吗?

编辑:我设法得到了所需的表格,但我需要进行一些调整,即更好地垂直对齐第二列Candidate NEA (id. – Pdes),以及更好地垂直对齐连续列相对于\cmidrule行。这是我的新代码:

\begin{table}[]
    \centering
    \caption{}
    \label{tab:my-table}
    \resizebox{\textwidth}{!}{%
    \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
    \begin{tabular}{
    c %col1 L-point
    S[table-format=4.0] %col2 No. of. sol.
    S[table-format=0.5,input-decimal-markers=-,output-decimal-marker=-] %col3 Cand. NEA
    S[table-format=1.3,round-mode=places,round-precision=3] %col4
    S[table-format=3.0,round-mode=figures,round-precision=3] %col5
    S[table-format=3.0,round-mode=figures,round-precision=3] %col6
    S[table-format=1.3,round-mode=figures,round-precision=3] %col7
    S[table-format=4.0,round-mode=places,round-precision=0] %col8
    S[table-format=1.2e1,scientific-notation = true,round-mode=places,round-precision=2] %col9
    S[table-format=1.2e1,scientific-notation = true,round-mode=places,round-precision=2] %col10
    S[table-format=6.0,round-mode=places,round-precision=0] %col11
   }
    \toprule
    \textbf{L} & \multicolumn{1}{M{1cm}}{\textbf{No.of\newline sol.}} & \multicolumn{1}{M{3cm}}{\textbf{Candidate NEA  (id. \mbox{--} Pdes)}} & \multicolumn{2}{M{2.5cm}}{\textbf{$\boldsymbol{\Delta V_{min}\mbox{--}TOF}$ (km/s \mbox{--} day)}} & \multicolumn{2}{M{2.5cm}}{\textbf{$\boldsymbol{TOF_{min}-\Delta V}$ (day \mbox{--} km/s)}} & \multicolumn{2}{M{3cm}}{\textbf{$\boldsymbol{d_{min}\mbox{--}E}$\newline (km \mbox{--} deg)}} & \multicolumn{2}{M{3cm}}{\textbf{$\boldsymbol{E_{min} \mbox{--} d}$\newline (deg\mbox{--}km)}} \\ 
    \cmidrule(){1-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}\cmidrule(l){10-11}
     & 5 & 31 - 2018 PN22 & 0.495786955408242 & 321.691007914094 & 321.691007914094 & 0.495786955408242 & 1304.55816893965 & 0.00766274786981772 & 0.00766274786981772 & 1304.55816893965 \\
     & 618 & 40 - 2020 HO5 & 0.296499238470772 & 563.204937799172 & 460.61389541864 & 0.434673022794247 & 44.1321628087056 & 0.0043440927667307 & 1.06333561065898e-06 & 13334.9589039407 \\
     & 207 & 47 - 2021 GM1 & 0.321225083304834 & 579.001176279967 & 458.977213170648 & 0.494635569509203 & 272.790892644318 & 0.000205574640657646 & 8.75927739836456e-05 & 16162.8584126721 \\
    \multirow{-4}{*}{\num{1}} & 110 & 48 - 2021 LF6 & 0.331415081004236 & 547.555702810118 & 426.019758493117 & 0.494810189855507 & 1635.52986610854 & 0.00604210949390045 & 0.000659773039856017 &     15282.6573179668 \\ \midrule
     & 188 & 36 - 2020 CD3 & 0.441087426101912 & 598.687300827277 & 560.12998365025 & 0.498671715102102 & 226.148958936483 & 0.00124800816293013 & 4.00652387797866e-05 & 720.645461262528 \\
     & 150 & 37 - 2020 FA1 & 0.363833486907914 & 617.815224738438 & 532.434130120624 & 0.477043833738971 & 542.710633669257 & 0.00217371251138835 & 0.000151649146744098 & 4570.03421246898 \\
     & 277 & 39 - 2020 HF4 & 0.325869742616378 & 620.47407865956 & 495.241518020061 & 0.490112884848913 & 316.676948047764 & 0.00396990356158546 & 3.56238695777137e-05 & 9221.76907418505 \\
     & 30 & 41 - 2020 MU1 & 0.462893611205412 & 597.439526870574 & 543.014823292621 & 0.496958058852358 & 1772.22939016773 & 0.00367089047771314 & 0.000488508198589889 & 13825.1797358252 \\
     & 373 & 44 - 2020 WY & 0.258567622278901 & 592.325909750424 & 457.543641043238 & 0.486364970362706 & 12.7440192334999 & 0.000402576106298293 & 5.77400555816601e-05 & 4916.39967823924 \\
     & 823 & 45 - 2021 AK5 & 0.150275430032146 & 690.288217497001 & 553.108214337696 & 0.490073643361243 & 168.651739286611 & 0.000968271504982416 & 1.7294477167511e-05 & 2102.12134797594 \\
     & 106 & 49 - 2021 RZ3 & 0.422216662637048 & 503.142619476216 & 412.861217455181 & 0.497744537876508 & 1010.29185702609 & 0.00513171449630041 & 3.17739132722177e-05 & 15816.5130302721 \\
     & 1161 & 50 - 2021 RG12 & 0.0600534121618513 & 469.018823203997 & 310.079915873532 & 0.48383013103322 & 24.9984223871349 & 0.000221411870262754 & 3.9138194517227e-06 & 1211.60680842878 \\
    \multirow{-9}{*}{\num{2}} & 177 & 52 - 2021 VX22 & 0.428923740467692 & 370.203538028964 & 346.732351012621 & 0.496774800147814 & 155.225383516442 & 9.50215401351551e-05 & 9.50215401351551e-05 &     155.225383516442 \\ \bottomrule
    \end{tabular}%
    }
    \end{table}

结果如下图所示,我用红色箭头标出了我需要的调整,即根据箭头所指移动的列或文本。 表格有待完善

答案1

我个人的建议是使用已有的工具,而不是自己创建。例如https://www.tablesgenerator.com/

首先,您的 .csv 完全不同于您想要实现的格式。您有一个候选列分布在其他三行中,因此使用 Excel 连接可以轻松修复该问题。然后,只需通过调整位数或选择科学格式,按列指定您想要的格式类型即可。

因此正确的.csv应该是这样的:

 L-point,Traj. no.,Candidate NEA (id.no. - Pdes),DVmin-TOF(km/s-day),,TOFmin-DV (day-km/s),,Dmin-E (km-deg),,Emin-D(deg-km),
1,5,31 - 2018 PN22 5,0.496,322,322,0.496,1305,7.66E-03,7.66E-03,1305
1,618,40 - 2020 HO5 618,0.296,563,461,0.435,44,4.34E-03,1.06E-06,13335
1,207,47 - 2021 GM1 207,0.321,579,459,0.495,273,2.06E-04,8.76E-05,16163
1,110,48 - 2021 LF6 110,0.331,548,426,0.495,1636,6.04E-03,6.60E-04,15283
2,188,36 - 2020 CD3 188,0.441,599,560,0.499,226,1.25E-03,4.01E-05,721
2,150,37 - 2020 FA1 150,0.364,618,532,0.477,543,2.17E-03,1.52E-04,4570
2,277,39 - 2020 HF4 277,0.326,620,495,0.490,317,3.97E-03,3.56E-05,9222
2,30,41 - 2020 MU1 30,0.463,597,543,0.497,1772,3.67E-03,4.89E-04,13825
2,373,44 - 2020 WY 373,0.259,592,458,0.486,13,4.03E-04,5.77E-05,4916
2,823,45 - 2021 AK5 823,0.150,690,553,0.490,169,9.68E-04,1.73E-05,2102
2,106,49 - 2021 RZ3 106,0.422,503,413,0.498,1010,5.13E-03,3.18E-05,15817
2,1161,50 - 2021 RG12 1161,0.060,469,310,0.484,25,2.21E-04,3.91E-06,1212
2,177,52 - 2021 VX22 177,0.429,370,347,0.497,155,9.50E-05,9.50E-05,155

下一步是简单地复制粘贴(从 EXCEL 而不是 .csv)到表格生成器网站。合并您想要的列和行。选择您想要不同颜色文本的单元格,最后在下拉菜单中选择 Booktabs 表格样式。您甚至可以直接指定要绘制哪些边框,它会自动检测应该在哪里使用\toprule , \midrule , \cmidrule等等。

站点生成按钮会生成代码,然后您只需进行任何微小的调整即可。

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}

\author{michel.gordillo }


\begin{document}

\begin{table}[]
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{ccccccccccc}
\toprule
L-point & Traj. no. & Candidate NEA (id.no. - Pdes) & \multicolumn{2}{c}{DVmin-TOF(km/s-day)} & \multicolumn{2}{c}{TOFmin-DV (day-km/s)} & \multicolumn{2}{c}{Dmin-E (km-deg)} & \multicolumn{2}{c}{Emin-D(deg-km)} \\ 
\cmidrule(r){1-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}\cmidrule(l){10-11}
 & 5 & 31 - 2018 PN22 5 & 0.496 & 322 & 322 & 0.496 & 1305 & 7.66E-03 & 7.66E-03 & 1305 \\
 & 618 & 40 - 2020 HO5 618 & 0.296 & 563 & 461 & 0.435 & 44 & 4.34E-03 & {\color[HTML]{FE0000} 1.06E-06} & 13335 \\
 & 207 & 47 - 2021 GM1 207 & 0.321 & 579 & 459 & 0.495 & 273 & 2.06E-04 & 8.76E-05 & 16163 \\
\multirow{-4}{*}{1} & 110 & 48 - 2021 LF6 110 & 0.331 & 548 & 426 & 0.495 & 1636 & 6.04E-03 & 6.60E-04 & 15283 \\ \midrule
 & 188 & 36 - 2020 CD3 188 & 0.441 & 599 & 560 & 0.499 & 226 & 1.25E-03 & 4.01E-05 & 721 \\
 & 150 & 37 - 2020 FA1 150 & 0.364 & 618 & 532 & 0.477 & 543 & 2.17E-03 & 1.52E-04 & 4570 \\
 & 277 & 39 - 2020 HF4 277 & 0.326 & 620 & 495 & 0.490 & 317 & 3.97E-03 & 3.56E-05 & 9222 \\
 & 30 & 41 - 2020 MU1 30 & 0.463 & 597 & 543 & 0.497 & 1772 & 3.67E-03 & 4.89E-04 & 13825 \\
 & 373 & 44 - 2020 WY 373 & 0.259 & 592 & 458 & 0.486 & {\color[HTML]{FE0000} 13} & 4.03E-04 & 5.77E-05 & 4916 \\
 & 823 & 45 - 2021 AK5 823 & 0.150 & 690 & 553 & 0.490 & 169 & 9.68E-04 & 1.73E-05 & 2102 \\
 & 106 & 49 - 2021 RZ3 106 & 0.422 & 503 & 413 & 0.498 & 1010 & 5.13E-03 & 3.18E-05 & 15817 \\
 & 1161 & 50 - 2021 RG12 1161 & {\color[HTML]{FE0000} 0.060} & 469 & {\color[HTML]{FE0000} 310} & 0.484 & 25 & 2.21E-04 & 3.91E-06 & 1212 \\
\multirow{-9}{*}{2} & 177 & 52 - 2021 VX22 177 & 0.429 & 370 & 347 & 0.497 & 155 & 9.50E-05 & 9.50E-05 & 155\\ \bottomrule
\end{tabular}%
}
\caption{}
\label{tab:my-table}
\end{table}


\end{document}

结果表:

书签表-多行-多列-xcolor

以这种方式生成代码要好得多,因为这样更容易修改细节。因为表格的格式通常非常不通用。例如,您可能希望将标题更改为数学模式,我假设您已经知道如何操作。如果您需要减小列宽以便将标题分成两行,我推荐使用数组包:它有以下命令:

  • p{'width'}文本在顶部垂直对齐的段落列

  • m{'width'} 段落列,文本垂直对齐于中间(需要数组包)

  • b{'width'}段落列的文本垂直对齐于底部(需要数组包)来源

最后是该表的代码:

%remember to include the array package    
\begin{table}[]
    \centering
    \resizebox{\textwidth}{!}{%
    \begin{tabular}{ccccccccccc}
    \toprule
    L-point & Traj. no. & \multicolumn{1}{m{3cm}}{Candidate NEA (id.no. - Pdes)} & \multicolumn{2}{m{3cm}}{DVmin-TOF (km/s-day)} & \multicolumn{2}{m{2.5cm}}{TOFmin-DV (day-km/s)} & \multicolumn{2}{m{3cm}}{Dmin-E (km-deg)} & \multicolumn{2}{c}{Emin-D (deg-km)} \\ 
    \cmidrule(r){1-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}\cmidrule(l){10-11}
     & 5 & 31 - 2018 PN22 5 & 0.496 & 322 & 322 & 0.496 & 1305 & 7.66E-03 & 7.66E-03 & 1305 \\
     & 618 & 40 - 2020 HO5 618 & 0.296 & 563 & 461 & 0.435 & 44 & 4.34E-03 & {\color[HTML]{FE0000} 1.06E-06} & 13335 \\
     & 207 & 47 - 2021 GM1 207 & 0.321 & 579 & 459 & 0.495 & 273 & 2.06E-04 & 8.76E-05 & 16163 \\
    \multirow{-4}{*}{1} & 110 & 48 - 2021 LF6 110 & 0.331 & 548 & 426 & 0.495 & 1636 & 6.04E-03 & 6.60E-04 & 15283 \\ \midrule
     & 188 & 36 - 2020 CD3 188 & 0.441 & 599 & 560 & 0.499 & 226 & 1.25E-03 & 4.01E-05 & 721 \\
     & 150 & 37 - 2020 FA1 150 & 0.364 & 618 & 532 & 0.477 & 543 & 2.17E-03 & 1.52E-04 & 4570 \\
     & 277 & 39 - 2020 HF4 277 & 0.326 & 620 & 495 & 0.490 & 317 & 3.97E-03 & 3.56E-05 & 9222 \\
     & 30 & 41 - 2020 MU1 30 & 0.463 & 597 & 543 & 0.497 & 1772 & 3.67E-03 & 4.89E-04 & 13825 \\
     & 373 & 44 - 2020 WY 373 & 0.259 & 592 & 458 & 0.486 & {\color[HTML]{FE0000} 13} & 4.03E-04 & 5.77E-05 & 4916 \\
     & 823 & 45 - 2021 AK5 823 & 0.150 & 690 & 553 & 0.490 & 169 & 9.68E-04 & 1.73E-05 & 2102 \\
     & 106 & 49 - 2021 RZ3 106 & 0.422 & 503 & 413 & 0.498 & 1010 & 5.13E-03 & 3.18E-05 & 15817 \\
     & 1161 & 50 - 2021 RG12 1161 & {\color[HTML]{FE0000} 0.060} & 469 & {\color[HTML]{FE0000} 310} & 0.484 & 25 & 2.21E-04 & 3.91E-06 & 1212 \\
    \multirow{-9}{*}{2} & 177 & 52 - 2021 VX22 177 & 0.429 & 370 & 347 & 0.497 & 155 & 9.50E-05 & 9.50E-05 & 155\\ \bottomrule
    \end{tabular}%
    }
    \caption{}
    \label{tab:my-table}
    \end{table}

Booktables 加阵列包

相关内容