% subfigmat.sty - version 1.0 <27 feb 1999>
%
% defines an array/matrix-type environment for subfigures and
% subtables of the form,
%
%  \begin{subfigmatrix}{NC}
%    \subfigure[]{..}
%     ...
%  \end{subfigmatrix}
%
% where NC is the number of subfigures per row (i.e., the
% number of columns).  the subfigures are ordered from
% left-to-right, then top-to-bottom.

% the environment is used within a float environment such as
% figure or table.  each subfigure should have a variable width
% tied to the local \linewidth value so each can be shrunk or
% expanded to accomate the requested layout.
%
% The environment does not require ``square'' matrices since it
% only works on a row-by-row basis; thus you could have a 2 x 4,
% a 1 x 3, or a 4 x 2 if you so choose.  if you neglect to give
% it a full row, such as the case of a 3 x 3 matrix with only
% 8 elements, it simply fills the rows from left to right until
% it runs out of elements.
%
% for further information, see the example and other notes after
% the \endinput in this file.
%
\ProvidesPackage{subfigmat}[1999/02/27 v1.0 Subfigure Matrix Package]
\RequirePackage{subfigure}[1995/03/06]
%
% define and initialize the internal variables:
\newlength{\sfm@width}%               Subfigure width
\newlength{\sfmcolsep}%               Subfigure column separation
\setlength{\sfmcolsep}{2\tabcolsep}%  Use twice tabular column separation
\newcounter{sfm@count}%               Item count
%
% the environment:
\newenvironment{subfigmatrix}[1]{%
  \begingroup%
  \centering%
  \vspace*{-\subfigtopskip}% remove the vertical spacing inserted
                           % by the subfigure package (version 2.0)
    %
    % Save the "real" subfigure macro and start the item counter off
    % at -1 to detect the first item.
    % Set the \sfm@width to the single element size.
    %
    \let\sfm@subfigure\subfigure%
    \setcounter{sfm@count}{-1}%
    \setlength{\sfm@width}{\linewidth}%
    \addtolength{\sfm@width}{\sfmcolsep}%
    \addtolength{\sfm@width}{-#1\sfmcolsep}%
    \divide\sfm@width by#1
    \@ifundefined{Gin@scalex}%
    \relax % we are not running graphicx!
    {\setkeys{Gin}{width=\sfm@width,keepaspectratio}}%
    %
    % Redefine the \subfigure and \subtable macros locally to this
    % environment so that we can wrap them with minipages.
    %
    \def\subfigure{%
      \ifnum \value{sfm@count} = -1
      % very first item
        \setcounter{sfm@count}{1}%
      \else% Not very first item
        \addtocounter{sfm@count}{1}%
        \ifnum \value{sfm@count} = 1
          % Beginning of next column, finish the last column.
          \\%
        \else%
          % middle or last item
          \hfill%
          \ifnum #1 = \value{sfm@count}%
            % Reset the counter of at the end of the row.
            \setcounter{sfm@count}{0}%
          \fi%
        \fi%
      \fi%
      \sfm@subfigure}%
    \let\subtable\subfigure%
  }{%
    \\%
  \endgroup}%
%
\endinput

further example:

  if you wanted to create a figure with four subfigures in a tiled
  or matrix format of 2 x 2, the following would suffice,

    \begin{figure}
      \begin{subfigmatrix}{2}
        \subfigure[]{...}
        \subfigure[]{...}
        \subfigure[]{...}
        \subfigure[]{...}
      \end{subfigmatrix}
      \caption{Example.}
      \label{f:eg}
    \end{figure}

  the result would look similar to the following,

        [subfig]     [subfig]
           (a)          (b)

        [subfig]     [subfig]
           (c)          (d)

         Figure 1: Example.

notes:

  comments, bugs, fixes can be sent to w.l.kleb@larc.nasa.gov.
  what becomes of them is another story. ;)

  each subfigure is placed within a minipage of the proper width
  to fit NC subfigures within the current float's \linewidth,
  accounting for 2\tabcolsep's worth of space between each
  adjacent subfigure.

  \linewidth is a fairly general length.  it is equal to
  \textwidth for single-column formats, \columnwidth for multiple-
  column documents (and also single-column documents), or
  according to a \parbox or minipage environment.

  if you are using the graphicx package, the subfigure widths are
  automatically set to the local \linewidth.

  the separation between figures can be changed via the
  \sfmcolsep variable, e.g.,

     \setlength{\sfmcolsep}{\hspace{0.2in}}

  to set a ``hard'' inter-column spacing as opposed to the
  default behavior of tying the inter-column spacing to the
  documents tabular column spacing (\tabcolsep}.

to do:

  transposed ordering: from top-to-bottom, then left-to-right.

history:

  25 Feb 1999  Bil Kleb <w.l.kleb@larc.nasa.gov> [v1.0]
     Edited for Arseneau-style release to ctan.
     Changed \sfm@colsep to a more user-tunable \sfmcolsep.

  27 Feb 1997  Bil Kleb <w.l.kleb@larc.nasa.gov>
     Minor changes.

  27 Feb 1997  Steven Douglas Cochran <sdc+@cs.cmu.edu>
     Created.

  24 Feb 1997  Bil Kleb <w.l.kleb@larc.nasa.gov>
     Posted question to news:comp.text.tex.

distribution:

 this program can redistributed and/or modified under the terms
 of the LaTeX Project Public License Distributed from CTAN
 archives in directory macros/latex/base/lppl.txt; either
 version 1 of the License, or (at your option) any later version.
