Datatool 和多个文件

Datatool 和多个文件

我正在尝试实现与 pgfplotstable 相同的目标这个问题我从 pgfplotstable 的作者 (Christian Feuersänger 博士) 本人那里得到了很好的帮助。因此,这个问题对我来说可以说是学术上感兴趣的。

我有两个 csv 文件:names.csv

Sl. No., Reg. No.,Name,Category,Group,Gate,Sponsored,Department
1,MS001,Ajay-D-Vimal Raj P,PY,OBC,--,No,Physics
2,MS002,Harish Kumar,PY,GE,GATE,Yes,Physics
3,MS003,Ajay-D-Vimal Raj P,PY,OBC,--,No,Physics
4,MS004,Harish Kumar,PY,GE,GATE,Yes,Physics

marks.csv

number,marks
MS001,67
MS002,25
MS003,62
MS004,55

要求:我想将第一个文件(names.csv)打印成一个长表,其中包括的第二列(即标记列)marks.csv作为名称列之后的列(即第四列)。

这是 MWE(可以工作,但不是我想要的)

\documentclass[10pt]{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{longtable}
\usepackage{datatool}
% \usepackage{filecontents}
%
% \begin{filecontents}{names.csv}
% serial,number,name,group,category,gate,sponsored,dept
%     1,MS001,Ajay-D-Vimal Raj P,PY,OBC,--,No,Physics
%     2,MS002,Harish Kumar,PY,GE,GATE,Yes,Physics
%     3,MS003,Ajay-D-Vimal Raj P,PY,OBC,--,No,Physics
%     4,MS004,Harish Kumar,PY,GE,GATE,Yes,Physics
% \end{filecontents}

% \begin{filecontents}{marks.csv}
%     number,marks
%     MS001,67
%     MS002,25
%     MS003,62
%     MS004,55
% \end{filecontents}
%
\begin{document}
\DTLloaddb{names}{names.csv}
\DTLloaddb{marks}{marks.csv}                       
% ----------------------------------------------------------------------%
% ----------------------------------------------------------------------%
{\small
\begin{longtable}{|c|l|p{3.5cm}|c|l|c|c|c|c|}\hline
% -----------------These are headings----------------------------------%
\textbf{} & \textbf{Reg. No.} &  \multicolumn{1}{c|}{\textbf{Name}} & \textbf{Marks}&
\multicolumn{1}{c|}{\textbf{Category}}  & \textbf{GATE} & \textbf{Sponsored} &\textbf{Department} \\ \hline
%
\endfirsthead
%
\multicolumn{8}{c}%
{{\bfseries  Continued from previous page}} \\
\hline
%
\textbf{} & \textbf{Reg. No.} & \multicolumn{1}{c|}{\textbf{Name}} & \textbf{Marks}&
\multicolumn{1}{c|}{\textbf{Category}}  & \textbf{GATE} & \textbf{Sponsored}&\textbf{Department} \\ \hline\hline
\endhead
%
\hline \multicolumn{8}{|r|}{{Continued on next page}} \\ \hline
\endfoot
%
\hline
\multicolumn{8}{|r|}{{Concluded}} \\ \hline
\endlastfoot
%-----------Headings end---------------------------------
%--------------------------table body starts-------------------
\DTLforeach{names}{%
\sl=serial, \reg=number,\name=name, \group=group, \category=category, \gate=gate,\sponsored=sponsored,\dept=dept}{%
\DTLiffirstrow{}{\\\hline}%  
% \DTLiflastrow{\dtlbreak}{}
% \DTLiffirstrow{\dtlbreak}{}
 \DTLforeach{marks}{%
 \marks=marks}{%
 \DTLiffirstrow{}{\\}%
%  \DTLiffirstrow{\dtlbreak}{}
% \DTLiflastrow{\dtlbreak}{}
\sl & \reg & \name & \marks & \group/\category & \gate & \sponsored & \dept
%
}%
}%
%--------------------------table body ends-------------------
\end{longtable}
}%
%
\end{document}

在此处输入图片描述

但不知何故,我无法正确循环,每个名字都重复了 4 次。

问题:

  1. 如何从marks.csv文件中获取相应的标记并打印出来。(获取\DTLforeach正确吗?)
  2. 如何按分数的升序对结果表进行排序,同时仅保持第一列的序列号不变。(所有其他列应按其相应的分数排序)?

如有任何进一步的澄清,我们将非常乐意提供帮助。

答案1

虽然LaTeX它不是作为 DBMS 设计的,因此用它进行连接确实很难看,但使用第二个匹配\DTLforeach您将获得所需的结果:

\DTLforeach[\DTLiseq{\subnum}{\reg}]{marks}{%
 \subnum=number, \marks=marks}{%

但我强烈建议使用专用 DBMS 来生成您喜欢的表,并且只在最后一步将其读入LaTeX。这样您就不需要任何这样的嵌套循环。

至于第二个问题datatool文档在第 5.8 节中告诉我们如何做到这一点:

\DTLsort{number}{names}

只需在加载之间名称db 并显示数据即可。对我来说,保持序列的原始顺序没有意义。但如果这是您需要的,我会在排序之前保存顺序,然后稍后读取。但如果您只想要连续的行编号,则可以使用计数器。使用以下代码定义它\newcounter{row}并使用以下代码打印每一行:

 \therow\refstepcounter{row}
 & \reg & \name & \marks & \group/\category & \gate & \sponsored & \dept

相关内容