使用 expl3 语法将文本附加到外部文件的每一行

使用 expl3 语法将文本附加到外部文件的每一行

假设我有一个外部 questionfileone.tex 文件,其中此外部文件的每一行代表一个特定的问题文本。例如,我可以将其用作我的工程专业学生的测试数据库。以下是该文件的一个说明性示例:

\begin{filecontents*}{questionfileone.tex}
1,"Engineering Question Text 1"
2,"Engineering Question Text 2"
3,"Engineering Question Text 3"
4,"Engineering Question Text 4"
5,"Engineering Question Text 5"
%and so on ..., hundreds, perhaps thousands of questions accumulated through the years ...
%and perhaps hundreds/thousands of external files containing thousands of questions per each external file
\end{filecontents*}

请注意,问题按时间顺序编号为 1、2、3 等,并且每个数字和该行的问题都用逗号分隔。

我希望将每个问题的答案放在同一行,也就是说,我希望我的文件看起来像这样:

\begin{filecontents*}{questionfileone.tex}
1,"Engineering Question Text 1","Answer Text"
2,"Engineering Question Text 2","Answer Text"
3,"Engineering Question Text 3","Answer Text"
4,"Engineering Question Text 4","Answer Text"
5,"Engineering Question Text 5","Answer Text"
\end{filecontents*}

请注意,我只是简单地为外部文件的每一行附加了字符串,"Answer Text"。如果外部文件中只有 5 行,则可以手动完成此操作 --- 复制粘贴系统。但是,如果有数千行,并且可能有数百/数千个外部文件(每个文件包含数千行),则可能值得以某种方式自动执行每行附加文本的过程。

请注意,我仍然需要更改Answer Text每一行,因为不同的问题会有不同的答案。但至少占位符已经存在了。

还请注意,这可以在 MS Excel 中使用内置的连接函数完成。但是,我在想是否可以使用 LaTeX 完成,特别是如果涉及大量文件时。

考虑以下 MWE:

%MWE
\documentclass{article}

%external file 1: questionfileone.tex
\begin{filecontents*}{questionfileone.tex}
1,"Engineering Question Text 1"
2,"Engineering Question Text 2"
3,"Engineering Question Text 3"
4,"Engineering Question Text 4"
5,"Engineering Question Text 5"
\end{filecontents*}

%external file 2: questionfiletwo.tex
\begin{filecontents*}{questionfiletwo.tex}
6,"Engineering Question Text 6"
7,"Engineering Question Text 7"
8,"Engineering Question Text 8"
9,"Engineering Question Text 9"
10,"Engineering Question Text 10"
\end{filecontents*}

%for declaration only
\newcommand*{\myAppendTextToEachLineOfExternalFile}[2]{#1#2}%
%#1 is the text to be appended to each line of the external files
%#2 are the list of external files

\begin{document}

\myAppendTextToEachLineOfExternalFile%
{,"Answer Text"}%
{%
questionfileone.tex,%
questionfiletwo.tex,%
}%
%This macro operation will append ``,"Answer Text"'' in each line of each of the external files questionfileone.tex and questionfiletwo.tex

\end{document}

那么,我的问题是:我们如何正确设计宏\myAppendTextToEachLineOfExternalFile{}以实现期望的结果?

如果可以的话,我们可以expl3在宏中使用语法吗?

我们可以将文本附加到外部文件的每一行吗除了最后一个空白行

请寻求您的帮助。

相关内容