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

Sebastian Rahtz, Leonor Barroca  Grant Gustafson

Julian Gilbey

2003/05/22

Contents

1 Generating Style Files and Documentation 1

2 Usage 2 2.1 Paper Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.2 Label Stock Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.3 Customizing for Other Labels: the traditional method . . . . . . . 3 2.4 Customizing for Other Labels: the new method . . . . . . . . . . . 4 2.5 Using an External Label source File . . . . . . . . . . . . . . . . . 5 2.6 Labels in the Main File . . . . . . . . . . . . . . . . . . . . . . . . 7 2.7 Control Sequences to Make Labels . . . . . . . . . . . . . . . . . . 7 2.8 Wrapping Long Lines and Debugging . . . . . . . . . . . . . . . . . 8

3 The macros 9

4 User macros 15

5 History and acknowledgements 18

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 Generating Style Files and Documentation

Automatic generation of the style files uses the command line

                       latex labels.ins

∗This file has version number v.13?, last revised 2003/05/22.

to create the files labels.sty (for L^{A}TEX2e) and olabels.sty (for L^{A}TEX209). Usage in the preamble section of a L^{A}TEX source file is \input{olabels.sty} for L^{A}TEX209 and \usepackage{labels} for L^{A}TEX2e. Backward compatibility with L^{A}TEX209 may be removed suddenly in some future release. Please observe that documentation is created by the command line latex labels.dtx, and no provision is made to produce documentation via L^{A}TEX209.

2 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 task 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.

2.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.

2.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!

2.3 Customizing for Other Labels: the traditional method

It is very likely that the label stock will be different than Avery 5360. Generally, it is necessary to tailor the 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. Personal adjustments can be made for the amount of white space around each label.

The 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 the printer driver parameters.

A common problem with printers is that label output is extra wide, requiring printing onto the very edges of the paper. Some printers may not be able to maintain print quality all the way to the edges of the paper. Adjust the parameters

\LeftBorder, \RightBorder, \TopBorder, \BottomBorder as outlined below to

solve this 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 optional 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 pass through the xerox machine automatically.

2.4 Customizing for Other Labels: the new method

One of the deficiencies of the method described above it that it is unable to cope well with labels having spaces between them, and the dimensions specified are not the natural ones which one would want to measure. If the package option newdimens is given (and this only works for users of L^{A}TEX 2ε), then we can give page parameters in a far more flexible manner as follows, where these dimensions are suitable for Avery L7160 labels:

   \documentclass[a4paper,12pt]{article}
   \usepackage[newdimens]{labels}
   \LabelCols=3%             Number of columns of labels per page
   \LabelRows=7%             Number of rows of labels per page
   \LeftPageMargin=7mm%      These four parameters give the
   \RightPageMargin=7mm%       page gutter sizes.  The outer edges of
   \TopPageMargin=15mm%        the outer labels are the specified
   \BottomPageMargin=15mm%     distances from the edge of the paper.
   \InterLabelColumn=2mm%    Gap between columns of labels
   \InterLabelRow=0mm%       Gap between rows of labels
   \LeftLabelBorder=5mm%     These four parameters give the extra
   \RightLabelBorder=5mm%      space used around the text on each
   \TopLabelBorder=5mm%        actual label.
   \BottomLabelBorder=5mm%
   \begin{document}%         End of preamble

(The \LabelSetup form can also be used, as above.) Thus the four \...PageMargin parameters and the \InterLabel... paramenters define the location of the physical labels on the page, and the four \...LabelBorder parameters define how the space on each label is to be used.

As before, the 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 the printer driver parameters.

We now describe how the locations of the labels are determined, as we did before. The label height and label width are given by the formulæ:

     LabelRows × Label height + (LabelRows − 1) × InterLabelRow =
         paperheight − TopPageMargin − BottomPageMargin

and

 LabelColumns × Label width + (LabelColumns − 1) × InterLabelColumn =
 paperwidth − LeftPageMargin − RightPageMargin

Then, within each label, the label text is vertically centered in a box which is

indented \TopLabelBorder from the top of the label, \BottomLabelBorder from
the bottom, \LeftLabelBorder from the left and \RightLabelBorder from the

right.

   It is an error if TopLabelBorder + BottomLabelBorder ≥ Label height, and

similarly for the width; the results in such cases may be unpredictable.

Note that the \LabelInfoTrue function only draws the outlines of the physical labels; see below for information on \LabelGridtrue.

2.5 Using an External Label source File

The simplest form of input is very short, 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 line 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. The formfeed character Ctrl-L seems to be acceptable in the source. A formfeed is not ignored, even if it 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 long lines are 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 characters below may be used in a source file for writing L^{A}TEX code. Generally, a raw source has to be manually stripped of these characters.

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. For some emacs versions, the first line of the marked block has to be empty, in order to produce a successful sort.

General purpose L^{A}TEX Source

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

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

2.6 Labels in the Main File

The names and addresses can appear directly 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}

2.7 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\sffamily]
      {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]
           {\textbf{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
           \emph{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.

2.8 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.

3 The macros

First of all, identify the package start, and specify that we recognise the newdimens option. The \iflabel@traddimens macro will record whether we are using the tradition labels dimension system or not.

1 ⟨∗package⟩

 2 ⟨!latex209⟩\NeedsTeXFormat{LaTeX2e}
 3 ⟨!latex209⟩\ProvidesPackage{labels}[2003/05/22 v.13]

4 ⟨∗latex209⟩

 5 \newdimen\paperwidth
 6 \paperwidth=8.5in% 297mm for a4paper
 7 \newdimen\paperheight
 8 \paperheight=11in% 210mm for a4paper
 9 \def\settoheight#1#2{\setbox\@tempboxa%
10 \hbox{#2}#1\ht\@tempboxa\setbox\@tempboxa\box\voidb@x}
11 \def\PackageWarning#1#2{\typeout{#1: #2}}

12 ⟨/latex209⟩

13 \newif\iflabel@traddimens
14 \label@traddimenstrue
15 ⟨!latex209⟩\DeclareOption{newdimens}{\label@traddimensfalse}
16 ⟨!latex209⟩\ProcessOptions

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

17 \newcount\LabelCols
18 \newcount\LabelRows
19 \iflabel@traddimens
20 \newdimen\LeftBorder
21 \newdimen\RightBorder
22 \newdimen\TopBorder
23 \newdimen\BottomBorder
24 \else
25 \newdimen\LeftPageMargin
26 \newdimen\RightPageMargin
27 \newdimen\TopPageMargin
28 \newdimen\BottomPageMargin
29 \newdimen\InterLabelColumn
30 \newdimen\InterLabelRow
31 \newdimen\LeftLabelBorder
32 \newdimen\RightLabelBorder
33 \newdimen\TopLabelBorder
34 \newdimen\BottomLabelBorder
35 \fi
36 \newcount\numberoflabels
37 \newdimen\label@width
38 \newdimen\label@height
39 \newdimen\area@width

40 \newdimen\area@height
41 \newdimen\half@label
42 \newdimen\half@area
43 \newdimen\addr@width
44 \newdimen\LabTmp
45 \newsavebox\this@label
46 \newcount\label@number
47 \newcount\skip@labels
48 \newcount\l@so@far
49 \newcount\LabelTotal
50 \newif\ifLabelGrid
51 \newif\iffirst@label
52 \newif\ifLabelInfo
53 \first@labeltrue
54 \LabelGridfalse
55 \LabelInfofalse

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

56 \ifdim\paperwidth=210mm\relax%
57 \LabelCols=3\relax\LabelRows=8\relax% a4paper
58 \else
59 \LabelCols=3\relax\LabelRows=7\relax% letterpaper
60 \fi%

These 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. Different defaults are necessary if we are using newdimens. A LaserJetIII seems to ignore about 8mm on the edges. Xerox machines ignore even more, on all sides.

61 \iflabel@traddimens
62 \LeftBorder=8mm
63 \RightBorder=8mm
64 \TopBorder=9mm
65 \BottomBorder=2mm
66 \else
67 \LeftPageMargin=4mm
68 \RightPageMargin=4mm
69 \TopPageMargin=5mm
70 \BottomPageMargin=5mm
71 \InterLabelColumn=0mm
72 \InterLabelRow=0mm
73 \LeftLabelBorder=5mm
74 \RightLabelBorder=5mm
75 \TopLabelBorder=4mm
76 \BottomLabelBorder=4mm
77 \fi

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.

78 \textwidth=\paperwidth
79 \textheight=\paperheight
80 \topmargin=-1in

81 \headheight=0em
82 \headsep=0em
83 \topskip=0em
84 \footskip=0em
85 \oddsidemargin=-1in
86 \evensidemargin=-1in
87 \pagestyle{empty}
88 \parindent=0em
89 \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.

We have two versions of this macro, depending on whether the newdimens option has been set.

90 \iflabel@traddimens
91 \def\@LabelSetup{%
92 \global\label@width\textwidth
93 \global\divide\label@width by\LabelCols
94 \global\label@height\textheight
95 \global\advance\label@height by-\TopBorder
96 \global\advance\label@height by-\BottomBorder
97 \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.

98 \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.

99 \global\area@width=\label@width
100 \global\advance\area@width by -\LeftBorder
101 \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.

102 \ifLabelGrid%
103 \global\advance\area@width by-2\fboxsep
104 \global\advance\area@width by-2\fboxrule
105 \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.

106 \ifLabelGrid
107 \global\advance\label@height by-2\fboxsep
108 \global\advance\label@height by-2\fboxrule
109 \fi

110 \global\half@label=\label@height\divide\half@label by2\relax
111 \global\label@number=1\relax

112 }

Now we handle the newdimens variant. The same general scheme applies, but the calculations are different.

113 \else
114 \def\@LabelSetup{%
115 \global\label@width\textwidth
116 \global\advance\label@width by-\LeftPageMargin
117 \global\advance\label@width by-\RightPageMargin
118 \global\advance\label@width by-\LabelCols\InterLabelColumn
119 \global\advance\label@width by\InterLabelColumn
120 \global\divide\label@width by\LabelCols
121 \ifdim\label@width<0pt
122 \PackageWarning{labels}{Some dimensions are silly: label width
123 \the\label@width~is negative!}
124 \label@width=0pt
125 \fi
126 \global\label@height\textheight
127 \global\advance\label@height by-\TopPageMargin
128 \global\advance\label@height by-\BottomPageMargin
129 \global\advance\label@height by-\LabelRows\InterLabelRow
130 \global\advance\label@height by\InterLabelRow
131 \global\divide\label@height by\LabelRows
132 \ifdim\label@height<0pt
133 \PackageWarning{labels}{Some dimensions are silly: label height
134 \the\label@height~is negative!}
135 \label@height=0pt
136 \fi

We now adjust the \...margin parameters to take account of \TopPageMargin

 and \LeftPageMargin.
137 \global\topmargin=-1in\global\advance\topmargin by\TopPageMargin
138 \global\oddsidemargin=-1in\global\advance\oddsidemargin by\LeftPageMargin
139 \global\evensidemargin=\oddsidemargin

Again, \area@width gives the area that will actually be used for printing; the

 width is cut down by \LeftLabelBorder plus \RightLabelBorder. These dimen-

sions can be set to zero if you have a design that uses the whole label. Similarly, \area@height gives the height of the printing area, \half@label gives half of the label height and \half@area gives half of the printable area height.

140 \global\area@width=\label@width
141 \global\advance\area@width by -\LeftLabelBorder
142 \global\advance\area@width by -\RightLabelBorder
143 \global\area@height=\label@height
144 \global\advance\area@height by -\TopLabelBorder
145 \global\advance\area@height by -\BottomLabelBorder
146 \global\half@label=\label@height\divide\half@label by 2\relax
147 \global\half@area=\area@height\divide\half@area by 2\relax
148 \global\label@number=1\relax

149 }

150 \fi

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.

151 \def\LabelSetup{\@LabelSetup
152 \ifLabelInfo
153 \typeout{Control sequences adjustable in the preamble:}
154 \typeout{\LabelRows=\the\LabelRows}
155 \typeout{\LabelCols=\the\LabelCols}
156 \iflabel@traddimens
157  \typeout{The newdimens option was not selected; the used parameters are:}
158  \TypeoutBlurb{\TopBorder}{\TopBorder}
159  \TypeoutBlurb{\BottomBorder}{\BottomBorder}
160  \TypeoutBlurb{\LeftBorder}{\LeftBorder}
161  \TypeoutBlurb{\RightBorder}{\RightBorder}
162 \else
163  \typeout{The newdimens option was selected; the used parameters are:}
164  \TypeoutBlurb{\TopPageMargin}{\TopPageMargin}
165  \TypeoutBlurb{\BottomPageMargin}{\BottomPageMargin}
166  \TypeoutBlurb{\LeftPageMargin}{\LeftPageMargin}
167  \TypeoutBlurb{\RightPageMargin}{\RightPageMargin}
168  \TypeoutBlurb{\InterLabelColumn}{\InterLabelColumn}
169  \TypeoutBlurb{\InterLabelRow}{\InterLabelRow}
170  \TypeoutBlurb{\TopLabelBorder}{\TopLabelBorder}
171  \TypeoutBlurb{\BottomLabelBorder}{\BottomLabelBorder}
172  \TypeoutBlurb{\LeftLabelBorder}{\LeftLabelBorder}
173  \TypeoutBlurb{\RightLabelBorder}{\RightLabelBorder}
174 \fi
175 \typeout{Computed values:}
176 \TypeoutBlurb{Label Width}{\label@width}
177 \TypeoutBlurb{Label Height}{\label@height}
178 \else
179 \typeout{To print info, put `\protect\LabelInfotrue' in the preamble}
180 \fi

181 }

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

182 \numberoflabels=1%
183 \def\sticky@label{\l@so@far=0%
184 \loop\ifnum\l@so@far<\numberoflabels\advance\l@so@far by 1\make@label%
185 \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.

186 \def\make@label{%
187 \ifnum\LabelTotal=0\vfill\eject\LabelTotal=\LabelRows\relax
188 \multiply\LabelTotal by \LabelCols\fi
189 \advance\LabelTotal by -1\relax
190 \ifLabelGrid
191 \let\boxing@type\framebox

192 \else
193 \let\boxing@type\makebox
194 \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.

195 \settowidth{\LabTmp}{\usebox{\this@label}}%
196 \ifdim\LabTmp=0cm\let\action=\relax\else\let\action=\BuildB@x\fi
197 %\TypeoutBlurb{Box Width}{\LabTmp}%
198 \advance\LabTmp by -\area@width
199 \ifdim\LabTmp>0cm\relax
200 \PkgBlurb{Label too wide}{\LabTmp}\fi
201 \settoheight{\LabTmp}%
202 {\begin{tabular}{l}\usebox{\this@label}\end{tabular}}%
203 %\TypeoutBlurb{Box height}{\LabTmp}%
204 \iflabel@traddimens\advance\LabTmp by -\half@label
205 \else\advance\LabTmp by -\half@area\fi
206 \ifdim\LabTmp>0cm\relax
207 \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, it’s empty and we discard it.

208 \ifnum\skip@labels<\sk@pped\let\action=\BuildB@x\fi
209 \action

210 }

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.

211 \def\BuildB@x{%
212 \iflabel@traddimens
213 \boxing@type[\label@width][c]{%
214 \rule{0pt}{\label@height}%
215 \raisebox{\half@label}[0pt][0pt]{%
216 \rule{\LeftBorder}{0pt}%
217 \usebox{\this@label}%
218 \rule{\RightBorder}{0pt}%
219 }}%

We have a slightly different system when we are using the new dimension system. We vertically center the label in the printing area and indent in by the value of \LeftLabelBorder. We also mess with the value of \fboxsep to make a framed box which doesn’t interfere with the placing of the label text.

220 \else
221 \begingroup
222 \fboxsep=-\fboxrule
223 \boxing@type{%
224 \vbox to \label@height{%

225 \vskip\TopLabelBorder
226 \vss
227 \hbox to \label@width{%
228  \hskip\LeftLabelBorder
229  \usebox{\this@label}%
230  \hss

231 }%

232 \vss
233 \vskip\BottomLabelBorder

234 }% 235 }%

236 \endgroup
237 \fi

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.
238 \ifnum\skip@labels<\sk@pped\message{x}\else\message{+}\fi
239 \ifnum\label@number=\LabelCols
240 \message{|}%
241 \endgraf\nointerlineskip
242 \iflabel@traddimens\else\vskip\InterLabelRow\fi
243 \global\label@number=1
244 \else\global\advance\label@number by 1
245 \iflabel@traddimens\else\hskip\InterLabelColumn\fi
246 \fi

247 }

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.

248 \newcount\@Milli
249 \def\ToMilli#1{\@Milli=#1\advance\@Milli by93225\relax
250 \divide\@Milli by 186450\relax}
251 \def\PkgBlurb#1#2{\ToMilli{#2}%
252 \PackageWarning{labels}{#1 by \the\@Milli mm (\the #2)}%

253 }

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

256 }

4 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.

257 \newcommand{\genericlabel}[1]{%
258 \iffirst@label\LabelSetup\first@labelfalse\fi%
259 \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.

260 \long\def\L@dblarg#1{\@ifnextchar[{#1}{\L@xdblarg{#1}}}
261 \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:

262 \long\def\addresslabel{\L@dblarg{\@addresslabel}}
263 \long\def\@addresslabel[#1]#2{\genericlabel{#1%
264 \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.

265 \long\def\boxedaddresslabel{\L@dblarg{\@boxedaddresslabel}}
266 \long\def\@boxedaddresslabel[#1]#2{\genericlabel{#1%
267 \addr@width=\area@width\advance\addr@width by-2\fboxsep%
268 \advance\addr@width by-2\fboxrule\fbox{%
269 \begin{tabular}{@{}p{\addr@width}@{}}\raggedright #2\end{tabular}}}}

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

270 \long\def\labels
271 {\iffirst@label\LabelSetup\first@labelfalse\fi\start@@label}
272 \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. A technical point: we cannot use \sbox, as otherwise TEX gets confused, attempting to use \bgroup as the second argument to \sbox, which is not what we want.

273 \def\start@@label{%
274 \begin{lrbox}{\this@label}%
275 \begin{minipage}{\area@width}\raggedright%
276 \catcode `\^^M =\active\@gobblecr%

277 }

278 \def\end@@label{%
279 \end{minipage}\end{lrbox}%
280 \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.

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

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

282 \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.

283 \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.

284 \def ^^M{\futurelet\nexttoken\isitapar}%
285 \def\isitapar{\ifx^^M\nexttoken\let\action=\new@label\else%
286 \let\action\start@newline\fi\action}%

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

287 \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.

288 \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.

289 \def\promptlabels{\typein[\labelfilename]{What is the name of the
290 label file?}
291 \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}
292 \newcount\sk@pped
293 \def\skiplabels#1{\sk@pped=#1%
294 \savebox{\this@label}{\rule{0pt}{.5in}}%
295 \skip@labels=0\relax
296 \loop\ifnum\skip@labels<\sk@pped\make@label%
297 \advance\skip@labels by 1\relax\repeat}

298 ⟨/package⟩

5 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.

• (GG) v.11, January 1998. New counter \LabelTotal and page eject when this counter is zero. Beta version April 1996, released January 1998. Documented L^{A}TEX209 style file olabels.sty.

• (JDG) v.12, January 2002. Introduced more natural dimension description system, which is activated by the ‘newdimens’ package option. This is only implemented for the L^{A}TEX 2ε version.

• (JDG) v.13, May 2003. Bug fix release: don’t break with the color package (thanks to Dominique de Waleffe for reporting it)

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!