将参数传递给 \input{text} (包含的文本文件)

将参数传递给 \input{text} (包含的文本文件)

我正在写一堆求职信,其中大约 90% 是样板,10% 是定制。但是,样板包含每次都需要更改的变量。与函数类似,我想将参数传递给样板。与先前有关自动化的帖子试图将参数传递给文档结构,而我的目标是将参数传递给文档内容。(我的工作假设是这是一个不同的问题。)

为了修正想法,以下是我的模板求职信的 MWE:

\documentclass[letterpaper,10pt]{article}
\begin{document}

Dear So and So, 

\input{boilerplate}

\end{document}

boilerplate.tex带有变量的文本在哪里:

我很高兴申请 [地点] 的 [职位] 职位。贵组织以严谨、高质量的主题编程而闻名,这与我的抱负完全一致。我很乐意有机会与顶级程序员合作,共同关注 [兴趣]。我致力于在编程方面追求卓越,并支持芝加哥小熊队。

其思想是将参数传递给[POSITION][PLACE][INTERESTS]

是否可以传递参数input{},或者只能在命令行上完成?

答案1

使用该listofitems包,这里有一个自制的键值接口,可以处理\input文件中任意数量的指定值。如果这太过分,那么以下原始方法之一就足够了。

\documentclass[letterpaper,10pt]{article}
\usepackage{filecontents,listofitems}
\begin{filecontents*}{boilerplate.tex}
I am excited to apply for the position of [POSITION] at [PLACE]. Your organization's
reputation for rigorous, high-quality programming on topics in align perfectly with 
my ambitions. I would relish the opportunity to collaborate with top coders on our 
shared interests in [INTERESTS]. I am committed to excellence in programming 
and supporting the Chicago Cubs.
\end{filecontents*}
\catcode`[=\active %
\def[#1]{\csname #1\endcsname{}}%
\catcode`[=12 %
\newcommand\myinput[2]{%
  \setsepchar{,}%
  \readlist*\mydata{#2}%
  \foreachitem\myvardef\in\mydata{%
    \setsepchar{=}%
    \readlist*\myvar{\myvardef}%
    \expandafter\edef\csname\myvar[1]\endcsname{\myvar[2]}%
  }%
  \catcode`[=\active %
  \input{#1}%
  \catcode`[=12 %
}
\begin{document}
Dear So and So, 

\myinput{boilerplate}{POSITION=engineer, PLACE=IBM, INTERESTS=omphaloskepsis}
\end{document}

在此处输入图片描述


原始方法

\documentclass[letterpaper,10pt]{article}
\usepackage{filecontents}
\begin{filecontents*}{boilerplate.tex}
I am excited to apply for the position of \POSITION{} at \PLACE{}. Your organization's
reputation for rigorous, high-quality programming on topics in align perfectly with 
my ambitions. I would relish the opportunity to collaborate with top coders on our 
shared interests in \INTERESTS{}. I am committed to excellence in programming 
and supporting the Chicago Cubs.
\end{filecontents*}
\newcommand\myinput[4]{%
  \def\POSITION{#2}%
  \def\PLACE{#3}%
  \def\INTERESTS{#4}%
  \input{#1}%
}
\begin{document}

Dear So and So, 

\myinput{boilerplate}{engineer}{IBM}{omphaloskepsis}

\end{document}

在此处输入图片描述

如果确实想在输入文件中使用括号分隔符,那么这是可行的(已编辑以允许任意数量的逗号分隔的参数#2):

\documentclass[letterpaper,10pt]{article}
\usepackage{filecontents,listofitems}
\begin{filecontents*}{boilerplate.tex}
I am excited to apply for the position of [POSITION] at [PLACE]. Your organization's
reputation for rigorous, high-quality programming on topics in align perfectly with 
my ambitions. I would relish the opportunity to collaborate with top coders on our 
shared interests in [INTERESTS]. I am committed to excellence in programming 
and supporting the Chicago Cubs, [FOUR], [FIVE], [SIX], [SEVEN], [EIGHT], [NINE],
and [TEN].
\end{filecontents*}
\catcode`[=\active %
\def[#1]{\csname #1\endcsname{}}%
\catcode`[=12 %
\newcommand\myinput[2]{%
  \setsepchar{,}%
  \readlist*\mydata{#2}%
  \def\POSITION{\mydata[1]}%
  \def\PLACE{\mydata[2]}%
  \def\INTERESTS{\mydata[3]}%
  \def\FOUR{\mydata[4]}%
  \def\FIVE{\mydata[5]}%
  \def\SIX{\mydata[6]}%
  \def\SEVEN{\mydata[7]}%
  \def\EIGHT{\mydata[8]}%
  \def\NINE{\mydata[9]}%
  \def\TEN{\mydata[10]}%
  \catcode`[=\active %
  \input{#1}%
  \catcode`[=12 %
}
\begin{document}

Dear So and So, 

\myinput{boilerplate}{engineer,IBM,omphaloskepsis, Orioles, Red Sox, Cardinals,
  Phillies, Tigers, Blue Jays, Braves}

\end{document}

在此处输入图片描述

答案2

\documentclass[letterpaper,10pt]{article}

% Definition of the variables
\newcommand{\myInterest}{Golf}
\newcommand{\myPosition}{Senior Engineer}
\newcommand{\myPlace}{New York}

\begin{document}

Dear So and So, 

% Use
% \myPlace{}
% \myPosition{}
% \myInterest{}
% in boilerplate.tex

\input{boilerplate.tex}

\end{document}

{}很重要,可以防止 LaTeX “吃掉” 命令后面的空格,请参见这里例如。考虑使用空间如果您喜欢我的解决方案,请打包。

答案3

您几乎可以隐藏boilerplate.tex文件中的所有内容。诀窍是在宏中定义文本,该宏将在最后调用,此时将定义相应键的值。

文件boilerplate.tex

% define the text
\newcommand{\boilerplate}{%
I am excited to apply for the position of \use{position} at \use{place}.
Your organization's reputation for rigorous, high-quality programming
on topics in align perfectly with my ambitions. I would relish the
opportunity to collaborate with top coders on our shared interests
in \use{interests}. I am committed to excellence in programming and
supporting the Chicago Cubs.}

% define the infrastructure
\ExplSyntaxOn
\keys_define:nn { boilerplate }
 {
  position  .tl_set:N = \l_invictus_position_tl,
  place     .tl_set:N = \l_invictus_place_tl,
  interests .tl_set:N = \l_invictus_interests_tl,
 }
\cs_new_protected:Nn \invictus_use_boilerplate:n
 {
  \keys_set:nn { boilerplate } { #1 }
  \boilerplate
 }
\cs_set_eq:NN \printboilerplate \invictus_use_boilerplate:n
\cs_new:Nn \invictus_use_variable:n
 {
  \tl_use:c { l_invictus_#1_tl }
 }
\cs_set_eq:NN \use \invictus_use_variable:n
\ExplSyntaxOff

文件test.tex

\documentclass{article}
\usepackage{xparse}

\usepackage{parskip} % normal choice for business letters

\input{boilerplate}

\begin{document}

Dear So and So,

\printboilerplate{
  position = pitcher,
  place = Wrigley Field,
  interests = strikeouts,
}

Best regards.

\end{document}

输出

在此处输入图片描述

相关内容