% LaTeX macros for captioning boxes
% Extracted 93-12-02 by Jorge Stolfi 
%   from eps-macros.tex of 12 Oct 90 by Jack Snoeyink 
%   w/ ideas from John Hershberger and code from Tom Rokicki 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% SUMMARY
%
%   The most important macros defined by this file are:
% 
%     \captionbox
%       Adds a numbered caption to the given box, defines the
%       given label to be its number, and returns a box
%       containing everything.
%
%     \journalpicts
%       Asks for a separate list of figures, tables, and captions for journals.
%
% USAGE
%
%     \captionbox{box}{style}{label}{[optshortcap]caption}
%
%   where
%
%     box
%       is a box containing a figure or table.
%
%     style
%       is the caption style {figure}, {figure*}, {table}, or {table*}.
%
%     label
%       is the label to be assigned to the figure. If empty, the figure
%       will not be labeled.
%
%     caption
%       is of the form {}, {Caption} or {[short caption] Long caption},
%       where the short caption goes into the list of figures/tables.
%	If empty, the figure will not be captioned or labeled.
% 
%  Executing \journalpicts{scalefactor} at the beginning of the
%  file causes all figures and tables given to \captionbox to
%  be written to a .pic file, and all their captions to a .cap file.
%  On \enddocument, these files are read in and the lists of
%  figures and captions are produced.
%  
% OPTIONS
%
%   The following macros affect the operation of \captionbox,
%   and may be redefined by the user:
%
%     \captionstyle
%
%   Here are their meanings:
%
%     \captionstyle
%       a macro executed captions.
%       Default: nothing
%
%  For non-english versions, or to add other types of floats, the
%  macro \definefnum should be changed.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\catcode`@=11 \endlinechar=-1   % Use LaTeX internals, igore line breaks

% Default options
%
\def\captionstyle{}
\def\boxcaptionstyle{\raggedright}

\newbox\@captionee

\long\def\captionbox#1#2#3#4{
  % save figure if journalpicts:
  \writepict{{#1}{#2}{#3}{#4}}
  \setbox\@captionee\hbox{#1}
  \parbox[t]{\wd\@captionee}{
    \box\@captionee
    \let\captionstyle=\boxcaptionstyle
    \@captionandlabel{#2}{#3}{#4}
  }
}

\def\@captionandlabel#1#2#3{ % {style} {label} {caption}
  \def\testit{#3}
  \ifx\testit
    \empty
  \else
    % save for journal figs
    \writecapt{{#1}{#2}{#3}}
    % remove optional stars
    \@captypeunstarred#1*.
    % parse the caption:
    \getcaption#3\endc@ption
    % check for empty label:
    \def\testit{#2}
    \ifx\testit
      \empty
    \else
      \label{#2}
    \fi
  \fi
}

% remove two-column star from style:
%
\def\@captypeunstarred#1*#2.{
  \def\@captype{#1}
}

% Parse the caption of the form {caption} or {[short] long caption} 
%

\def\getcaption{\@ifnextchar[{\@getcaptwo}{\@getcapone}}

\long\def\@getcapone#1\endc@ption{
  \caption[#1]
  {
    \def\baselinestretch{1}
    \Large\normalsize\captionstyle
    \ignorespaces
    #1
  }
}

\long\def\@getcaptwo[#1]#2\endc@ption{
  \caption[#1]
  {
    \def\baselinestretch{1}
    \Large\normalsize\captionstyle
    \ignorespaces
    #2
  }
}

% Ugly latex hacking to get the right figure/table name:
%
\def\definefnum#1{
  \def\fnum@figure{Figura \ref{#1}}
  \def\fnum@table{Table \ref{#1}}
  \def\fnum@code{Algorithm \ref{#1}}
}

\def\writepict#1{}  % Default definition if no \journalpicts
\def\writecapt#1{}  % Default definition if no \journalpicts

% Creates separate list of figures and tables:
%

\def\journalpicts#1{
  \newwrite\pictfile
  \newwrite\captfile

  \openout\pictfile\jobname.pic
  \openout\captfile\jobname.cap

  \gdef\writepict##1{\unexpandedwrite\pictfile{\doit##1}}
  \gdef\writecapt##1{\unexpandedwrite\captfile{\doit##1}}

  \global\let\ENDdocument=\enddocument
  \gdef\enddocument{\DOjournalpicts{#1}\ENDdocument}
}

%	generate enlarged figures 1 per page and list captions
%
\def\DOjournalpicts#1{
  {
    \def\writepict##1{}\closeout\pictfile
    \def\writecapt##1{}\closeout\captfile
    % turn off toc, lof, and so on:
    \@fileswfalse

    % Generate list of figures:
    
    \onecolumn
    \def\globalscaling{#1}
    \def\doit##1##2##3##4{ % {box}{style}{label}{caption}
      % print a picture on blank page:
      \begin{##2}[t]
        {\hss##1\hss}
      \end{##2}
      \vspace*{1in}
      % use Figure \ref{label} or Table \ref{label}:
      \definefnum{##3}
      \@captionandlabel{##2}{}{##4}
      \clearpage
    }
    
    \input\jobname.pic

    % Generate list of captions:

    \def\doit##1##2##3{ % {style}{label}{caption}
      % print caption:
      %	use Figure \ref{label} or Table \ref{label}:
      \definefnum{##2}
      \@captionandlabel{##1}{}{##3}
    }

    % Be sure captions aren't centered:
    \raggedright\let\captionstyle=\raggedright
    \def\@makecaption##1##2{##1: ##2\par}

    \input\jobname.cap
    
    % to be followed by \enddocument.
  }
}

\catcode`@=12 \endlinechar=`\^^M

