%% This is jlabels.sty written by Jamal K. Tartir.
%%
%% jktartir@hotmail.com
%%
%% A latex package designed to make letter-sized pages of labels.
%%
%% Copyright 2011 by Jamal K. Tartir.
%%
%% All rights reserved.
%%
%% This file may be modified for individual use.
%% 
%% A modified version of this file my not be
%% distributed under its original name.

%% Alternately, this work may be distributed and/or modified
%% under the conditions of the LaTeX Project Public License.

%%%%%     PACKAGES     %%%%%

\RequirePackage{calc}
\RequirePackage{ifthen}
\RequirePackage{pstricks}
\RequirePackage{etoolbox}

%%%%%     NEW LENGTHS     %%%%%

\newlength{\jlabelHorizontalPitch}
\newlength{\jlabelVerticalPitch}

%%%%%     COUNTERS     %%%%%

\newcounter{jlabelxcoordinate}
\newcounter{jlabelycoordinate}
\newcounter{jlabelNumberOfRowsPerPage}
\newcounter{jlabelNumberOfColumnsPerPage}
\newcounter{jlabelNumberOfLabelsPerPage}
\newcounter{jlabelNumberOfLabelsOnThisPage}
\newcounter{jlabelNumberOfTimesThisLabel}

%%%%%     DEFINITIONS     %%%%%

\def\jlabeljustify{l}

\def\jlabelfillstylec{%
\gdef\setjlabelcoordinates{%
\setcounter{jlabelxcoordinate}{%
\value{jlabelNumberOfLabelsOnThisPage}/\value{jlabelNumberOfRowsPerPage}}%
\setcounter{jlabelycoordinate}{%
(\value{jlabelxcoordinate}*\value{jlabelNumberOfRowsPerPage})
-\value{jlabelNumberOfLabelsOnThisPage}}}}

\def\jlabelfillstyler{%
\gdef\setjlabelcoordinates{%
\setcounter{jlabelycoordinate}{%
-\value{jlabelNumberOfLabelsOnThisPage}/\value{jlabelNumberOfColumnsPerPage}}
\setcounter{jlabelxcoordinate}{%
(\value{jlabelycoordinate}*\value{jlabelNumberOfColumnsPerPage})
+\value{jlabelNumberOfLabelsOnThisPage}}}}

%%%%%     COMMANDS     %%%%%

\newcommand{\jlabelLengths}[9][1in]{%
%% #1: Units
%% #2: Left Margin
%% #3: Top Margin
%% #4: Label Width
%% #5: Label Height
%% #6: Number of Rows
%% #7: Number of Columns
%% #8: Horizontal Gap
%% #9: Vertical Gap
%
\setlength{\voffset}{-1in}
\setlength{\hoffset}{-1in}
\setlength{\textwidth}{8in}
\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{0pt}
\setlength{\headheight}{0pt}
\setlength{\marginparsep}{0pt}
\setlength{\footskip}{0pt}
\setlength{\topmargin}{0pt}
\setlength{\headsep}{0pt}
\setlength{\marginparwidth}{0pt}
\setlength{\marginparpush}{0pt}
%
\setlength{\unitlength}{#1}%
%
\setlength{\jlabelHorizontalPitch}{#4\unitlength}
\setlength{\jlabelVerticalPitch}{#5\unitlength}
%
\addtolength{\hoffset}{#2\unitlength}
\addtolength{\hoffset}{.5\jlabelHorizontalPitch}
\setlength{\textheight}{#3\unitlength}
\addtolength{\textheight}{.5\jlabelVerticalPitch}
\addtolength{\jlabelHorizontalPitch}{#8\unitlength}
\addtolength{\jlabelVerticalPitch}{#9\unitlength}
%
\setcounter{jlabelNumberOfRowsPerPage}{#6}
\setcounter{jlabelNumberOfColumnsPerPage}{#7}
\setcounter{jlabelNumberOfLabelsPerPage}{#6*#7}
%
\psset{xunit=\jlabelHorizontalPitch}
\psset{yunit=\jlabelVerticalPitch}}

\newcommand{\beginjlabels}[1][c]{%
\csexpandonce{jlabelfillstyle#1}%
\xdef\thisjlabelfillstyle{#1}%
\thispagestyle{empty}%

\setcounter{jlabelNumberOfLabelsOnThisPage}{0}

\text{ }

\vfill

\noindent%
\begin{pspicture}(0,0)}

\def\endjlabels{\end{pspicture}}

\newcommand{\jlabelt}[2][1]{%
%
\setcounter{jlabelNumberOfTimesThisLabel}{0}%
\whiledo{\value{jlabelNumberOfTimesThisLabel}<#1}{%
%
\ifthenelse{%
\value{jlabelNumberOfLabelsOnThisPage}<\value{jlabelNumberOfLabelsPerPage}}%
{}{\endjlabels\newpage\beginjlabels[\thisjlabelfillstyle]}%
%
\setjlabelcoordinates%
%
\rput(\value{jlabelxcoordinate},\value{jlabelycoordinate})%
{\begin{tabular}{\jlabeljustify}#2\end{tabular}}%
%
\addtocounter{jlabelNumberOfLabelsOnThisPage}{1}%
%
\addtocounter{jlabelNumberOfTimesThisLabel}{1}}%%      end whiledo
}

\newcommand{\myjlabel}[2]{\csgdef{myjlabeladdress#1}{#2}}

\newcommand{\jlabel}[2][1]{\jlabelt[#1]{\csexpandonce{myjlabeladdress#2}}}