\NeedsTeXFormat{LaTeX2e}[1995/06/01]
\ProvidesClass{labels}
              [2002/03/02 v0.2]
%% $Id: labels.cls,v 1.3 2002/11/16 13:06:34 wybo Exp $
%%%% todo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% remember the position of the last label and then
% recalculate the position of a second call to lbl to absolute postion on the page
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% example:

%% \documentclass[3x8]{labels}
%% %              ^ ^
%% %              | |
%% %              | no. of rows on a sheet
%% %              no. of columns on a sheet
%% % currently available: 2x5 2x8 3x8 4x12
%% 
%% \usepackage{txfonts}
%% 
%% % the dimens \dx and \dy are the offsets of labels relative to the upper left of the
%% % available field; both are 5mm by default. The gives labels more top and
%% % left whitespace:
%% \dx=15mm\dy=10mm
%% 
%% \begin{document}
%% \sf		% choose a font
%% \footnotesize	% and a size
%% 
%% % the \Lbl command prints one label at the time, starting in the upper left:
%% \Lbl{W.H. Dekker\\Servalys\\Deilsedijk 60\\4158 CH Deil} % 308
%% \Lbl{E.H.M. Frambach\\Bruggensingel-noord 66\\3823 BC Amersfoort} % 574
%% \Lbl{F. Goddijn\\Stichting Johan Polak\\Postbus 30196\\6803 AD Arnhem} % 236
%% \Lbl{J. Hagen\\Pragma\\Ridderstraat 27,  Hasselt\\8061 GH Hasselt} % 191
%% \Lbl{T.J.G Veenker\\Faculteit der Letteren, UU\\Trans 10\\3512 JK Utrecht} % 200
%% \Lbl{J.E. van Weerden\\Utrechtsestraatweg 64\\3445 AV Woerden} % 590
%% \Lbl{K.H. Wesseling\\AMC, K2-228\\Meibergdreef 9\\1105 AZ Amsterdam} % 370
%% 
%% % The \lbl command prints one label one or more times, in a specified starting
%% % column:
%% 
%% \lbl{2}{4}{6}{\textbf{the first line in bold}\\line 2\\line3}
%% %    ^  ^  ^   ^^^^^label text with \\ newlines^^^^^
%% %    |  |  |
%% %    |  |  no. of labels to print
%% %    |  start in this column (first=1)
%% %    start on this line (first=1)
%% 
%% % With all parameters 0, the \lbl command prints as many labels as there fit on
%% % an empty page, starting at the current position:
%% \lbl{0}{0}{0}{this label will\\
%%               be printed 3x8 times\\
%%               starting at the current position\\
%%               (upper left if this is the only label printed)
%% \end{document}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}
\ProcessOptions

\LoadClass[a4paper]{letter}
\newcount\nx\newcount\ny\newdimen\hoogte\newdimen\breedte\newdimen\dx\newdimen\dy

\DeclareOption{3x8} {\nx=3\ny=8 \dx=5mm\dy=5mm \textwidth=210mm\textheight297mm\topmargin-35.4mm}
\DeclareOption{2x5} {\nx=2\ny=5 \dx=5mm\dy=5mm \textwidth=210mm\textheight297mm\topmargin-42.4mm}
\DeclareOption{2x7} {\nx=2\ny=7 \dx=45mm\dy=5mm \textwidth=150mm\textheight267mm\topmargin-32.4mm}
\DeclareOption{card} {\nx=2\ny=5 \dx=15mm\dy=11mm \textwidth=180mm\textheight275mm\topmargin-32.4mm}
\DeclareOption{4x12}{\nx=4\ny=12\dx=5mm\dy=13mm\textwidth=210mm\textheight306mm\topmargin-29.4mm}
\ExecuteOptions{3x8,a4paper}
\ProcessOptions
\AtEndOfClass{\makelabels}

\hsize=\textwidth
\newdimen\labelcolsep\labelcolsep=10mm
\hoogte=\textheight\divide\hoogte by\ny
\breedte=\textwidth\advance\breedte by -\nx\labelcolsep\divide\breedte by\nx
\advance\textwidth by-\labelcolsep
\def\headerline{}

\input repeat
\renewcommand*{\startlabels}{\labelcount\z@
  \pagestyle{empty}%
  \let\@texttop\relax
  \advance\topmargin by\dy
  \voffset\z@
  \topskip\z@
  \headsep \z@
  \headheight \z@
  \oddsidemargin -1in\advance\oddsidemargin by\dx
  \evensidemargin -1in\advance\evensidemargin by\dx
  \@colht\textheight  \@colroom\textheight \vsize\textheight
  %\baselineskip \z@
  \lineskip \z@
  \boxmaxdepth \z@
  \parindent \z@
  \relax}

\renewcommand*{\mlabel}[2]{%
  \parbox[b][\hoogte][c]{\breedte}{\strut\ignorespaces #2}%
  \hskip0pt\penalty-100\hskip\labelcolsep plus.5\labelcolsep}

\newcommand{\lbl}[4]{
  \newcount\x\x=#1\advance\x by -1\multiply\x by\nx\advance\x by -1\advance\x by #2
  \newcount\n\n=#3
  \ifnum\n=0\n=\nx\multiply\n by\ny\else\fi
  \repeat \to{\x} \do {\Lblempty} \repeat \to{\n} \do{\Lbl{#4}}
}
\newcommand{\Lbl}[1]{\begin{letter}{%
   {\ifx\headerline\empty #1\else\headerline\\\protect\rule[2ex]{\hsize}{.2pt}\\#1\fi}
  }\end{letter}}
\newcommand{\Lblempty}{\begin{letter}{}\end{letter}}
\typeout{===> labels dimensions: \the\breedte x \the\hoogte mm}
%vim: syntax=tex
