# -*- mode: Perl -*-
# /=====================================================================\ #
# |  subfloat                                                           | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;
#======================================================================
# Note that here, subfigures is a container for figures
# rather than a sub-figure within a figure!
DefMacro('\subfiguresbegin', '\begin{subfigures}');
DefMacro('\subfiguresend',   '\end{subfigures}');
DefMacro('\subtablesbegin',  '\begin{subtables}');
DefMacro('\subtablesend',    '\end{subtables}');

NewCounter('subfloatfigure', 'figure', idprefix => 'sf', idwithin => 'figure');
NewCounter('subfloattable',  'table',  idprefix => 'st', idwithin => 'table');
DefMacro('\thesubfloatfigure', '\themainfigure\alph{subfloatfigure}');
DefMacro('\thesubfloattable',  '\themaintable\alph{subfloattable}');
DefMacroI('\subfloatfigurename', undef, 'Figure');
DefMacroI('\subfloattablename',  undef, 'Table');

#DefMacro('\thesubfigure', '\alph{subfigure}');
#DefMacro('\thesubtable',  '\alph{subtable}');

DefMacro('\fnum@subfigure', '(\thesubfigure)');

DefEnvironment('{subfigures}',
  "<ltx:figure xml:id='#id' inlist='#inlist'>"
    . "#tags"
    . "#body"
    . "</ltx:figure>",
  properties   => sub { RefStepCounter('figure'); },    # No caption (?)
  beforeDigest => sub {
    Let('\themainfigure', '\thefigure');
    beforeFigure('figure');
    # redefine figure!
    DefEnvironment('{figure}[]',
      "<ltx:figure xml:id='#id' ?#1(placement='#1')>"    # NO inlist for subfigures!(?)
        . "#tags"
        . "#body"
        . "</ltx:figure>",
      properties   => { layout => 'vertical' },
      beforeDigest => sub { beforeFloat('subfloatfigure'); },
      afterDigest  => sub { afterFloat($_[1]); });
    DefEnvironment('{figure*}[]',
      "<ltx:figure xml:id='#id' ?#1(placement='#1')>"
        . "#tags"
        . "#body"
        . "</ltx:figure>",
      properties   => { layout => 'vertical' },
      beforeDigest => sub { beforeFloat('subfloatfigure', double => 1); },
      afterDigest  => sub { afterFloat($_[1]); });
  },
  afterDigest => sub { afterFloat($_[1]); });

DefMacro('\fnum@subtable', '(\thesubtable)');
DefEnvironment('{subtables}',
  "<ltx:table xml:id='#id' inlist='#inlist'>"
    . "#tags"
    . "#body"
    . "</ltx:table>",
  properties   => sub { RefStepCounter('table'); },    # No caption (?)
  beforeDigest => sub {
    Let('\themaintable', '\thetable');
    beforeFigure('table');
    # redefine table!
    DefEnvironment('{table}[]',
      "<ltx:table xml:id='#id' ?#1(placement='#1')>"    # NO inlist for subtables! (?)
        . "#tags"
        . "#body"
        . "</ltx:table>",
      properties   => { layout => 'vertical' },
      beforeDigest => sub { beforeFloat('subfloattable'); },
      afterDigest  => sub { afterFloat($_[1]); });
    DefEnvironment('{table*}[]',
      "<ltx:table xml:id='#id' ?#1(placement='#1')>"
        . "#tags"
        . "#body"
        . "</ltx:table>",
      properties   => { layout => 'vertical' },
      beforeDigest => sub { beforeFloat('subfloattable', double => 1); },
      afterDigest  => sub { afterFloat($_[1]); });
  },
  afterDigest => sub { afterFloat($_[1]); });

#======================================================================
1;
