\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{nccfloats}
         [2002/05/17 v1.01 NCC Extention of Graphicx (NCC)]
\RequirePackage{nccboxes}[2002/03/20]
%
% \changes{v1.00}{2002/03/23}{This version is uploaded to CTAN}
% \changes{v1.01}{2002/05/17}{Handle of side floats improved}
%
% Mini-figures and mini-tables
%
% \minifig[POS][HEIGHT][INNER-POS]{WIDTH}{BODY}
% \minitabl[POS][HEIGHT][INNER-POS]{WIDTH}{BODY}
%
% They are prepared in a minipage. The parametes have the same meaning
% as for the \parbox command.
%
% The \caption command is useful in the BODY.
%
\newcommand{\minifig}{\begingroup\def\@captype{figure}\NCC@minifloat}
\newcommand{\minitabl}{\begingroup\def\@captype{table}\NCC@minifloat}
%
% Figures and tables inside text
%
% \sidefig[POS](MWIDTH)(TWIDTH){MINIFLOAT BODY}{TEXT BODY}
% \sidetabl[POS](MWIDTH)(TWIDTH){MINIFLOAT BODY}{TEXT BODY}
%
% These commands place a minifloat (figure or table) next to the specified
% text on the outer side of page (to the right for odd page and
% to the left for even page). In twocolumn or oneside mode, minifloats
% are always posed to the right. *-forms for these commands provide
% reverse placement. By default, minifloat is vertically centered with
% respect to the text, struts are inserted before and after the text to
% provide normal distances of the text box from surrounding text.
%
% All parameters in square and round brackets are optional and mean
% the following:
%
% POS:    contains alignment char (tbc) of minifloat with respect
%         to text box and additional chars controlling text preparation:
%         j -- justify the last line of text, n -- no struts.
% MWIDTH: the width of minifloat in \unitlength
% TWIDTH: the width of text box in \unitlength
%
% If both width parameters are absent, the widths are calculated
% as (\linewidth-1.5em)/2. If TWIDTH is absent, the text body width
% is calculated as \linewidth-MWIDTH-1.5em.
%
\newcommand{\sidefig}{\NCC@sidemfloat{\minifig}}
\newcommand{\sidetabl}{\NCC@sidemfloat{\minitabl}}
%
% \ifleftsidefloat{L-CLAUSE}{R-CLAUSE}
%
% This command is useful in parameters of \sidefig or \sidetabl.
% It processes L-CLAUSE if minifloat body is posed to the left and
% R-CLAUSE otherwise.
%
\newif\ifNCC@smfltleft
\newcommand{\ifleftsidefloat}{%
  \ifNCC@smfltleft
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
%
%  Service commands for float figures and tables placement
%
%  \fig[LOC](WIDTH){BODY}
%  \tabl[LOC](WIDTH){BODY}
%  \figs[LOC](LWIDTH)(RWIDTH){LEFT BODY}{RIGHT BODY}
%  \tabls[LOC](LWIDTH)(RWIDTH){LEFT BODY}{RIGHT BODY}
%
% All parameters in square and round brackets are optional and mean
% the following:
%
% LOC:    the location positions for output of floats (defaul is [ht])
% WIDTH:  the width of body in \unitlength
% LWIDTH: the width of left body in \unitlength
% RWIDTH: the width of right body in \unitlength
%
% If both width parameters are absent, the widths are calculated
% as (\linewidth-1em)/2. If RWIDTH is absent, the right body width
% is calculated as \linewidth-LWIDTH-1em.
% *-forms of these commands are used to produce wide floats in \twocolumn mode.
%
\newcommand{\fig}{\NCC@float{figure}}
\newcommand{\tabl}{\NCC@float{table}}
\newcommand{\figs}{\NCC@floats{figure}b}
\newcommand{\tabls}{\NCC@floats{table}t}
%
% Minifloat
%
\newcommand*\NCC@minifloat[1][c]{%
  \@ifnextchar[{\NCC@mflt{#1}}{\NCC@@mflt{#1}\relax[s]}}
\def\NCC@mflt#1[#2]{%
  \@ifnextchar[{\NCC@@mflt{#1}{#2}}{\NCC@mflt{#1}{#2}[#1]}}
\long\def\NCC@@mflt#1#2[#3]#4#5{%
  \@iiiminipage{#1}{#2}[#3]{#4}\normalfont
  \NCC@fltstyle #5\endminipage\endgroup}
%
% Side minifloat
%
\def\NCC@sidemfloat#1{%
  \NCC@smfltleftfalse
  \if@twocolumn \else
    \if@twoside
      \ifodd\c@page \else \NCC@smfltlefttrue \fi
    \fi
  \fi
  \NCC@pair{\NCC@smflt{#1}}%
    {\ifNCC@smfltleft \NCC@smfltleftfalse \else \NCC@smfltlefttrue\fi
     \NCC@smflt{#1}}%
    {1.5em}{}%
}
% \NCC@smflt{MINIFLOAT-COMMAND}{POS}{MWIDTH}{TWIDTH}{MBODY}{TBODY}
\long\def\NCC@smflt#1#2#3#4#5#6{%
  \let\NCC@t\relax \let\NCC@b\relax \let\NCC@j\relax \let\NCC@n\strut
  \@tfor\@tempa :=#2\do {%
    \expandafter\let\csname NCC@\@tempa\endcsname\@empty}%
  \ifx\NCC@t\@empty \def\NCC@c{t}\else
   \ifx\NCC@b\@empty \def\NCC@c{b}\else
    \def\NCC@c{c}%
   \fi
  \fi
  \ifx\NCC@j\@empty \def\NCC@j{\parfillskip\z@skip}\fi
  \edef\NCC@t{\parindent\the\parindent\ifvmode\else\noindent\fi}%
  \ifvmode\else
    \unskip{\parfillskip\rightskip\par}\vskip -\parskip
  \fi
  \setbox\@tempboxa\vbox{\hsize\linewidth\noindent
    \NCC@wcalc{#3}{#4}%
    \ifNCC@smfltleft
      \jparbox{\Strut}[\NCC@c]\@tempdima{#1\@tempdima{#5}}%
      \nobreak\hskip\@tempdimc
    \fi
    \jparbox{\NCC@n\Strut}[\NCC@c]\@tempdimb{%
      \everypar{\NCC@n\everypar{}}\NCC@t#6%
      \ifvmode \else \unskip\NCC@n\NCC@j\fi}%
    \ifNCC@smfltleft \else
      \nobreak\hskip\@tempdimc
      \jparbox{\Strut}[\NCC@c]\@tempdima{#1\@tempdima{#5}}%
    \fi
  }%
  \@tempdima\dp\@tempboxa \advance\@tempdima\lineskip
  \dp\@tempboxa\@tempdima
  \@tempdima\ht\@tempboxa \advance\@tempdima -\ht\strutbox
  \noindent \raise-\@tempdima\box\@tempboxa
}
%
% Single float
%
\def\NCC@float#1{\@ifstar{\NCC@flt{#1*}}{\NCC@flt{#1}}}
\def\NCC@flt#1{\@ifnextchar[{\NCC@flt@{#1}}{\NCC@flt@{#1}[ht]}}
\def\NCC@flt@#1[#2]{\begin{#1}[#2]\centering
 \@ifnextchar({\NCC@@flt{#1}}{\NCC@@flt{#1}()}}
\long\def\NCC@@flt#1(#2)#3{%
  \if!#2!\@tempdima\linewidth \else \@tempdima#2\unitlength\fi
  \begingroup\NCC@minifloat[c]\@tempdima{#3}%
  \end{#1}%
}
%
% Pair of floats
%
\def\NCC@floats#1#2{%
  \NCC@pair{\NCC@@flts{#1}{#2}}{\NCC@@flts{#1*}{#2}}{1em}{ht}}
\long\def\NCC@@flts#1#2#3#4#5#6#7{%
  \begin{#1}[#3]\NCC@wcalc{#4}{#5}%
    \begingroup\NCC@minifloat[#2]\@tempdima{#6}\nobreak\hskip\@tempdimc
    \begingroup\NCC@minifloat[#2]\@tempdimb{#7}%
  \end{#1}%
}
%
% \NCC@pair{COMMAND1}{COMMAND2}{DEF-DIST}{DEF-PLACE}*[PLACE](LWIDTH)(RWIDTH)
% reads parameters (all except first 4 ones are optional) and executes
% COMMAND1{PLACE}{LWIDTH}{RWIDTH} if star is absent or
% COMMAND2{PLACE}{LWIDTH}{RWIDTH} if star presents.
%
\def\NCC@pair#1#2#3#4{\@tempdimc#3\relax
  \@ifstar{\NCC@pair@{#2}{#4}}{\NCC@pair@{#1}{#4}}}
\def\NCC@pair@#1#2{\@ifnextchar[{\NCC@pair@@{#1}}{\NCC@pair@@{#1}[#2]}}
\def\NCC@pair@@#1[#2]{\def\next@{#1{#2}}%
  \@ifnextchar({\NCC@pair@@@}{\NCC@@pair()()}}
\def\NCC@pair@@@(#1){\@ifnextchar({\NCC@@pair(#1)}{\NCC@@pair(#1)()}}
\def\NCC@@pair(#1)(#2){\next@{#1}{#2}}
%
% \NCC@wcalc{LWIDTH}{RWIDTH}
% Calculates widths of left and right boxes in \@tempdima and \@tempdimb
% using numeric values of parameters as multiple for \unitlength.
% The distance between boxes must be specified in \@tempdimc before the call.
%
% If first parameter is empty, \@tempdima is set to
%   (\linewidth-\@tempdimc)/2.
% If second parameter is empty, \@tempdimb is set to
%   \linewidth-\@tempdima-\@tempdimc.  
% If second parameter is specified, \@tempdimc is changed to
%   \linewidth-\@tempdima-\@tempdimb.
%
\def\NCC@wcalc#1#2{%
  \if!#1!\@tempdima .5\linewidth \advance\@tempdima -.5\@tempdimc 
  \else  \@tempdima#1\unitlength
  \fi
  \if!#2!\@tempdimb \linewidth \advance\@tempdimb -\@tempdima
         \advance\@tempdimb -\@tempdimc
  \else  \@tempdimb#2\unitlength
         \@tempdimc \linewidth \advance\@tempdimc -\@tempdima
         \advance\@tempdimc -\@tempdimb
  \fi
}
%
% Customizing a style of floats.
%
\newcommand*{\FloatStyle}[1]{\def\NCC@fltstyle{#1}}
\@onlypreamble\FloatStyle
%
% Defaults: centered \footnotesize text.
%
\FloatStyle{\footnotesize\centering}
\endinput
