% LaTeX macros for captioning boxes
% \def\lastedit{2014-06-25 13:39:20 by stolfilocal}
% 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:
% 
%     \botcaptionbox{BOX}{STYLE}{LABEL}{CAPTION}
%     \topcaptionbox{BOX}{STYLE}{LABEL}{CAPTION}
%     \captionbox{BOX}{STYLE}{LABEL}{CAPTION}
%       Adds a numbered caption to the given box, defines the
%       given label to be its number, and returns a box
%       containing everything.  Here:
%
%         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 {[ShortCaption] LongCaption},
%           where the ShortCaption goes into the list of figures/tables.
%           If empty, the figure will not be captioned or labeled.
% 
%       The \captionbox macro is intended for use
%       in \begin{figure}, \begin{table}, etc., which decide the
%       placement according to the STYLE; while \topcationbox and
%       \botcaptionbox are for hand-assembly of figure-like things.
%
%     \journalpicts{SCALEFACTOR} 
%       Called 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:
%
%     \botcaptionstyle
%     \topcaptionstyle
%       Macros executed before typesetting the text of \topcaptionbox
%       or \botcaptionbox, respectively.
%
%     \captionstyle
%       A macro executed before typesetting the text of \captionbox in
%       the text and in the list of figures.
%
%     \listcaptionstyle
%       A macro executed before typesetting the text of \captionbox in
%       the list of captions.
% 
% 
%     \botcaptionskip
%       Spacing above \botcaptionbox. 
% 
%     \topcaptionskip
%       Spacing below \botcaptionbox or below \topcaptionbox.
%
%     \abovecaptionskip
%     \belowcaptionskip
%       Spacing above and below \captionbox.
%
%  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\listcaptionstyle{}
\def\captionstyle{}
\def\botcaptionstyle{\raggedright}
\def\topcaptionstyle{\centering}

\newdimen{\topcaptionskip}
\setlength{\topcaptionskip}{5pt}
\newdimen{\botcaptionskip}
\setlength{\botcaptionskip}{5pt}

\newbox\@captionee

\long\def\botcaptionbox#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=\botcaptionstyle
    \abovecaptionskip=\botcaptionskip
    \belowcaptionskip=0pt
    \@captionandlabel{#2}{#3}{#4}
  }
}

\long\def\topcaptionbox#1#2#3#4{
  % save figure if journalpicts:
  \writepict{{#1}{#2}{#3}{#4}}
  \setbox\@captionee\hbox{#1}
  \parbox[t]{\wd\@captionee}{
    \let\@captionstyle=\topcaptionstyle
    \abovecaptionskip=0pt
    \belowcaptionskip=\topcaptionskip
    \@captionandlabel{#2}{#3}{#4}
    \box\@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=\captionstyle
    \@captionandlabel{#2}{#3}{#4}
  }
}

\def\@captionandlabel#1#2#3{ % {style} {label} {caption}
  \def\testit{#3}
  \ifx\testit
    \empty
  \else
    % save for list of captions
    \writecapt{{#1}{#2}{#3}}
    % remove optional stars
    \@captypeunstarred#1*.
    % parse the caption:
    \getc@ption#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\getc@ption{\@ifnextchar[{\@getcaptwo}{\@getcapone}}

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

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

% Ugly latex hacking to get the right figure/table name:
%
\def\definefnumPt#1{
  \def\fnum@figure{\textbf{Figura \ref{#1}}}
  \def\fnum@table{\textbf{Tabela \ref{#1}}}
  \def\fnum@code{\textbf{Algoritmo \ref{#1}}}
}
\def\definefnumEn#1{
  \def\fnum@figure{\textbf{Figure \ref{#1}}}
  \def\fnum@table{\textbf{Table \ref{#1}}}
  \def\fnum@code{\textbf{Algorithm \ref{#1}}}
}
\def\definefnum#1{\definefnumEn{#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 the 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
    }
    
    \let\@captionstyle=\captionstyle
    \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:
    \let\@captionstyle=\listcaptionstyle
    \def\@makecaption##1##2{##1: ##2\par}

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

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

