%%
%%
%% This is file `ticket.sty',
%%
%% File: ticket.sty Copyright (c) 1999-2016 Thomas Emmel
%% <thomas@family-emmel.de>
%%
%%
% This package may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% This package consists of the file ticket.sty 
%%
% HISTORY:
%
%  v0.1a-0.1d non-official implementations 
%
%  v0.2       fixing some minor bugs and add examples
%
%  v0.3a      checking for an old calc-package
%  v0.3b      use internal values if tdf-file is not specified
%  v0.3c      \ticketreset added.
%  v0.3d      added option emptycrossmark as provided by
%             Rafal Bielski <rafal dot bielski at dhl dot com>
%             Similar option was provided by
%             Ivan Lloro Boada <antispam at wanadoo dot es>
%
%  v0.4a      added the flashcard-style invented by
%             Santiago Mejia <san_tico at yahoo dot com>
%  v0.4b      added 'rowmode' introduced by Wulf Coulmann 
%             <wulf at coulmann dot de>
%  v0.4c      added \ticketskip to allow for empty tickets
%             at the start such that you can re-use used sheets
%             by Maximilian Appel <mail at maxappel dot de>
%  v0.4d      lost some examples in last package
%             no new features
%
%             Many thanks for all contributions
%              
%
\def\fileversion{v0.4d}
\def\filedate{2016/10/11}

\ProvidesPackage{ticket}[\filedate\space\fileversion]
\typeout{Package: `ticket' \fileversion\space <\filedate> (Thomas Emmel)}

\newif\if@crossmark      \@crossmarkfalse
\newif\if@circlemark     \@circlemarkfalse
\newif\if@emptycrossmark \@emptycrossmarkfalse
\newif\if@cutmark        \@cutmarkfalse
\newif\if@boxed          \@boxedfalse
\newif\if@extern         \@externfalse
\newif\if@backside       \@backsidefalse
\newif\if@rowmode        \@rowmodefalse

\DeclareOption{boxed}{\@boxedtrue}
\DeclareOption{crossmark}{\@crossmarktrue}
\DeclareOption{circlemark}{\@circlemarktrue}
\DeclareOption{emptycrossmark}{\@emptycrossmarktrue}
\DeclareOption{cutmark}{\@cutmarktrue}
\DeclareOption{rowmode}{\@rowmodetrue}

\DeclareOption*{\edef\ticketToUse{\CurrentOption.tdf}\@externtrue}%

\ProcessOptions
%
\RequirePackage{ifthen}
\RequirePackage{calc}
% older calc packages have a small bug
\@ifpackagelater{calc}{1998/07/06}%
      {% Yes. OK! 
       }
      {% No. 
        \PackageWarning{ticket}
        {Package calc too old (older than v4.1b).}%
        }%

\pagestyle{empty}

% standard definitions
\topmargin=0pt
\headheight=0pt
\headsep=0pt
\oddsidemargin=0pt
\evensidemargin=0pt
\footskip=0pt
\voffset=0pt
\hoffset=0pt
\unitlength=1mm

%
\newcounter{ticket@x}
\newcounter{ticket@y}
\setcounter{ticket@x}{1}
\setcounter{ticket@y}{1}

\newcounter{ticket@num@X}% Number of tickets in horizontal direction
\newcounter{ticket@num@Y}% Number of tickets in vertical direction
\setcounter{ticket@num@X}{2}% default value
\setcounter{ticket@num@Y}{3}% default value

\def\ticketNumbers#1#2{\setcounter{ticket@num@X}{#1}%
                       \setcounter{ticket@num@Y}{#2}}
%

\newdimen\ticketWidth% width of a ticket
\newdimen\ticketHeight% height of a ticket

\newdimen\ticket@dis@X% horizontal space between tickets
\newdimen\ticket@dis@Y% vertical space between tickets
\ticket@dis@X=0mm% default value
\ticket@dis@Y=0mm% default value

\def\ticketSize#1#2{\gdef\ticket@size@X{#1}\ticketWidth=#1\unitlength%
                    \gdef\ticket@size@Y{#2}\ticketHeight=#2\unitlength}

\def\ticketDistance#1#2{\ticket@dis@X=#1\unitlength%
                        \ticket@dis@Y=#2\unitlength}

%% now load ticket definitions from file
\if@extern
\InputIfFileExists{\ticketToUse}
  {\typeout{load \ticketToUse}}
  {\errmessage{\ticketToUse\space not found! Please use another file}}
\else
  \unitlength=1mm
  \hoffset=0mm
  \voffset=0mm
  \ticketNumbers{2}{2}
  \ticketSize{50}{20} 
  \ticketDistance{5}{10}
\fi

%% temporary length
\newdimen\ticket@shift@x
\newdimen\ticket@shift@y
\newdimen\back@shift

\setlength{\back@shift}{(\ticketWidth+\ticket@dis@X)*(\theticket@num@X-1)}%

%% a restart
\def\ticketreset{%
  \setcounter{ticket@x}{1}\setcounter{ticket@y}{1}%
  \if@backside%
    \setlength{\ticket@shift@y}{0pt}%
    \setlength{\ticket@shift@x}{\back@shift}%
  \else%
    \setlength{\ticket@shift@y}{0pt}%
    \setlength{\ticket@shift@x}{0pt}%
  \fi}

%% backsides should be handled different for flashcards
\def\backside{%
  \@backsidetrue%
  \ticketreset%
  \clearpage}%

% the heart: a "counter"
\newcommand{\ticket@counter}{%
  \if@rowmode%
    \ifthenelse{\value{ticket@x}<\value{ticket@num@X}}%
      {\stepcounter{ticket@x}\\[-\baselineskip]}%
      % otherwise:
      {\setcounter{ticket@x}{1}%
        \ifthenelse{\value{ticket@y}<\value{ticket@num@Y}}%
          {\stepcounter{ticket@y}\\[-\baselineskip]}%
          % otherwise:
          {\setcounter{ticket@y}{1}\setcounter{ticket@x}{1}\newpage}}%
        \setlength{\ticket@shift@y}{(\ticketHeight+\ticket@dis@Y)*(\theticket@y-1)}%
        \if@backside%
          \setlength{\ticket@shift@x}{\back@shift-(\ticketWidth+\ticket@dis@X)*(\theticket@x-1)}%
        \else%
          \setlength{\ticket@shift@x}{(\ticketWidth+\ticket@dis@X)*(\theticket@x-1)}%
        \fi%
  \else%
    \ifthenelse{\value{ticket@y}<\value{ticket@num@Y}}%
      {\stepcounter{ticket@y}\\[-\baselineskip]}%
      % otherwise:
      {\setcounter{ticket@y}{1}%
        \ifthenelse{\value{ticket@x}<\value{ticket@num@X}}%
          {\stepcounter{ticket@x}\\[-\baselineskip]}%
          % otherwise:
          {\setcounter{ticket@x}{1}\setcounter{ticket@y}{1}\newpage}}%
        \setlength{\ticket@shift@y}{(\ticketHeight+\ticket@dis@Y)*(\theticket@y-1)}%
        \if@backside%
          \setlength{\ticket@shift@x}{\back@shift-(\ticketWidth+\ticket@dis@X)*(\theticket@x-1)}%
        \else%
          \setlength{\ticket@shift@x}{(\ticketWidth+\ticket@dis@X)*(\theticket@x-1)}%
        \fi%
   \fi}%

% by Maximilian Appel 2016
\newcounter{ticket@skipped}
\newcommand{\ticketskip}[1]{
    \setcounter{ticket@skipped}{0}
    \ % space needed to avoid "there is no line here to end" error
    \whiledo{\theticket@skipped<#1}{\stepcounter{ticket@skipped}\ticket@counter}
}
%
   
% default content...
% users should renew this command
\newcommand{\ticketdefault}{%
%
\put( 5, 5){Ticket....}%
}%

% the main command: \ticket 
% users should define a command like \myticket to get more than one parameter
\newcommand{\ticket}[1]{%
% position global
\noindent%
\raisebox{-\ticket@shift@y}[0mm][0mm]{%
\raisebox{-\ticketHeight}[0mm][0mm]{%
\hspace*{\ticket@shift@x}%
%
% open a picture of zero size and place some crosses
\begin{picture}(0,0)%
\if@crossmark%
  \put(0,0){\ticket@cross}%
  \put(0,\ticket@size@Y){\ticket@cross}%
  \put(\ticket@size@X,0){\ticket@cross}%
  \put(\ticket@size@X,\ticket@size@Y){\ticket@cross}%
\fi%
\if@circlemark%
  \put(0,0){\circle{1}}%
  \put(0,\ticket@size@Y){\circle{1}}%
  \put(\ticket@size@X,0){\circle{1}}%
  \put(\ticket@size@X,\ticket@size@Y){\circle{1}}%
\fi%
\if@boxed%
  \put(0,0){\line(1,0){\ticket@size@X}}%
  \put(0,0){\line(0,1){\ticket@size@Y}}%
  \put(\ticket@size@X,\ticket@size@Y){\line(-1,0){\ticket@size@X}}%
  \put(\ticket@size@X,\ticket@size@Y){\line(0,-1){\ticket@size@Y}}%
\fi%
\if@emptycrossmark%
  \put(0,0){\begin{picture}(0,0)%
      \put(-1,0){\line(-1,0){1}}%
      \put(0,-1){\line(0,-1){1}}\end{picture}}%
  \put(0,\ticket@size@Y){\begin{picture}(0,0)%
      \put(-1,0){\line(-1,0){1}}%
      \put(0,1){\line(0,1){1}}\end{picture}}%
  \put(\ticket@size@X,0){\begin{picture}(0,0)%
      \put(1,0){\line(1,0){1}}%
      \put(0,-1){\line(0,-1){1}}\end{picture}}%
  \put(\ticket@size@X,\ticket@size@Y){\begin{picture}(0,0)%
      \put(1,0){\line(1,0){1}}%
      \put(0,1){\line(0,1){1}}\end{picture}}%
\fi%
\if@cutmark%
  \ifthenelse{\value{ticket@x}=1}%
   {\put(-1,0){\line(-1,0){1}}\put(-1,\ticket@size@Y){\line(-1,0){1}}}{}%
  \ifthenelse{\value{ticket@y}=1}%
   {\put(0,\ticket@size@Y){\begin{picture}(0,0)%
      \put(0,1){\line(0,1){1}}\end{picture}}%
    \put(\ticket@size@X,\ticket@size@Y){\begin{picture}(0,0)%
      \put(0,1){\line(0,1){1}}\end{picture}}}{}%
  \ifthenelse{\value{ticket@x}=\value{ticket@num@X}}%
   {\put(\ticket@size@X,0){\begin{picture}(0,0)%
      \put(1,0){\line(1,0){1}}\end{picture}}%
    \put(\ticket@size@X,\ticket@size@Y){\begin{picture}(0,0)%
      \put(1,0){\line(1,0){1}}\end{picture}}}{}%
  \ifthenelse{\value{ticket@y}=\value{ticket@num@Y}}%
   {\put(0,-1){\line(0,-1){1}}\put(\ticket@size@X,-1){\line(0,-1){1}}}{}%
\fi%
% put the default content into the ticket
\ticketdefault%
% put the variable content into the ticket
#1%
\end{picture}}}\ticket@counter}%
%
\def\ticket@cross{%
  \begin{picture}(0,0)%
    \put(-1,0){\line(1,0){2}}\put(0,-1){\line(0,1){2}}%
    \end{picture}}%
%
\endinput