如何在使用 datatool 上传的 csv 表中使用 \hlines 和 | 列分隔符

如何在使用 datatool 上传的 csv 表中使用 \hlines 和 | 列分隔符

嗨,我一直在尝试使用该datatool包轻松地将 csv 文件中的数据以表格格式导入文档。我似乎总是发现第一列的行向下移动了一行。

如果我重写代码以模仿 datatool 手动\DTLforeach命令,我将无法添加水平线。我应该怎么做?我是否遗漏了一些非常明显的东西?

补充问题:如果我想使用\DTLdecimaltolocale{item}{cmd}将千位分隔符添加到此数据库的项目列表中,我该怎么做?谢谢

两个表格显示数据工具未对齐的 csv

\documentclass{article}
\usepackage{array,datatool}

\begin{filecontents}{sample.csv}
account, opening balance, expected, revised, amends
fruit, 1000000,1500000,1750000,250000
berries, 400000,500000, 450000,-50000
nuts, 300000,375000,390000,15000
legumes, 1250000, 1000000, 950000, 50000
cereals, 800000, 80000, 90000, 10000
\end{filecontents}

\begin{document}
\begin{table}[c]
\DTLloaddb[keys={ac,ob,ep,rv,df}]{dtl1}{sample.csv}
\caption{A Table to show misaligned borders}

\begin{tabular}{|p{4cm}|>{\raggedleft}p{2cm}|>{\raggedleft}p{2cm}|
>{\raggedleft}p{2cm}|>{\raggedleft}p{2cm}|}
\hline 
 & \bfseries 2010 Actual (a) & \textbf{2011 Target} & \textbf{2011
   Updated Plan (b)} &
\textbf{Difference (a) - (b)} \tabularnewline \hline \hline
\DTLforeach{dtl1}{\what=ac,\current=ob,\budget=ep,\plan=rv,\diff=df}{%
\what & \current & \budget & \plan & \diff \tabularnewline}
\end{tabular}
\end{table}

\begin{table}
\caption{A Table to show misaligned borders working}
\begin{tabular}{|p{4cm}|>{\raggedleft}p{2cm}|>{\raggedleft}p{2cm}|
>{\raggedleft}p{2cm}|>{\raggedleft}p{2cm}|}
\hline
\bfseries account&\bfseries opening balance&\bfseries
expected&\bfseries revised&\bfseries amends\tabularnewline \hline \hline
fruit& 1000000&1500000&1750000&250000\tabularnewline
berries& 400000&500000& 450000&-50000\tabularnewline
nuts& 300000&375000&390000&15000\tabularnewline
legumes& 1250000& 1000000& 950000& 50000\tabularnewline
cereals& 800000& 80000& 90000& 10000\tabularnewline \hline
\end{tabular}
\end{table}


\end{document}

答案1

虽然不是你问题的答案,但我也建议pgfplotstable对于此类构造。然后,您可以使用如下所示的键值系统非常轻松地自定义输出。

\documentclass{article}
\usepackage{array,booktabs}
\usepackage{pgfplotstable}

\begin{filecontents}{sample.csv}
account, opening balance, expected, revised, amends
fruit,1000000,1500000,1750000,250000
berries,400000,500000, 450000,-50000
nuts,300000,375000,390000,15000
legumes,1250000,1000000, 950000,50000
cereals,800000,80000, 90000, 10000
\end{filecontents}

\pgfplotstableread[col sep=comma]{sample.csv}\mydata
\newcolumntype{d}{>{\raggedleft}b{2cm}}
\newcolumntype{e}{>{\raggedleft\arraybackslash}b{2cm}}
\begin{document}


\begin{table}
\caption{Fiscal Table of Breakfast Costs}
\pgfplotstabletypeset[
every head row/.style={before row={\toprule\hline},after row={\hline}},
every last row/.style={after row=\hline\bottomrule},
columns/account/.style         ={string type,column type={l},column name=Account},
columns/opening balance/.style ={column type={d},column name=Opening Balance},
columns/expected/.style        ={column type={d},column name=2011 Expected Target},
columns/revised/.style         ={column type={d},column name=2011 Updated Plan},
columns/amends/.style          ={column type={e},column name={Difference \textbf{(a) - (b)}}},
/pgf/number format/.cd,int detect,set thousands separator={,},
]\mydata
\label{tab:breakfast}
\end{table}
\end{document}

在此处输入图片描述

答案2

为了完整性,我添加了一个使用的解决方案datatool。虽然我使用了tikz很多,但对我来说它在视觉上更合乎逻辑,所以pgfplotstable@percusse 提出的 tikz/ 解决方案显然更灵活。我注意到很多关于格式和数据的数据工具问题,但没有格式答案。万一这有用的话……

\documentclass{article}
\usepackage{array,booktabs,datatool}

\begin{filecontents}{sample.csv}
account, opening balance, expected, revised, amends
fruit, 1000000,1500000,1750000,250000
berries, 400000,500000, 450000,-50000
nuts, 300000,375000,390000,15000
legumes, 1250000, 1000000, 950000, 50000
cereals, 800000, 80000, 90000, 10000
\end{filecontents}

\newcommand{\seps}[1]{\DTLdecimaltolocale{#1}{\tmp1}\tmp1}
\newcolumntype{d}{>{\raggedleft}b{2cm}}
\newcolumntype{e}{>{\raggedleft\arraybackslash}b{2cm}}
\DTLloaddb[keys={ac,ob,ep,rv,df}]{dtl1}{sample.csv}

\begin{document}
\begin{table}

\caption{A Table to show misaligned borders}
\begin{tabular}{|l|d|d|d|e|}
\toprule \hline 
 & \bfseries 2010 Actual (a) & \textbf{2011 Target} & \textbf{2011
   Updated Plan (b)} &
\textbf{Difference (a) - (b)}%
\DTLforeach{dtl1}{\what=ac,\current=ob,\budget=ep,\plan=rv,\diff=df}{%
\ifthenelse{\value{DTLrowi}=1}{\tabularnewline \hline}{\tabularnewline}
\what & \seps{\current} & \seps{\budget} & \seps{\plan} & \seps{\diff}}\\
\hline \bottomrule

\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

相关内容