% card.sty by R. Potu\v cek jr.                                  March 6, 1992 %
%                                                                              %
\message{Card style Version 2.04, March 6, 1992 by R. Potu\v cek jr.}          %
%                                                                              %
% This style allows you to print text formatted for cardfile cards. As it      %
% is a problem to print on such cards anyway, the text ist formatted into      %
% groups of consecutive boxes, so as to allow cutting it out and glueing       %
% together to cardfiles.                                                       %
%                                                                              %
% Card size can be manipulated at will and card pages will be enumerated       %
% optionally and independently for each card, so as to allow texts longer      %
% than one card.                                                               %
%                                                                              %
% The style should work with plain \TeX\ as well es with \LaTeX.               %
%                                                                              %
%                                                                              %
%                                                                              %
%                                                                              %
% If you have any suggestions, questions or comments, please e-mail (with a    %
% Version number please) to:                                                   %
%                                                                              %
%   potucek@winx06.informatik.uni-wuerzburg.de                                 %
%                                                                              %
% ---------------------------------------------------------------------------- %
%                                                                              %
% For use with \LaTeX\ use the environment `card', which expects a parameter   %
% containing material supposed to be set in the header:                        %
%                                                                              %
% \begin{card}{heading}                                                        %
%   ...                                                                        %
% \end{card}                                                                   %
%                                                                              %
% For use with plain \TeX\�enclose the actual text between `\card' and         %
% `\endcard':                                                                  %
%                                                                              %
% \card{heading}                                                               %
%   ...                                                                        %
% \endcard                                                                     %
%                                                                              %
% ---------------------------------------------------------------------------- %
%                                                                              %
% The text within the environment will be formatted as follows:                %
%                                                                              %
%    +---------------------------+---------------------------+                 %
%    | heading              page | heading              page |                 %
%    +---------------------------+---------------------------+                 %
%    | Text, broken into small   | allow usage of all other  |                 %
%    | pages of their own, so as | \LateX\ commands.         |                 %
%    | to allow comfortable usage|                           |                 %
%    | as an environment, an to  |                           |                 %
%    +---------------------------+---------------------------+                 %
%                                                                              %
% ---------------------------------------------------------------------------- %
%                                                                              %
% The size of the card is set by \cardheight and \cardwidth. The distance of   %
% the text from the borders is set by \cardfreespace. Pagenubers can be        %
% selected by setting:                                                         %
%                                                                              %
%   \let\cardpageno\on          % turn on pagenumbers                          %
%   \let\cardpageno\off         % turn off pagenumbers                         %
%                                                                              %
% Before each card and between two boxes \cardfreespace will be inserted.      %
% Furthermore there is a token register called `\everycard' that is executed   %
% at the beginning of every card, so as to allow a general style definition    %
% for every card, which then may be dependent on the environment you use.      %
%                                                                              %
% If you don't like empty pages to be printed, i.e. pages that have a header   %
% but don't contain any text, you can suppress printing by setting:            %
%                                                                              %
%   \let\cardprtempty\off       % suppress empty pages                         %
%   \let\cardprtempty\on        % print emty pages                             %
%                                                                              %
% ---------------------------------------------------------------------------- %
%                                                                              %
% These are the parameters you may change, however you should modify them in   %
% your source file rather than changing these global presets!                  %
%                                                                              %
\edef\on{1 }\edef\off{0 }

\let\cardpageno\on
\let\cardprtempty\on

\newdimen\cardwidth        \cardwidth74mm
\newdimen\cardheight       \cardheight52mm
\newdimen\cardfreespace    \cardfreespace5pt
\newdimen\cardsepamount    \cardsepamount\medskipamount

\newtoks\everycard          \everycard{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Don't change anything that follows! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Allow `@' in command names;
%
\ifcat @a \def\card@atdef{1} \else \catcode`\@=11 \def\card@atdef{0} \fi

% Declare registers needed:
%
\newdimen\card@height       \card@height\cardheight
\newdimen\card@headheight   \card@headheight12pt
\newdimen\card@tmpdim
\newdimen\card@ghsize
\newdimen\card@gvsize

\newbox\card@textboxone
\newbox\card@textboxtwo
\newbox\card@pagebox
\newbox\card@strutbox

\newtoks\origout
\newtoks\card@lhead         \card@lhead={}
\newtoks\card@rhead         \card@rhead={}

\newcount\card@pagecount    \card@pagecount0
\newcount\card@lcount       \card@lcount1
\newcount\card@rcount       \card@rcount2
\newcount\card@lrf          \card@lrf0

\def\on{1}
\def\off{0}
% Message macro for debugging:
%
\def\card@db#1#2{
    \ifnum#1<0  \message{#2} \fi
}

% Define strut for cardhead. Default is \strut. It shouldn't be needed but
% let's make sure:
%
\def\card@strut{\strut}
\def\card@makestrut#1{
    \global\def\card@strut{\copy\card@strutbox}
    \global\setbox\card@strutbox=\hbox{\vphantom{#1}}
    \card@tmpdim=\ht\card@strutbox \advance\card@tmpdim 1.4pt\global\ht\card@strutbox=\card@tmpdim
    \card@tmpdim=\dp\card@strutbox \advance\card@tmpdim 1.4pt\global\dp\card@strutbox=\card@tmpdim
}

% Flush contents of \TeX's vertical list:
%
\def\card@preflush{
    \origout=\output
    \card@tmpdim\vsize \vsize2\vsize
    \global\setbox\card@pagebox=\vbox{}
    \output={
        \card@db1{:}
        \global\setbox\card@pagebox=\vbox{
            \unvbox\card@pagebox
            \par\noindent\vskip\cardsepamount
            \ifvbox255\unvbox255
            \else\box255\fi
        }
    }
    \par\penalty-\@M
    \output=\origout
    \vsize\card@tmpdim
    \ifvbox\card@pagebox \else
        \global\setbox\card@pagebox=\vbox{\box\card@pagebox}
    \fi
}

% Initialize all local parameters of a card with those infos available.
% Furthermore change \output temporarily to allow page breaking within a card.
%
\def\card@init#1{
    \card@preflush
    \origout=\output
    \output={\card@output}
    \card@ghsize\hsize
    \card@gvsize\vsize
    \card@makestrut{#1}
    \card@lhead={#1\ifnum\on=\cardpageno\hfill\the\card@lcount\hskip\cardfreespace\else\fi}
    \card@rhead={#1\ifnum\on=\cardpageno\hfill\the\card@rcount\hskip\cardfreespace\else\fi}
    \global\card@headheight0pt
        \global\advance\card@headheight \ht\card@strutbox
        \global\advance\card@headheight \dp\card@strutbox
    \global\card@tmpdim\cardheight
        \global\advance\card@tmpdim -\card@headheight
        \global\card@height\card@tmpdim
        \global\advance\card@tmpdim -2\cardfreespace
               \vsize\card@tmpdim
    \global\card@tmpdim\cardwidth
        \global\advance\card@tmpdim -2\cardfreespace
               \hsize\card@tmpdim
    \global\card@pagecount0
    \global\card@lcount1
    \global\card@rcount2
    \global\card@lrf0
    \topskip0pt
    \card@db0{(}
}

% Restore \hsize, \vsize and \output to previous values:
%
\def\card@exit{
    \global\advance\card@lrf 2
    \the\output
    \hsize\card@ghsize %globa?
    \vsize\card@gvsize
    \output=\origout
    \origout={}
    \card@db0{)}
}

% Make cardfile-card with global dimensions and {lhead}{rhead}{lbody}{rbody}
%
\def\makecard#1#2#3#4{
    \vbox to \cardheight{
        \hrule
        \hbox to 2\cardwidth{\vrule
            \hbox to \cardwidth{\card@strut\hskip\cardfreespace#1\hss}\hss\vrule
            \hbox to \cardwidth{\card@strut\hskip\cardfreespace#2\hss}\hss\vrule}
        \hrule
        \hbox to 2\cardwidth{\vrule\hbox to \cardwidth{\hskip\cardfreespace
            \vbox to \card@height{\vskip\cardfreespace#3\vss}\hss
            \hss}\hss\vrule\hbox to \cardwidth{\hskip\cardfreespace
            \vbox to \card@height{\vskip\cardfreespace#4\vss}\hss
            \hss}\hss\vrule}
        \vss
        \hrule
    }
}

% Test whether left box contains any material
%
\newif\ifcard@page
\def\ifpage{
    \ifnum\the\card@lrf>0 \card@pagetrue        % Create output
    \else                 \card@pagefalse \fi   % unless regular left
    \ifnum\the\card@lrf=2
        \ifvbox\card@textboxone\else            % or empty
            \card@pagefalse
        \fi
        \ifnum\cardprtempty=\on\relax
            \ifnum\the\card@lcount=1
                \card@pagetrue
            \fi
        \fi
    \fi
    \ifcard@page
}

% Actual \output routine. This routine does not create any output. Rather it
% writes such boxes as it wishes to output into \card@pagebox, which then is
% passed to the regular output routine!
%
\def\card@output{
    \global\advance\card@pagecount 1
    \ifcase\the\card@lrf
            \global\setbox\card@textboxone=\box255\global\card@lcount\card@pagecount \card@db0{ (0,<\the\card@pagecount>) }
        \or \global\setbox\card@textboxtwo=\box255\global\card@rcount\card@pagecount \card@db0{ (1,<\the\card@pagecount>) }
        \or \global\setbox\card@textboxone=\box255\global\card@lcount\card@pagecount \card@db0{ (2,<\the\card@pagecount>) }
        \or \global\setbox\card@textboxtwo=\box255\global\card@rcount\card@pagecount \card@db0{ (3,<\the\card@pagecount>) }
    \fi
    \ifpage
        \global\setbox\card@pagebox=\vbox{\unvbox\card@pagebox
            \par\noindent\vskip\cardsepamount
            \makecard{\the\card@lhead}{\the\card@rhead}
                     {\box\card@textboxone}{\box\card@textboxtwo}
        }
    \fi
%   Test routines:
%   \ifnum\the\card@lrf>1 \shipout\box\card@pagebox \fi % for testing only
%   \ifnum\the\card@lrf>1 \shipout\copy\card@pagebox \fi % for testing only
    \ifnum\the\card@lrf>0 \global\card@lrf0 \else \global\card@lrf1 \fi
}

% The actual definition of the environment comes here, so as to allow use
% with plain \TeX\ as well:
%
\long\def\card#1{
    \begingroup
    \card@db2{#1}
    \card@init{#1}
    \the\everycard
    \par
}
\long\def\endcard{
    \vfill
    \penalty-\@M
    \card@exit
    \unvbox\card@pagebox
    \endgroup
}

% Restore `@' to catcode `other' if needed:
%
\ifx0\card@atdef \catcode`@=12 \else \fi