You are on page 1of 6

Mail merge with the use of formlett.

sty
Zhuhan JIANG
School of Computing and Information Technology, University of Western Sydney, Vic-
toria Road, Parramatta NSW 2150, Australia. Email: z.jiang@uws.edu.au
(Last updated on 26 May 2003)
In this article, the author explains how to use a form-letter or mail merge style
formlett.sty, designed for plain TEX and LaTEX or LaTEX2e.formlett.sty supports
different parameter input methods, parameter naming and defaulting mechanism, as
well as facilities for previewing parameter positions and printing labels.It is written for
the purpose of being powerful, robust and above all easy to use.

Introduction We have been looking for 1 – 5 for quite a while


Our purpose here is to describe a comprehensive im- without any luck, could you help us out? If so,
plementation of a macro system, handling form let- please ring 1 – 6
.
ters or mail merge under plain TEX or LaTEX 2.09
or LaTEX2e. The main objective is to provide an Cheers! Michael
easy way to output many form letters with their and we need just 6 parameters to personalise an indi-
own parameters, with or without the use of multiple vidual letter. A simplest example of supplying a set
files. There will be a coherent and simple format for or a cluster of these parameters to output a merged
putting parameters inside a form letter, with a num- form letter is to use
ber of helping facilities for such as naming param- \moreletter
eters and previewing their positions. A minimum Mrs L Stenson;\#1-20 Sunset Street;%
support for printing mailing labels is also provided. Hills, Norway;Louise;a Bible;220-8888!
The concept of macros [1] for form letters is not new: For more letters to be merged this way, just use
there already exist macros in this connection such as again the command \moreletter in the above for-
merge, textmerg and address to name a few, see mat, in which each parameter is separated by a semi-
[2,3] for further details. Our stress here is therefore colon ‘;’ and the whole parameter cluster is ended
laid on the ease to use, along with the power and by an exclamation mark ‘!’. The actual code in
the robustness of the macros. LaTEX for making the above form letter, along with
an extra merged letter, may read as follows
\documentstyle[formlett]{article}
Basic format \begin{document}
0001

0002
Suppose we would like to write a form letter for mail
\beginletter 0003
merge in the following form
\NOPAGENUMBERS\parindent=0pt 0004

<<FULL NAME>> \paras[1]\par\paras[2]\par 0005

<<ADDRESS-01>> \paras[3]\par\medskip 0006

<<ADDRESS-02>> Dear \paras[4], \par\medskip 0007

We have been looking for 0008


Dear <<GIVEN NAME>>, \paras[5] for quite a while 0009

We have been looking for <<MISSING ITEM>> for without any luck, could you help 0010

quite a while without any luck, could you help us us out? If so, please ring 0011

out? If so, please ring <<PHONE NUMBER>>. \paras[6]. \par\medskip 0012

Cheers!\hfill Michael\vfill\eject 0013


Cheers! Michael
\endletter 0014

where the text inside the double arrow brackets are 0015

to be replaced by the parameters specific to each \moreletter % letter one 0016

individual letter. If we number these parameters Mrs L Stenson;\#1-20 Sunset Street;% 0017

one by one in a single group, then the letter template Hills, Norway;Louise;a Bible;220-8888! 0018

will be organised as \moreletter % letter two 0019

S Wales;UMIST;Manchester;Sir or Madam;% 0020


1 – 1
a \TeX\ package{+}manual;225-9905! 0021
1 – 2
\end{document} 0022

1 – 3 The line numbers in the above and later on are for


quick reference only: they are not a part of the pro-
1 – 4
Dear , gram code. The letter template in lines 4-13 is de-

Jiang Z Mail merge with the use of formlett.sty 1


fined by the \beginletter and \endletter pair in As for the more precise position of the merging pa-
line 3 and line 14, with each use of \moreletter gen- rameters, they can always be viewed via the com-
erating a new merged letter. We note that command mand \preview, which in this case gives the follow-
\paras[m] represents the mth merged parameter ing positioning
(of the first group, see later), and the command
1 – 1
\NOPAGENUMBERS will remove the page numbering
for both LaTEX and plain TEX. We also note that 1 – 2 (+): macro:->\noindent <<paras>> macro:->\par
if one replaces lines 1-2 by \input formlett.sty,
then the program will be compilable under plain Dear 2 – 1
,
TEX as well.
We have been looking for 2 – 2 for quite a while
Although the mail merge mechanism in the above without any luck, could you help us out? If so,
will be sufficient for most simple applications, 2 – 3
please ring .
formlett provides in fact many more features. We
shall thus in the followings introduce the main fea- Cheers! Michael
tures one by one, from the most useful to the less
likely encountered. This way, the mail merge for the above letter tem-
plate can also be done (in plain TEX) in the following
To start with, we notice that in the above exam- more flexible way
ple there are exactly two parameters to be used for \input formlett.sty 0025

the address. This is inconvenient to say the least, \beginletter 0026

even if we allocate say three more extra parameters \NOPAGENUMBERS\parindent=0pt 0027

for the purpose. This naturally calls for the separa- \noindent{\it\paras[1]}\par 0028

tion of the whole cluster of parameters into several \blockparas[2]\par\bigskip 0029

groups, so that the numbering of the parameters in Dear \paras[1][2],\par\medskip 0030

every group will be resynchronised. We thus use We have been looking for 0031

\paras[m][n] to denote the mth parameter of the \paras[2][2] for quite a while 0032

nth group of a whole cluster of merging parameters. without any luck, could you help 0033
In fact we may use \blockparas[m][n][pre][post] us out? If so, please ring 0034
to represent parameters in the nth group, from the \paras[3][2]. \par\medskip 0035
mth to the last parameter of that group, where the Cheers!\hfill Michael\vfill\eject 0036
tokens denoted by pre and post are those to be added \endletter \preview 0037
in front and behind respectively each of the legit- 0038
imate parameters. The defaults for pre and post \beginpilemode 0039
are \noindent and \par respectively. It is therefore Mrs L Stenson;\#1-20 Sunset Street 0040
more sensible to specify the merging parameters of Hills, Norway+Louise;a Bible;220-8888! 0041
the earlier example via 0042

<<FULL NAME>> 0023


S Wales;UMIST;Manchester+% 0043

<<ADDRESS-etc>> 0024
Sir or Madam;a \TeX\space 0044

package{+}manual;225-9905! 0045

Dear <<GIVEN NAME>>, \endpilemode 0046

\end 0047
We have been looking for <<MISSING ITEM>> for
We note that by default we used plus sign ‘+’ to
quite a while without any luck, could you help us
separate different groups inside a cluster of parame-
out? If so, please ring <<PHONE NUMBER>>.
ters, and each cluster inside a \beginpilemode and
Cheers! Michael \endpilemode pair will output a merged letter, as
if it were produced by command \moreletter fol-
where <<ADDRESS-etc>> will be produced by lowed by that cluster of parameters. The output of
\blockparas[2][1], i.e. all the parameters of the the first merged letter in fact reads
first group, starting from the 2nd parameter. We
note that the missing square-bracketed macro pa- Mrs L Stenson
rameters for \blockparas are automatically pro- #1-20 Sunset Street Hills, Norway
vided: in fact macro parameters in formlett Dear Louise,
are systematically defaulted. Hence for example,
We have been looking for a Bible for quite a while
\paras[1][1] is equivalent to any of the following
without any luck, could you help us out? If so,
commands
please ring 220-8888.
\paras, \paras[1], \paras[][1],
\paras[1][], \paras[][] Cheers! Michael

Jiang Z Mail merge with the use of formlett.sty 2


It is often desirable to supply parameters line by Louise 0086

line, i.e. one line as one parameter. Hence the a Bible 0087

two merged letters produced by lines 25-47 can also 220-8888 0088

be made by replacing the content included in the Mrs L Stenson 0089

\beginpilemode and \endpilemode pair (lines 39- \#1-20 Sunset Street 0090
46) by the following new code Hills, Norway 0091
\blockmarks 0048
0092
\beginblockmode 0049
Sir or Madam 0093
Mrs L Stenson 0050
a \TeX\ package+manual 0094
\#1-20 Sunset Street 0051
225-9905 0095
Hills, Norway 0052
S Wales 0096
----- 0053
UMIST 0097
Louise 0054
Manchester 0098
a Bible 0055
\endlinemode 0099
220-8888 0056
\end{document} 0100
====== 0057
Notice that we have changed the numbering order of
0058
the parameters so that the number of address lines
S Wales 0059
output by \blockparas is more flexible.
UMIST 0060

Manchester 0061

---- 0062
Advanced features
Sir or Madam 0063
The advanced features to be discussed below are
a \TeX\ package+manual 0064
mainly for the purpose of (i) writing long form letter;
225-9905 0065
(ii) making many different form letters; (iii) read-
==== 0066
ing mail merge parameters that are in crude ASCII
form; and (iv) generating mail labels.
\endblockmode 0067

\defaultmarks 0068 It is in general better to keep the letter tem-


where obviously we have used lines with ‘----’ to plate, i.e. the content between \beginletter
separate groups and used lines with ‘====’ to end and \endletter like those in lines 4-13, in
a cluster of parameters. This was done by the a separate file, say myletter.let, and use
use of \blockmarks which changes the group de- \inputletter{myletter.let} to load in the letter
limiter ‘+’ and cluster delimiter ‘!’ into the above template. The advantage of this over the previously
two new ones, with the \defaultmarks at the end used method is that the letter template can now be
setting them back. If the whole cluster of parame- arbitrarily large without any risks of causing com-
ters are categorised into a single (first) group, then puter memory problem.
we may even use a block of consecutive nonempty
lines to provide the merging parameters. This is Suppose we have a collection of letter templates, we
done by the use of command pair \beginlinemode may wish to remind ourselves what each parameter
and \endlinemode. Hence we may also conduct the represents. For this purpose, we may add at the
above mail merge in for instance LaTEX2e by top of the letter template a command \paranames
\documentclass{article} 0069
cluster!, where cluster is a cluster of parameters
\usepackage{formlett} 0070
each giving a name to the corresponding parameter.
\begin{document} 0071
Command \paranames takes its macro parameters
\beginletter 0072
in the same way \moreletter does. After a letter
\NOPAGENUMBERS\parindent=0pt 0073
template is read in, command \showparas will out-
\paras[4]\par\blockparas[5] 0074
put a form letter with its parameters replaced by
\par\medskip 0075
the corresponding names. The mainly italic passage
Dear \paras[1], \par\medskip 0076
containing lines 23-24, for instance, is the output of
\showparas for the letter template in lines 102-109
We have been looking for 0077
given explicitly later on.
\paras[2] for quite a while 0078

without any luck, could you help 0079 We may also use \paradefaults cluster! to
us out? If so, please ring 0080 provide default parameters, i.e. the parame-
\paras[3]. \par\medskip 0081 ters to replace the empty or not entered ones,
Cheers!\hfill Michael\vfill\eject 0082 and use \loaddefaultparas inside letter tem-
\endletter 0083 plate to activate the parameter defaulting. We
0084 note that in general commands \paranames and
\beginlinemode 0085 \loaddefaultparas, if present, should be at the top

Jiang Z Mail merge with the use of formlett.sty 3


of the letter template, with \loaddefaultparas be- \inputletter{myletter.let} 0136

low the command \paranames. Hence for our pre- \showparas \preview % utility demo 0137

vious mail merge, we may re-do it via \paradefaults To whom it may concern! 0138

\input formlett.sty 0101 \inputfile{myletter.adr} 0139

\beginletter 0102 \beginlabels % 1st group as address 0140

\paranames % optional 0103 \inputfile{myletter.adr} % for labels 0141

\tt<<FULL NAME>>;\tt<<ADDRESS-etc>>;% 0104 \endlabels 0142

+\tt<<GIVEN NAME>>;\tt<<MISSING ITEM>>;% 0105 \end{document} 0143

\tt<<PHONE NUMBER>>! 0106 where the lines 140-142 will generate the address
\loaddefaultparas % optional 0107 labels using the first group (as is intended here) of
{letter details given in lines 27-36} 0108 parameters. More precise format of \beginlabels
\endletter 0109 and etc can be found in the Appendix.
\preview \showparas 0110

0111
Another way of making labels, if no separate
\paradefaults % optional 0112
files for the parameters are present, is to re-read
To whom this may concern 0113
the document itself and make labels instead of
+Sir or Madam;something;% 0114
merged form letters in the second reading. We
061-225-9905! 0115
may thus use \input formlett.sty \initstyle
0116
[styles]{article}{preamble} to replace
\blockmarks 0117
\documentstyle [formlett, styles]{article}
\beginrawblockmode{} 0118
preamble \begin{document}, which will be valid
Mrs L Stenson 0119
for LaTEX but ignored for TEX , and will en-
#1-20 Sunset Street 0120
able one to use \labelsquit at the end to read
Hills, Norway 0121
in the current document again with all the let-
------ 0122
ters there converted into the corresponding la-
Louise 0123
bels. Though \initstyle is valid for plain TEX,
a Bible 0124
LaTEX2.09 as well as LaTEX2e, \initclass will gen-
220-8888 0125
erate \documentclass instead of \documentstyle
========= 0126
when LaTEX2e is the processing environment. If you
0127
only want to execute certain commands the first
...... 0128
time round (i.e. before \labelsquit re-reads the
0129
file again), use \firstread{commands} for this pur-
Above empty line active 0130
pose. For more detailed use and examples, read the
\endrawblockmode 0131
programmer’s document attached to the style file
\defaultmarks 0132
formlett.sty and the example files it generates.
\end{document} 0133
In fact, many details and extra features that are not
We note that in blockmode with \blockmarks, a contained in this article may be found there.
line with ‘....’ marks the immediate start of a We already noted that if a form letter is large, we
parameter block, whether or not the lines imme- have to use \inputletter to load in the template.
diately following it are empty or not. Also that However, it is often still desirable to keep everything
in rawblockmode enclosed by \beginrawblockmode inside a single file, while allowing it to make new
and \endrawblockmode, all characters are input in (scratch) files when it is being compiled. We may
their original ASCII form. In other words, the spe- thus use for example
cial characters for TEX will be ignored here. \beginfile{myletter.let}
If the merging parameters are given by exactly m {letter template of lines 103-108}
lines per cluster, as is often the case when they are \endfile
produced by a database utility, then we may use the to create a letter template file myletter.let. Like-
\begindatamode and \enddatamode pair to mark wise, the merging parameters can be saved to an-
the beginning and the end of the merging parame- other run-time created file myletter.adr.
ters. For more details, see the Appendix at the end.
We have so far only used the default delimiters
We may keep the letter template and the merging ‘;’, ‘+’ and ‘!’ to separate single, group and
parameters in two separate files. For instance, if we cluster of parameters, with temporary change via
save lines 103-108 to file myletter.let, lines 39- \blockmarks to ‘....’, ‘----’ and ‘====’ respec-
46 or lines 48-68 to file myletter.adr, then we can tively. However we may change the delimiters to
produce via LaTEX the mail merge by any characters by \delimiters{S}{G}{C}, where
\documentstyle[formlett]{article} 0134 S, G and C are the tokens to delimit single, group
\begin{document} 0135 and cluster of parameters respectively, and change

Jiang Z Mail merge with the use of formlett.sty 4


the delimiters back to the default by \defaultmarks \initstyle in the replaced line is replaced further-
or equivalently by \delimiters{;}{+}{!}. more by \initclass, except that now the native
LaTEX2e environment is preserved, when applicable,
To conclude this section, let us finally look below rather than turning it into the compatibility mode
at an ‘ideal’ and ‘finished’ mail merge application of LaTEX2.09.
code,. It is currently in a form compilable under
LaTEX2e, and is also valid for plain TEX if the first Programmer’s tips
three lines (lines 144-146) are removed.
First we note that all the macro parameters that are
\documentclass{article} 0144
to appear inside square brackets of formlett’s com-
\usepackage{formlett} % LaTeX2e 0145
mands are defaulted, just like how \paras[m][n]
\begin{document} 0146
is defaulted earlier on. The actual default values can
0147
be found in the Appendix.
%%%% MAKE file scr@tch@.let 0148

\beginfile{scr@tch@.let} % letter content 0149


For those wizard users who want to do every-
\paranames <<name>>;<<address>>+<<items>>! 0150
thing their own way, we point out that if for in-
stance the 3rd letter parameter of the 2nd group
\loaddefaultparas 0151
of a cluster is given as <A>, then \LET2*3~ will
\parindent=0pt\blockparas\bigskip\bigskip 0152
contain \b@group\relax<A>\e@group right after a
Dear \paras, \par\bigskip 0153
cluster is read in. \DEF2*3~, on the other hand,
This part is typically the letter content. 0154
contains the corresponding default parameter in
It now displays all the items in the 2nd 0155
the same fashion. Furthermore, the command
parameter group. They are \par 0156
\checkparas[m][n]{LET} will copy the content of
\blockparas[][2] \vfill\eject 0157
\paras[m][n], minus the ‘wrapping’ extra tokens
\endfile 0158
\b@group\relax and \e@group, to \cachedata and
0159
set \ifemptyparas to true or false depending on
%%%% MAKE file scr@tch@.adr 0160
whether the content is empty or not. This way,
\beginfile{scr@tch@.adr} % address file 0161
a user may even change the characteristics of his
\blockmarks 0162
letter template by first testing the content of the
\beginblockmode 0163 supplied individual parameters. However, we note
T Teng 0164 that if \loaddefaultparas is executed, then the
UMIST 0165 LET array, when some of its elements are not sup-
Manchester M60 1QD 0166 plied, will contain the corresponding elements of the
===== 0167 DEF array. Hence care must be exercised under such
0168 circumstances, when interpreting the \cachedata
Z Jiang 0169 generated by \checkparas[m][n]{LET}. If neces-
UNE 0170 sary, we may use \delparadefaults to delete cur-
Armidale NSW 2351 0171 rent default parameter array DEF so as to conduct
Australia 0172 \checkparas{LET} more precisely.
------ 0173
There are also a number of generic macro utilities
Email: zhuhan@neumann.une.edu.au 0174
in formlett, including a user stack and a multi-
===== 0175 dimensional array mechanism.
\endblockmode 0176
formlett was written for all TEX dialects, its format
\defaultmarks 0177
is thus more close to that of plain TEX. In fact, we
\endfile 0178
deliberately avoided mixing up formlett with the
0179
standard letter environment of LaTEX. This is largely
% MAIN BODY 0180
due to the fact that the limited facilities of LaTEX
\paradefaults To Whom It May Concern 0181
letter environment are easy to come by anyway, and
+No further info available{!}! 0182
are not really worth writing buckled code to make
\inputletter{scr@tch@.let} 0183
formlett a type of extension of the environment.
\inputfile{scr@tch@.adr}% for letters 0184
Nevertheless, one can still use the letter environment
\beginlabels 0185
of LaTEX inside the form letter template. Besides,
\inputfile{scr@tch@.adr}% for labels 0186
the applicability of formlett is not restricted for
\endlabels 0187 making form letters; it can also be used to merge
\end{document} 0188 other type of articles or passages.
We note that if we replace lines 144-146 by
\input formlett.sty \initstyle{}{}, then the References
mail merge can be processed by either plain TEX [1]. Knuth D E, The TEXbook, Reading, Mass.,
or LaTEX (including LaTEX2e). It is likewise if Addison-Wesley, 1992.

Jiang Z Mail merge with the use of formlett.sty 5


[2]. Piff M, Text merges in TEX and LaTEX, TUG- n
\beginletter delimiters for letter con-
boad, 13(4):518, 1993. tent (template)
[3]. Damrau J and Wester M, Form letters with n \endletter
\beginpilemode normal letter parameters
3-across labels capacity, TUGboat, 13(4):510, n \endpilemode cluster-wise
1991. \beginblockmode for line-by-line blocks of
\endblockmode parameters, empty lines
active within each cluster
Appendix n
\beginlinemode for line-by-line parame-
\endlinemode ters, empty lines delimit
In the followings, we give a brief summary of the clusters
main commands given by formlett version 2.1.
n
\beginrawblockmode{T } raw text mode;
\endrawblockmode nonempty T replaces
Let m and n be numbers, p, q and r be dimen- \endrawblockmode to
sions, A, B, S, G, C and T be tokens, and X be a n mark end
\beginrawlinemode{T } raw text parameters and
box. Furthermore, we shall denote by R a full set
n \endrawlinemode active spaces etc
of letter parameters ended by ‘!’, with ‘;’ separat- \begindatamode[T ]{m} m raw text lines for one
ing parameters inside a same group and ‘+’ separat- \enddatamode form letter
ing different parameter groups. We moreover denote \PAGENO=1 page number reset to 1
\NOPAGENUMBERS no page numbers
R by F , when ‘; + !’ there can be replaced by ‘S \textbox[p]{text} text into box of width p
G C’ respectively if \delimiters{S}{G}{C} is is- \boxmore[r]{X} add borderline to box X
sued. In the commands tabulated below, the macro at a distance r
\addressbox[p][q]{text} text into box of width
parameters contained in squared brackets support p, with wrapped options
default. In particular, the defaults are m=1, n=1, n indented by q
p=8truecm, q=1.5em, r=3pt, A=\noindent and \beginlabels[a][b][c][d][e][f ]
\endlabels
B=\par. form letters become la-
defaults: bels: address taken from
a=20pt, cth to last parameter of
\paras[m][n] mth parameter of nth b=\tt\raggedright, dth group, with width e,
group c=1, d=1, indent f , borderspace a
\blockparas[m][n][A][B] mth to the last param- e=2.6in, f =2em and font toks b
eter of nth group, each \firstread{T } toks T will not be read
preceded by A and fol- if the file is re-read via
lowed by B, wrapped by \labelsquit
{} if B=\relax \initstyle[a]{b}{c} initiation for
\addressparas[m][n][p][q] mth to the last pa- \labelsquit,with styles
rameter of nth group, a, documentstyle b and
each put into a box of preamble c
width p with indent q for \initclass[a][o]{b}{c} similar to \initstyle (o
wrapped portions is LaTEX2e options), but
\loaddefaultparas fill empty parameters retains native LaTEX2e
with defaults when applicable
\checkparas[m][n]{T } mth parameter of \labelsquit[a][b][c][d][e][f ]{file.ext}
nth group copied
to \cachedata; quit after converting let-
\ifemptyparas is true defaults: ters to labels by reading
if element is empty; T is see that for \beginlabels the current document or
often LET or DEF file.ext
\moreletter F use parameters F to out-
put a new letter
\paranames R use R as parameter
names
\paradefaults R use R as default
parameters
\delparadefaults delete default parameters
\delimiters{S}{G}{C} use S, G, C as delimiters
\defaultmarks use ‘; + !’ as delimiters
\blockmarks use ‘....’, ‘----’, ‘====’
as delimiters
\preview highlight parameter
positions
\showparas display parameter
n names, if any
\beginfile[T ]{file.ext} write text verbatim to
\endfile file file.ext (empty im-
plies scr@tch@.tex),
nonempty T replaces
\endfile to mark last
full line
\inputletter{f ile.ext} input letter content
\inputfile{f ile.ext} input f ile.ext

Jiang Z Mail merge with the use of formlett.sty •End of Document• 6

You might also like