作为对几个问题的跟进,我提出了以下 MWE,目的是用arrayjobx
从 CSV 文件加载的值填充数组(通过)。
期望的输出是打印如下内容:
---------------------------------------
| | John Doe (084754) |
| Students: | Carl Sagan (458736) |
| | Peanut Butter (432756) |
---------------------------------------
CSV:
Campo,Valor,Aux
alumno,"John Doe","084754"
alumno,"Carl Sagan","458736"
alumno,"Peanut Butter","432576"
该LaTeX
文件:
\documentclass{minimal}
\usepackage{arrayjobx,datatool,multirow,forloop}
\newcounter{numst}
% Offending line vvv
\newcommand{\addstudent}[2]{\expandafter\addstudentaux\expandafter{#1}\expandafter{#2}}
\newcommand{\addstudentaux}[2]{%
\stepcounter{numst}%
\ifthenelse{\equal{\value{numst}}{1}}{\newarray\Students\newarray\Boletas}{}
\Students(\arabic{numst})={#1}%
\Boletas(\arabic{numst})={#2}%
}
\newcommand{\IsEmptyElement}[2]{\ifthenelse{\boolean{emptydata}}{#1}{#2}}
\AtEndDocument{\ifthenelse{\value{numst} > 0}{\delarray\Students\delarray\Boletas}{}}
\begin{document}
\DTLloaddb{Datos}{datos.csv}
\DTLforeach{Datos}{\campo=Campo,\valor=Valor,\valorb=Aux}{%
\ifthenelse{\equal{\campo}{alumno}}{%
\addstudent{\valor}{\valorb}
}{}
}
\begin{tabular}{cc}
\ifthenelse{\value{numst} > 0}{%
\ifthenelse{\value{numst} = 1}{%
\textbf{Student:} & \Students(1) \\
\textbf{ID:} & \Boletas(1) \\
}{%
\multirow{\value{numst}}{*}{\textbf{Students:}}%
\newcounter{st}%
\forloop{st}{1}{\not{\value{st} > \value{numst}}}{%
\checkBoletas(\arabic{st})%
& \Students(\arabic{st}) \IsEmptyElement{}{(\Boletas(\arabic{st}))} \\%
}%
}
}{}
\end{tabular}
\end{document}
输出:
我如何才能正确检索这些值? \expandafter
仅有助于成功检索最多一个值。提前致谢!
编辑:我这样做是因为表格末尾会附加其他数据。
答案1
您使用扩展将错误的项目传递给了\addstudentaux
。相反,我使用了一个\begingroup\edef\x{\endgroup <stuff>}\x
技巧来扩展 中的内容<stuff>
:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{datos.csv}
Campo,Valor,Aux
alumno,"John Doe","084754"
alumno,"Carl Sagan","458736"
alumno,"Peanut Butter","432576"
\end{filecontents*}
\usepackage{arrayjobx,datatool,multirow,forloop}
\newcounter{numst}
\newcommand{\addstudent}[2]{%
\stepcounter{numst}%
\ifthenelse{\equal{\value{numst}}{1}}{\newarray\Students\newarray\Boletas}{}
\Students(\arabic{numst})={#1}%
\Boletas(\arabic{numst})={#2}%
}
\newcommand{\IsEmptyElement}[2]{\ifthenelse{\boolean{emptydata}}{#1}{#2}}
\AtEndDocument{\ifthenelse{\value{numst} > 0}{\delarray\Students\delarray\Boletas}{}}
\begin{document}
\DTLloaddb{Datos}{datos.csv}
\DTLforeach{Datos}{\campo=Campo,\valor=Valor,\valorb=Aux}{%
\ifthenelse{\equal{\campo}{alumno}}{%
\begingroup\edef\x{\endgroup\noexpand\addstudent{\valor}{\valorb}}\x
}{}
}
\begin{tabular}{cc}
\ifthenelse{\value{numst} > 0}{%
\ifthenelse{\value{numst} = 1}{%
\textbf{Student:} & \Students(1) \\
\textbf{ID:} & \Boletas(1) \\
}{%
\multirow{\value{numst}}{*}{\textbf{Students:}}%
\newcounter{st}%
\forloop{st}{1}{\not{\value{st} > \value{numst}}}{%
\checkBoletas(\arabic{st})%
& \Students(\arabic{st}) \IsEmptyElement{}{(\Boletas(\arabic{st}))} \\%
}%
}
}{}
\end{tabular}
\end{document}
如果没有全局观,那么在代码建议和/或改进方面就无能为力。
这只是一个简单的实现,无需使用即可实现相同的输出arrayjobx
:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{datos.csv}
Campo,Valor,Aux
alumno,"John Doe","084754"
alumno,"Carl Sagan","458736"
alumno,"Peanut Butter","432576"
\end{filecontents*}
\usepackage{datatool}
\begin{document}
\DTLloaddb{Datos}{datos.csv}
\begin{tabular}{cc}
\textbf{Students:} &
\begin{tabular}{@{}c@{}}
\DTLforeach{Datos}{\campo=Campo,\valor=Valor,\valorb=Aux}{%
\ifthenelse{\equal{\campo}{alumno}}{%
\valor~(\valorb) \\
}{}
}
\\[-\normalbaselineskip]
\end{tabular}
\end{tabular}
\end{document}