[Tex/LaTex] Passing parameters to \input{text} (included text file)

pdftex

I'm writing a bunch of cover letters that are roughly 90% boilerplate and 10% customization. However, the boilerplate contains variables that I need to change each time. Similar to a function, I want to pass arguments to the boilerplate. Unlike previous posts about automation that seek to pass parameters to document structure, my goal is to pass parameters to document content. (My working assumption is that this is a different problem.)

To fix thoughts, here is a MWE of my template cover letter:

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

Dear So and So, 

\input{boilerplate}

\end{document}

where boilerplate.tex is text with variables:

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.

and the idea is to pass arguments to [POSITION], [PLACE], and [INTERESTS].

Is it possible to pass parameters to input{}, or can this only be done on the command line?

Best Answer

Using the listofitems package, here is a homemade key-value interface that can handle an arbitrary number of specified values in the \input file. If that is overkill, one of the original approaches that follow should suffice.

\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}

enter image description here


ORIGINAL APPROACHES

\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}

enter image description here

And if one really wanted to use bracketed delimiters in the input file, then this works (EDITED to allow arbitrary number of comma separated arguments in #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}

enter image description here

Related Question