我必须用 Latex 创建证书,证书的格式我已经掌握。在证书中,只有参与者的姓名会发生变化,其他一切都保持不变(包括格式、边框、颜色等)。
参与者名单以文本文件 (participant.txt) 的形式提供。此文件每行包含一名参与者。
如何使用 latex 自动生成证书。
我的路线图如下: - 我将创建一个 TEX 文件的副本;搜索包含参与者姓名的特定行,在此行中,我将用 new_participant 替换旧参与者(通过读取参与者.txt 文件)并编译代码。所有参与者都需要执行相同的操作。
如何继续实施?有任何帮助吗?
答案1
这是一个为所有参与者创建一个输出文件的解决方案。您不必为此使用任何脚本,只需运行一次 LaTeX 就足够了。如果您希望为每个参与者开始一个新页面,您可能需要在定义中插入\newpage
或。\clearpage
\printCertificate
\documentclass{article}
% I use these encodings in order to properly typeset “René Descartes”, which
% is in my participant.txt file. Use encodings appropriate for your situation,
% of course.
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xparse}
\ExplSyntaxOn
\msg_new:nnn { upsmakeparticlist } { file-not-found }
{ File~'\exp_not:n {#1}'~not~found. }
\cs_new_protected:Npn \ups_call_user_callback:Nn #1#2
{
% Call the user callback (#1) with contents obtained from the file (#2).
#1 {#2}
}
\cs_generate_variant:Nn \ups_call_user_callback:Nn { Nx }
\ior_new:N \g_ups_stream_ior
% #1: macro taking one argument (the participant name)
% #2: file containing one participant per line (blank lines are ignored)
\cs_new_protected:Npn \ups_list_participants:Nn #1#2
{
\ior_open:NnF \g_ups_stream_ior {#2}
{ \msg_error:nnn { upsmakeparticlist } { file-not-found } {#2} }
\ior_map_inline:Nn \g_ups_stream_ior
{
\tl_if_eq:nnF {##1} { \par } % ignore blank lines
{
% Calling \ups_call_user_callback:Nx will *not* trigger expansion
% of user-provided contents from ##1, so this should be safe to use
% (see the documentation of \tl_trim_spaces:n).
\ups_call_user_callback:Nx #1 { \tl_trim_spaces:n {##1} }
}
}
\ior_close:N \g_ups_stream_ior
}
% #1: macro taking one argument (the participant name)
% #2: file containing one participant per line (blank lines are ignored). Must
% be readable by TeX; for instance, it can live in the same directory as the
% .tex document or be somewhere in TEXINPUTS.
\NewDocumentCommand \participantlist { m m }
{
\ups_list_participants:Nn #1 {#2}
}
\ExplSyntaxOff
% Will be our callback function that is called for each participant
\newcommand*{\printCertificate}[1]{%
This is the certificate of #1, etc.\par
}
\begin{document}
\participantlist{\printCertificate}{participant.txt}
\end{document}
输入文件participant.txt
(可能在 TEXINPUTS 中;例如,可以与.tex
文件位于同一目录中):
Isaac Newton
Pierre de Fermat
Karl Friedrich Gauss
Henri Lebesgue
William Rowan Hamilton
Georg Ferdinand Ludwig Philipp Cantor
René Descartes
输出: