![使用数据工具从表中获取特定项目并打印](https://linux22.com/image/262845/%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%B7%A5%E5%85%B7%E4%BB%8E%E8%A1%A8%E4%B8%AD%E8%8E%B7%E5%8F%96%E7%89%B9%E5%AE%9A%E9%A1%B9%E7%9B%AE%E5%B9%B6%E6%89%93%E5%8D%B0.png)
我正在使用 datatool 加载一个 csv 表。学生姓名在该表中(第一列是名字,第二列是姓氏),我想在试卷上打印这些姓名。在一个相关问题中,我问如何在每次考试中打印学生证号码。我喜欢以类似的方式打印每次考试中从表格中获取的姓名。我的第一种方法(我只想打印任意值)看起来像这样,但不幸的是不起作用:
\documentclass[
paper=A4,
fontsize=10pt
]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{multido}
\usepackage{datatool}
\pagestyle{empty}
\newcounter{studentidcounter}
\setcounter{studentidcounter}{1}
\newcommand{\studentid}{%
\ifnum\thestudentidcounter<10\relax%
0\thestudentidcounter\else%
\thestudentidcounter\fi%
}
\DTLsetdelimiter{,}
\DTLloaddb[noheader=true]{members}{members.csv}
\DTLgetvalue{\studentsurname}{members}{1}{2}
\DTLgetvalue{\studentlastname}{members}{1}{1}
\newcommand{\studentname}[2]{#1 #2}
\begin{document}
\show\studentsurname
\multido{\nx = 1+1}{3}{
\input{examMain.tex}
\clearpage
%----------------------------------------------------------------------
% Uncomment next lines if exam has an odd number of pages.
% \newpage
% \mbox{}
% \newpage
%----------------------------------------------------------------------
\stepcounter{studentidcounter}%
\setcounter{equation}{0}
}
\end{document}
那么我如何才能实现在 multido 中迭代表格?我需要某种可以在 examMain.tex 中使用的宏来访问学生的全名?
members.cvs 的一个可能示例如下
Name1, FirstName1
Name2, FirstName2
Name3, FirstName3
答案1
使用\DTLforeach
:
\begin{filecontents*}{students.csv}
firstname,lastname
FirstName1,LastName1
FirstName2,LastName2
FirstName3,LastName3
\end{filecontents*}
\begin{filecontents*}{\jobname-exam.tex}
\noindent Student ID = \fbox{\studentid}\quad
\studentfirstname\ \studentlastname
\bigskip
Solve the equation $x-1=0$.
\end{filecontents*}
\documentclass{article}
\usepackage{datatool}
\pagestyle{empty}
\newcounter{studentidcounter}
\newcommand{\studentid}{%
\ifnum\thestudentidcounter<10 0\fi
\thestudentidcounter}
\DTLsetdelimiter{,}
\DTLloaddb{students}{students.csv}
\begin{document}
\DTLforeach{students}
{\studentfirstname=firstname,\studentlastname=lastname}
{\stepcounter{studentidcounter}
\input{\jobname-exam.tex}
%\clearpage
\par\bigskip\hrule\bigskip % just not to produce many pages
}
\end{document}