A package for making sticky labels in L^{A}TEX^{∗}

Sebastian Rahtz, Leonor Barroca  Grant Gustafson

1995/06/22

Contents

1 Usage 1 1.1 Paper Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 Label Stock Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 Customizing for Other Labels . . . . . . . . . . . . . . . . . . . . . 3 1.4 Using an External Label source File . . . . . . . . . . . . . . . . . 4 1.5 Labels in the Main File . . . . . . . . . . . . . . . . . . . . . . . . 5 1.6 Control Sequences to Make Labels . . . . . . . . . . . . . . . . . . 6 1.7 Wrapping Long Lines and Debugging . . . . . . . . . . . . . . . . . 7

2 The macros 7

3 User macros 11

4 History and acknowledgements 14

Abstract

A L^{A}TEX style to print a regular grid of ragged-right labels on a page, suitable for sheets of labels which can be fed through a laser printer. Macros are provided to allow easy input of names and addresses in a form free of TEX markup. Equally useful is a feature for making multiple copies of a single label, e.g., return address stickers to go with the labels.

1 Usage

This style file was written to produce a sheet of labels which can be xeroxed onto Avery brand 5360 sticky-label material. This label material is made for a xerox machine and it has 7 rows and 3 columns of labels on letterpaper. It works for other label layouts as well. In particular, the defaults for the most popular paper sizes letterpaper and a4paper are 7 rows by 3 columns and 8 rows by 3 columns, respectively.

Other uses include production of disk labels, book labels, shipping labels, name tags, door cards, photo galleries and compact membership lists.

The service of this package is to produce a rectangular grid of items on a sheet of paper, with each item centered in its grid area. It really doesn’t matter what goes in the grid area: text, graphics or any L^{A}TEX construct. For example, you might use the fancybox package to produce oval-box or shadow-box name tags for a conference.

∗This file has version number 10, last revised 1995/06/22.

1.1 Paper Sizes

The paper size is supplied by the document options for the class. The European default is a4paper and the American default is letterpaper, 8.5 × 11-inch, inherited from the class file, e.g., article.cls.

a4paper 297mm by 210mm.

a5paper 210mm by 148mm.

b5paper 250mm by 176mm.

letterpaper 11in by 8.5in.

legalpaper 14in by 8.5in.

executivepaper 10.5in by 7.25in.

1.2 Label Stock Sizes

A number of different label stocks are available for xerox machines and laser printers. The typical stock has m rows and n columns with various gutters on the page. Below is a table of Avery products that are in common use in America.

Label Size Labels Rows Cols Gutters Avery
in inches per page  Stock No
1 \frac{7}{16} × 2 \frac{13}{16} 21/page  7  3  Yes  5360
2 × 4 \frac{1}{4}  10/page  5  2  Yes  5352
2 × 4  10/page  5  2  Yes  5327
1 × 4  20/page  10  2  Yes  5161, 5261
1 \frac{3}{8} × 2 \frac{11}{16} 21/page  7  3  Yes  5329
1 × 2 \frac{5}{8}  30/page  10  3  Yes  5331, 5160, 5260
1 \frac{1}{3} × 4  14/page  7  2  Yes  5162, 5262
\frac{1}{2} × 1 \frac{3}{4}  80/page  20  4  Yes  5267
1 × 2 \frac{3}{4}  33/page  11  3  No  5332, 5351, 5354, 5314

Another common label stock is Dennison 37-141, having 3 columns and 8 rows of 1\frac{3}{8} × 2\frac{7}{8} gutterless labels on letterpaper. The information by the manufacturer is not precisely correct, because the first column is exactly 2\frac{7}{8} inches wide, but the other two are 2\frac{13}{16} inches wide.

A common problem with implementing this package on a new label stock is precision of the laser printer, which prints the master, and the xerox machine which prints the master onto the label stock. Some of the above are for direct use on a laser printer, which removes one source of error, but replaces it by the possibility that the label stock will jam in the laser printer!

1.3 Customizing for Other Labels

Your labels will very likely be different than Avery 5360. So first you have to tailor your source file to a particular type of label. The lines below may be placed in the preamble and edited to suit the application and the actual printer used for output.

   \documentclass[12pt]{article}
   \usepackage{labels}
   \LabelCols=3%      Number of columns of labels per page
   \LabelRows=7%      Number of rows of labels per page
   \LeftBorder=8mm%   Space added to left border of each label
   \RightBorder=8mm%  Space added to right border of each label
   \TopBorder=9mm%    Space to leave at top of sheet
   \BottomBorder=2mm% Space to leave at bottom of sheet
   \begin{document}%  End of preamble

These controls can also be issued after the beginning of the document, but the results are undefined unless the following example is followed, which uses the \LabelSetup control sequence:

   \begin{document}%  End of preamble
   \LabelCols=3%      Number of columns of labels per page
   \LabelRows=7%      Number of rows of labels per page
   \LeftBorder=8mm%   Space added to left border of each label
   \RightBorder=8mm%  Space added to right border of each label
   \TopBorder=9mm%    Space to leave at top of sheet
   \BottomBorder=2mm% Space to leave at bottom of sheet
   \LabelSetup%       Invoke new settings

For example, your grid maybe has only two columns of ten labels each, requiring the first two variables to be reset. The gutter areas on the label material dictate how to set the other parameters, plus of course personal adjustments of the amount of white space around each label.

Your printer driver is expected to print the page exactly as it should in vanilla TEX, i.e., with the origin of the page down one inch and right one inch from the top left hand corner of the paper. If it doesn’t, adjust your driver parameters.

The usual problem with printers is that label output is quite wide, requiring the very edges of the paper, on which your printer may not write correctly. Ad-

just the parameters \LeftBorder, \RightBorder, \TopBorder, \BottomBorder

as outlined below to solve this common problem.

First of all, \TopBorder is not the white space at the top of the page, but the amount of space to leave at the top so that the first label is centered on its grid. This number is determined by trial and error using the actual printer, xerox machine and label stock to be used in the production run.

The second value \BottomBorder is determine empirically also, so that the labels are all centered on their grids. A mathematical formula to be satisfied is

        Label Height = \frac{paperheight − TopBorder − BottomBorder}{LabelRows}

The values \LeftBorder and \RightBorder are amounts of white space to be added to the left and right of the actual label area so that the label itself does not smash into the edge of the grid. The actual label width is considerably smaller than the grid width (physical width of the label stock). It is determined by the formula

         LeftBorder + Label Width + RightBorder = \frac{paperwidth}{LabelCols}

Normally, the left and right white space dimensions are the same, but there can be exceptions. It is best to determine the dimensions empirically on the actual equipment to be used in the production run. Visual guides from a dvi viewer can help, but be aware that the laser printer may fail to print near the edge and the xerox machine may either enlarge or shrink the image onto the label stock.

An option grid overlay can be produced with the label output for testing purposes by the preamble control sequence \LabelInfotrue. Most dvi viewers are capable of showing the entire test grid. Once printed, the overlay can be compared with the actual label stock to see if the borders are in the proper place. By adjusting parameters, the master copy off the laser printer should be able to go through the xerox machine automatically.

1.4 Using an External Label source File

The simplest form of input is very easy, as in the following example:

   \documentclass{article}
   \usepackage{labels}
   \begin{document}
   \labelfile{names.dat}
   \end{document}

where names.dat contains names and address in plain format. Source files in plain format may contain extra blank lines (only one blank between labels is required). Sort fields should begin with % in column 1, so they don’t print as part of the label. Leading and trailing blanks on lines are ignored. Ctrl-L’s seem to be acceptable in the source. They are not ignored, even if one appears at the end of the file. For example, if ^L appears on a line by itself, then it either produces an empty label or else a blank line, if it happens to be in a group of label lines. The label data commonly appears on lines, flush left, with no intervening blank lines, because a blank line signals a new label. To purposely create blank labels, as for filling out a page of labels, see the control sequence \skiplabels{#1}, infra. The actual text in the external file may contain L^{A}TEX markup controls. This includes comment marks %, which will be ignored during typesetting.

Source File Creation and Sorting

Most mailing lists that already exist can be edited to create an acceptable list in the proper format. If you want long lines to wrap, then force all the material to be wrapped onto one line in the source. Otherwise, break the material into lines of the length desired. Any special items in a mailing label can be coded in L^{A}TEX, e.g., foreign names with accented characters, trademarks and font size changes.

Generally, raw sources have to be searched for special L^{A}TEX controls and brought up to standard. The following characters are trouble in a raw source file. Only use for writing L^{A}TEX code obeying L^{A}TEX coding rules!

Double quote  \"     Hash (number) \#     Underscore    \_
Dollar        \$     Percent       \%     Ampersand     \&
Less than     \<     Greater than  \>     Vertical bar  \|
Commercial at \@     Backslash     \\     Circumflex    \^
Left brace    \{     Right brace   \}     Tilde         \~

A typical comment line might start with % and then ^L (ctrl-L) followed by a sort key (e.g., the zip code or last name). The emacs editor supports a method of sorting such records, thereby rearranging the source file into a new label order. The method:

• Mark the whole file as a region: ^[< goes to the top of file, then ^@ sets mark; follow by ^[> to mark the whole file.

• Invoke emacs’s sort-pages external routine as follows. Press ^[x then enter sort-pages and press return. The region marked will be sorted on the first line of each page, with a page delimited by ^L.

General purpose L^{A}TEX Source

If you want to set up a general-purpose L^{A}TEX source file, then use the following source, which prompts for the labels file name.

    \documentclass{article}
    \usepackage{labels}
    \begin{document}
    \promptlabels
    \end{document}

1.5 Labels in the Main File

You can, of course, just have the names and addresses in the main file, rather than using \labelfile to include them. The file format is to be exactly as described above, especially, follow the advice about blank lines, which delimit labels. This example makes use of the labels environment.

   \documentclass{article}
   \usepackage{labels}
   \begin{document}
   \begin{labels}
   Me
   My address
   My City, State, Zipcode
   My Brother
   His address
   His City, State, Zipcode
   \end{labels}
   \end{document}

1.6 Control Sequences to Make Labels

There are other ways of accessing the same system.

1. The control sequence called \addresslabel[#1]{#2} accepts for optional argument #1 L^{A}TEX controls, e.g., font size and style, and for argument #2 the rows of a tabular array of one column.

    \documentclass{article}
    \usepackage{labels}
    \begin{document}
      \addresslabel[\small\sf]
      {Me \\my street \\ mytown \\ England}
    \end{document}

2. Boxed labels use the special macro called \boxedaddresslabel[#1]{#2}, as follows. This produces a frame-box around the label itself, leaving lots of white space around the frame. An optional argument [#1] is provided for local setting of L^{A}TEX controls, for example, [\fboxsep=3pt] will change the white space near the frame. The boolean variable \LabelInfotrue appears in the preamble in order to print out additional information, especially the label dimensions and the settings of certain variables.

    \documentclass{article}
    \usepackage{labels}
    \LabelInfotrue
    \begin{document}
      \boxedaddresslabel[\fboxsep=3pt]
      {{\bf Me} \\my street \\ mytown \\ England}
    \end{document}

3. To duplicate a label, there is a counter called \numberoflabels which you can set. For example, to print a return address 21 times, use this source:

    \documentclass{article}
    \usepackage{labels}
    \numberoflabels=21
    \begin{document}
      \addresslabel{Me \\my street \\ mytown \\ England}
    \end{document}

4. For more sophisticated users, there is a macro \genericlabel which you can call, with an argument of whatever you want to appear on the label (e.g., for disk labels). Thus you could have

           \genericlabel{%
           \begin{tabular}{|c|}
           \hline
           My Amazing Program\\
           \hline
           Disk 1 of 1\\
           \hline
           \em We aim to serve\\
            \hline
           \end{tabular}
           }

to produce a label like this:

                                                     |------------------|
                                                     |My Amazing Program|
                                                     |------------------|
                                                     |Disk 1 of 1       |
                                                     |------------------|
                                                     |We aim to serve   |
                                                     |------------------|

This feature has been used to print business cards with graphical logo.

1.7 Wrapping Long Lines and Debugging

Debugging of label files can be assisted by the internal error messages which are emitted when a label box is too high or too wide for the set parameters. This kind of error is unlikely to occur with the standard controls and the labels environment, because by default they use ragged right and wrap long lines.

In all modes, you can opt for a grid around each label field by setting a Boolean variable called \LabelGridtrue, e.g.,

    \documentclass{article}
    \usepackage{labels}
    \LabelGridtrue
    \numberoflabels=21
    \begin{document}
      \addresslabel{Me \\my street \\ mytown \\ England}
    \end{document}

By default you get no grids. The grids are useful for judging the ‘spillover’ of addresses onto adjacent labels, caused by long lines. See also \boxedaddresslabel, which draws a tighter box with more white space around the label text. Both can be used at the same time.

2 The macros

First of all, identify the package start, and provide some compatibility code for L^{A}TEX 2.09. The general agreement is that macro packages will not be retrofitted to the now obsolete L^{A}TEX 2.09, but this one happens to work.

1 ⟨∗package⟩

 2 %
 3 ⟨!latex209⟩\ProvidesPackage{labels}[\LabelsFileInfo]
 4 ⟨!latex209⟩\NeedsTeXFormat{LaTeX2e}

5 ⟨∗latex209⟩

 6 \newdimen\paperwidth
 7 \paperwidth=8.5in% 297mm for a4paper
 8 \newdimen\paperheight
 9 \paperheight=11in% 210mm for a4paper
10 \def\settoheight#1#2{\setbox\@tempboxa%

11 \hbox{#2}#1\ht\@tempboxa\setbox\@tempboxa\box\voidb@x}
12 \def\PackageWarning#1#2{\typeout{#1: #2}}

13 ⟨/latex209⟩

We will be recording the size of a label, and the dimensions of the grid, so set up variables accordingly.

14 \newcount\LabelCols
15 \newcount\LabelRows
16 \newdimen\LeftBorder
17 \newdimen\RightBorder
18 \newdimen\TopBorder
19 \newdimen\BottomBorder
20 \newcount\numberoflabels
21 \newdimen\label@width
22 \newdimen\label@height
23 \newdimen\half@label
24 \newdimen\area@width
25 \newdimen\addr@width
26 \newdimen\LabTmp
27 \newsavebox{\this@label}
28 \newcount\label@number
29 \newcount\skip@labels
30 \newcount\l@so@far
31 \newif\ifLabelGrid
32 \newif\iffirst@label
33 \newif\ifLabelInfo
34 \first@labeltrue
35 \LabelGridfalse
36 \LabelInfofalse

Set defaults for the labels based upon paper size and common use. These values can be reset dynamically at runtime in the preamble.

37 \ifdim\paperwidth=210mm\relax%
38 \LabelCols=3\relax\LabelRows=8\relax% a4paper
39 \else
40 \LabelCols=3\relax\LabelRows=7\relax% letterpaper
41 \fi%

Variables are provided to allow you to force a border on the left and right edges of each label. The values will affect every label, of course, so you may need to experiment to get pleasing results. The other variables adjust the gutter width at the top and bottom of a page. They apply just to these two edges, and do not apply to a particular label. A LaserJetIII seems to ignore about 8mm on the edges. Xerox machines ignore even more, on all sides.

42 \LeftBorder=8mm
43 \RightBorder=8mm
44 \TopBorder=9mm
45 \BottomBorder=2mm

We need to reset all the dimensions appropriately for a page of labels, and the printer will need to know about the paper size as well.

46 \textwidth=\paperwidth
47 \textheight=\paperheight
48 \topmargin=-1in
49 \headheight=0em

50 \headsep=0em
51 \topskip=0em
52 \footskip=0em
53 \oddsidemargin=-1in
54 \evensidemargin=-1in
55 \pagestyle{empty}
56 \parindent=0em
57 \parskip=0pt

Now calculate the size of labels simply as a proportion of the page size (if you haven’t got that right, this won’t work, will it?). This macro is to be executed before the first label is made. In environment labels and macro \genericlabel (see below) this happens automatically. If you write your own label environment or macro, then model it after one of the aforementioned.

58 \def\@LabelSetup{%
59 \global\label@width\textwidth%
60 \global\divide\label@width by\LabelCols%
61 \global\label@height\textheight%
62 \global\advance\label@height by-\TopBorder%
63 \global\advance\label@height by-\BottomBorder%
64 \global\divide\label@height by\LabelRows%

The top margin of the paper is generally unused for labels. Avery 5360 label sheets have a 1/4-inch gutter on the top and bottom. However, there are no left or right margin gutters. We adjust the top margin to keep the labels from printing on the gutter.

65 \global\topmargin=-1in\global\advance\topmargin by\TopBorder%

It is not usually advisable to make the label printing go right to the edge of the available area, so \area@width gives the area that will actually be used for printing; the width is cut down by \LeftBorder plus \RightBorder. These dimensions can be set to zero if you have a design that uses the whole label.

66 \global\area@width=\label@width%
67 \global\advance\area@width by -\LeftBorder%
68 \global\advance\area@width by -\RightBorder%

If the labels are to be produced on a grid, for debugging, then the usual setting of \fbox separator width and \fbox rule width must be subtracted so that the label box doesn’t crash into the grid.

69 \ifLabelGrid%
70 \global\advance\area@width by-2\fboxsep%
71 \global\advance\area@width by-2\fboxrule%
72 \fi%

The height of the label box will be 2\half@label. However, this box will definitely crash into the grid lines. Reduce the label height to account for the rule and separator widths that will be added automatically by the boxing routine later on.

73 \ifLabelGrid%
74 \global\advance\label@height by-2\fboxsep%
75 \global\advance\label@height by-2\fboxrule%
76 \fi%
77 \global\half@label=\label@height\divide\half@label by2\relax
78 \global\label@number=1\relax

79 }

The boolean variable \LabelInfotrue is used to toggle the amount of information printed at runtime. The boolean is placed in the preamble to invoke a more informative printout. The default is no information, but still echo a message, saying how to print more information.

80 \def\LabelSetup{\@LabelSetup%
81 \ifLabelInfo
82 \typeout{Control sequences adjustable in the preamble:}
83 \typeout{\LabelRows=\the\LabelRows}
84 \typeout{\LabelCols=\the\LabelCols}
85 \TypeoutBlurb{\TopBorder}{\TopBorder}
86 \TypeoutBlurb{\BottomBorder}{\BottomBorder}
87 \TypeoutBlurb{\LeftBorder}{\LeftBorder}
88 \TypeoutBlurb{\RightBorder}{\RightBorder}
89 \typeout{Computed values:}
90 \TypeoutBlurb{Label Width}{\label@width}
91 \TypeoutBlurb{Label Height}{\label@height}
92 \else
93 \typeout{To print info, put `\protect\LabelInfotrue' in the preamble}
94 \fi

95 }

We might want to print the same label several times, so \sticky@label will repeat \make@label a specified number of times (\numberoflabels)

96 \numberoflabels=1%
97 \def\sticky@label{\l@so@far=0%
98 \loop\ifnum\l@so@far<\numberoflabels\advance\l@so@far by 1\make@label%
99 \repeat}

The real label-making macro appears below. It assumes the actual text is in a box called \this@label. It is vital to make sure spaces are not included at the end of lines in these macros, or all hell breaks loose. Internal checks are made for box width and height, to report violations. The environments and macros provided below always produce a ragged right box of fixed width. Use \genericlabel to defeat the ragged right box and fixed box width.

100 \def\make@label{%
101 \ifLabelGrid%
102 \let\boxing@type\framebox%
103 \else%
104 \let\boxing@type\makebox%
105 \fi%

The boxes made by the method below can overflow horizontally or vertically. The code below emits an error message which pinpoints the trouble and the degree of difficulty encountered. Most of the time the text wrapping and ragged right controls fix the problem, but some extra long lines can be troublesome. An essential part of this code is to test the box for zero width, which will emit an empty label. If we are doing it on purpose, then emit the label, otherwise discard it.

106 \settowidth{\LabTmp}{\usebox{\this@label}}%
107 \ifdim\LabTmp=0cm\let\action=\relax\else\let\action=\BuildB@x\fi%
108 %\TypeoutBlurb{Box Width}{\LabTmp}%
109 \advance\LabTmp by -\area@width%
110 \ifdim\LabTmp>0cm\relax%
111 \PkgBlurb{Label too wide}{\LabTmp}\fi%

112 \settoheight{\LabTmp}%
113 {\begin{tabular}{l}\usebox{\this@label}\end{tabular}}%
114 %\TypeoutBlurb{Box height}{\LabTmp}%
115 \advance\LabTmp by -\half@label%
116 \ifdim\LabTmp>0cm\relax%
117 \LabTmp=2\LabTmp\PkgBlurb{Label too tall}{\LabTmp}\fi%

Check for an empty box. Build the box provided it has some dimension or else we are skipping labels on purpose. Otherwise, its empty and we discard it.

118 \ifnum\skip@labels<\sk@pped\let\action=\BuildB@x\fi%
119 \action

120 }

Now build the box for the actual label. The box has minimum height, which is set by using a vertical rule of zero width. At the same time, set a minimum box width. Set a position to half-way up a strut of the height of the label, thus forcing text to be the correct height and vertically centered. Apply box methods to adjust the white space left and right, using horizontal rules of zero height.

121 \def\BuildB@x{%
122 \boxing@type[\label@width][c]{%
123 \rule{0pt}{\label@height}%
124 \raisebox{\half@label}[0pt][0pt]{%
125 \rule{\LeftBorder}{0pt}%
126 \usebox{\this@label}%
127 \rule{\RightBorder}{0pt}%
128 }}%

Print + for this label to target errors by label number. Empty labels made on purpose are printed as x. Start a new line and print | if we have printed a row of

 \LabelCols labels.
129 \ifnum\skip@labels<\sk@pped\message{x}\else\message{+}\fi%
130 \ifnum\label@number=\LabelCols%
131 \message{|}%
132 \endgraf\nointerlineskip%
133 \global\label@number=1\else\global\advance\label@number by 1\fi%

134 }%

To print out dimensions in more usual millimeters, the following macros are used, which convert from TEX’s scaled points into millimeters. The common TEX points are also printed.

135 \newcount\@Milli
136 \def\ToMilli#1{\@Milli=#1\advance\@Milli by93225\relax
137 \divide\@Milli by 186450\relax}
138 \def\PkgBlurb#1#2{\ToMilli{#2}%
139 \PackageWarning{labels}{#1 by \the\@Milli mm (\the #2)}%

140 }

141 \def\TypeoutBlurb#1#2{\ToMilli{#2}%
142 \typeout{#1=\the\@Milli mm (\the #2)}%

143 }

3 User macros

The basic case is a generic macro \genericlabel{#1} which takes its argument #1 and puts it out on a label. No ragged right. No fixed width. Very basic.

144 \newcommand{\genericlabel}[1]{%
145 \iffirst@label\LabelSetup\first@labelfalse\fi%
146 \savebox{\this@label}{#1}\sticky@label\@gobblecr}

The more useful macro \addresslabel[#1]{#2} is based upon a tabular environment, therefore it accepts lines ending in \\. It is supposed to reproduce what is made by the labels environment. Extra space left and right is removed from the tabular environment and the width is fixed at value \area@width (see above for a discussion of this computed dimension) with ragged right edge. The optional argument [#1] is provided in order to set local values of certain variables, for example, [\fboxrule=2pt] could appear as the optional argument in order to locally change box rule size, without affecting the grid line size used for debugging. The plan is to use the common denominator of 2.09 and 2e, so first we define some double argument double-talk.

147 \long\def\L@dblarg#1{\@ifnextchar[{#1}{\L@xdblarg{#1}}}
148 \long\def\L@xdblarg#1#2{#1[]{#2}}

Using L^{A}TEX2e extensions, the command \addresslabel[#1]{#2} could be coded

as \newcommand{\addresslabel}[2][]{...}. In coding common to both 2.09

and 2e, the following works:

149 \long\def\addresslabel{\L@dblarg{\@addresslabel}}
150 \long\def\@addresslabel[#1]#2{\genericlabel{#1%
151 \begin{tabular}{@{}p{\area@width}@{}}\raggedright #2\end{tabular}}}

The macro \boxedaddresslabel[#1]{#2} adds a framed box around the address label defined above. The trouble here is in determining the box width, which is reduced from its normal size by the widths of the box rule and rule separator. The optional argument [#1] is the same as for the previous macro, but here it performs a real service, because it is often the case that box rule and box separator sizes should be adjusted locally.

152 \long\def\boxedaddresslabel{\L@dblarg{\@boxedaddresslabel}}
153 \long\def\@boxedaddresslabel[#1]#2{\genericlabel{#1%
154 \addr@width=\area@width\advance\addr@width by-2\fboxsep%
155 \advance\addr@width by-2\fboxrule\fbox{%
156 \begin{tabular}{@{}p{\addr@width}@{}}\raggedright #2\end{tabular}}}}

The environment labels, for verbatim labels, will be defined.

157 \long\def\labels
158 {\iffirst@label\LabelSetup\first@labelfalse\fi\start@@label}
159 \def\endlabels{\end@@label}

The label contents are saved in a box called \this@label, formed as a raggedright minipage of width \area@width. The trick is to make the end of line character into a macro ^^M which gets executed each time it is encountered. By testing for blank lines, we can find the end of a series of consecutive address lines and cause the macro to terminate the label box definition, and possibly start another. The usual action taken on intercept of an end of line character is to insert \newline. It is only the case of a blank line that causes us to end the current label. Here, swallow a pending ^M, to avoid having a blank line at the start of each label. Extra blank lines cause blank labels, which are thrown away when encountered, later on.

160 \def\start@@label{%
161 \savebox{\this@label}\bgroup\raggedright%
162 \begin{minipage}{\area@width}\raggedright%
163 \catcode `\^^M =\active\@gobblecr%

164 }%

165 \def\end@@label{%
166 \end{minipage}\egroup%
167 \sticky@label}%

The principal support macros needed to define the labels environment will be defined below. These macros assume names and addresses appear as consecutive lines separated by a blank line. If we are in the middle of consecutive address lines, then just start a new line.

168 \def\start@newline{\mbox{}\expandafter\newline}%

If we have met a blank line, then finish the current label and start a new label.

169 \def\new@label{\end@@label\start@@label}%

The macro ^^M invokes either \start@newline or \new@label. To define it, we use some hackery from Phil Taylor. Step one is to activate a control sequence at end of line.

170 \catcode `\^^M = \active%

The opaque definition required uses futurelet to selectively expand a control sequence during a definition. The macro ^^M is supposed to replace carriage returns by \newline, or else end this label and start a new one with \new@label. The idea is to define ^^M to be a control sequence \nexttoken, active only when ^^M has been made an active control sequence. Then the expansion of ^^M{#1} will be \nexttoken, provided the next line #1 following the current one has no characters (except carriage return at the end). Otherwise, we must be at the start of a new cluster of label lines.

171 \def ^^M{\futurelet\nexttoken\isitapar}%
172 \def\isitapar{\ifx^^M\nexttoken\let\action=\new@label\else%
173 \let\action\start@newline\fi\action}%

Definitions involving ^^M are finished. Re-instate the original catcode for carriagereturn.

174 \catcode `\^^M = 5\relax%

The syntax of the labels environment can be shortened to a single line, provided an input file name is known and the contents have been prepared for use with the labels environment.

175 \def\labelfile#1{\begin{labels}\input#1\end{labels}}

In the interest of a general engine for label production, the program can prompt for the file name at runtime. This kind of feature makes it possible to run one batch file to create labels, regardless of the source.

176 \def\promptlabels{\typein[\labelfilename]{What is the name of the
177 label file?}
178 \labelfile{\labelfilename}}

It is possible that a number of empty labels should be printed before going on to print the next set of labels. The \skiplabels macro takes an integer argument of the number of empty labels to produce. An x is printed on the terminal for each such empty label. For example:

    \documentclass{article}
    \usepackage{labels}
    \begin{document}
    \numberoflabels=19

 \boxedaddresslabel{Me \\my street \\ mytown \\ England}
 \skiplabels{2}% Make 21 on first sheet
\numberoflabels=16
 \boxedaddresslabel{You \\your street \\ yourtown \\ England}
\end{document}
179 \newcount\sk@pped
180 \def\skiplabels#1{\sk@pped=#1%
181 \savebox{\this@label}{\rule{0pt}{.5in}}%
182 \skip@labels=0\relax
183 \loop\ifnum\skip@labels<\sk@pped\make@label%
184 \advance\skip@labels by 1\relax\repeat}

185 ⟨/package⟩

4 History and acknowledgements

• (SPQR) v.1 May 9th 1989 simply allowed for \addresslabel{... \\ ...\\...}

• (SPQR) v.2 July 15th permitted verbatim style with no explicit end of lines

• (SPQR) v.3 March 1991 made more generic

• (SPQR) v.4 January 1992 checked and made to work with emtex drivers to my satisfaction, and documented to bare-bones level with ‘doc’ system.

• (SPQR) v.5 March 1993 allowed for \skiplabels

• (SPQR) v.6 January 1994 for L^{A}TEX 2ε

• (SPQR) v.7 January 1994 fixes

• (SPQR) v.8 April 1994 revised .dtx file

• (GG) v.9 April 1995 revised .dtx file to Package so it works with article,

report and book classes. Revisions by gustafson@math.utah.edu. Made

source independent of paper size (it depended on a4 paper). Fixed extra space bug in tabular array of \addresslabel - it did not reproduce results of the labels environment. Minipage lines weren’t ragged, now they are. New controls to set spaces around labels. Corrected the label placement computations. Error reporting for labels that are too big for the set dimensions. Report in millimeters. Invoked label setup at runtime. New \boxedaddresslabel[#1]{#2}. Micrometer placement of label grid on the page. Introduced empty label algorithm. Got rid of strange error messages.

• (GG) v.9 May 1995. newenvironment{labels} failed under L^{A}TEX 2.09 (1991), but worked under later versions! Defining \labels and \endlabels fixed the problem.

• (SPQR) v.10 June 1995. Checked and issued.

The crucial macros which make the system bearable for mailing lists by redefining end of line came from Phil Taylor; apologies to him for using them in a L^{A}TEX style file!