%PACKAGE plates
%    For placement of special float enviornment `plates'.  Allows
%    user to put plate floats in document as if they were figure, but
%    the plates are all gathered and printed at the specified location.
%    Designed to be helpful when including color figures at the back of
%    a document.  Optionally, the `figures' option can be supplied and then
%    the plates will just be printed in the document.
%
%NEW ENVIORNMENT
%\begin{plate}
%%just use like figure enviornment
%\end{plate}
%
%NEW COMMANDS
%\ProcessPlates - at point of issue the plates are gathered together and printed.
%\listofplates - prints the list of plates just like \listoffigures
%\setplatename - sets the name of the float enviornment - default `plates'
%
%OPTIONS
%figures - treats plates like regular floats.  Plates are printed inside the document
%            and \autoref{} will now use Figure instead of Plate in referencing.
%            \listofplates & \ProcessPlates has no effect
%memoir - if using memoir class creates an added appendix page where \ProcessPlates is issued.
%onefloatperpage - only allows one float per page, after each float \cleardoublepage is issued.
%   otherwise default is to gather floats together but will print as many as will fit to a page
%   as well as printing on both sides of a two sided document. *Probably should fix this
%   or make an option to turn off both sides.    
%
%
%This Package includes code from the Memoir class and uses
%the endplate package, a modification of endfloat.
%
%Anthony A. Tanbakuchi
%Mar.19.2002
%
%
\NeedsTeXFormat{LaTeX2e} \ProvidesPackage{plates}[2002/03/18 v0.1 FigSize sizes graphics for graphicx package]
\RequirePackage{ifthen}[1994/06/01]

\newcounter{optfigures}
\newcounter{optmemoir}
\newcounter{optonefloatperpage}
    %Option to process all plates as figures (ie --- the package is disables, plate enviornments act like figures.)
    \DeclareOption{figures}{\setcounter{optfigures}{1}}
    \DeclareOption{memoir}{\setcounter{optmemoir}{1}}
        \DeclareOption{onefloatperpage}{\setcounter{optonefloatperpage}{1}}
    \ProcessOptions\relax

%The real setplatename definition comes later... this is just to ensure that 
%when figures option is used setplatename is still valid.
\newcommand{\setplatename}[1]{}

\ifthenelse{\value{optfigures} = 0}{%
%
%COMMANDS for the new float env & list of are from memoir.  
%If memoir is not in use then define the commands
\makeatletter
\@ifundefined{newlistof}{%TRUE TEXT
%*******************************************************************
%%***************COPIED from MEMOIR CLASS***************************
%*******************************************************************
\newcounter{newflo@tctr}
  \setcounter{newflo@tctr}{1}
\newcommand{\newfloat}[4][\@empty]{%
  \@namedef{ftype@#2}{\value{newflo@tctr}}
  \addtocounter{newflo@tctr}{\value{newflo@tctr}}
  \@ifundefined{c@#2}{% counter is not defined
    \ifx \@empty#1\relax
      \newcounter{#2}
    \else
      \newcounter{#2}[#1]
      \expandafter\edef\csname the#2\endcsname{%
        \expandafter\noexpand\csname the#1\endcsname.\noexpand\arabic{#2}}
    \fi}{}
  \setcounter{#2}{0}

  \@namedef{ext@#2}{#3}  % file extension
  \@ifundefined{c@#3depth}{\newcounter{#3depth}}{}
  \setcounter{#3depth}{1}

  \@namedef{fps@#2}{tbp}                     % position
  \@namedef{fnum@#2}{#4~\@nameuse{the#2}}    % caption naming
  \@namedef{fleg#2}{#4}                      % legend naming
  \@namedef{flegtoc#2}##1{}                  % legend name in ToC

  \newenvironment{#2}{\@float{#2}}{\end@float}
  \newenvironment{#2*}{\@dblfloat{#2}}{\end@dblfloat}
} % end \newfloat

\newcommand{\newlistentry}[4][\@empty]{%
  \@ifundefined{c@#2}{%    check & set the counter
    \ifx \@empty#1\relax
      \newcounter{#2}
    \else
      \newcounter{#2}[#1]%
      \expandafter\edef\csname the#2\endcsname{%
        \expandafter\noexpand\csname the#1\endcsname.\noexpand\arabic{#2}}
    \fi}{}
  \setcounter{#2}{0}

  \@namedef{l@#2}##1##2{%
    \ifnum \@nameuse{c@#3depth} > #4\relax
      \vskip \@nameuse{cftbefore#2skip}
      {\leftskip \@nameuse{cft#2indent}\relax
       \rightskip \@tocrmarg
       \parfillskip -\rightskip
       \parindent \@nameuse{cft#2indent}\relax\@afterindenttrue
       \interlinepenalty\@M
       \leavevmode
       \@tempdima \@nameuse{cft#2numwidth}\relax
       \expandafter\let\expandafter\@cftbsnum\csname cft#2presnum\endcsname
       \expandafter\let\expandafter\@cftasnum\csname cft#2aftersnum\endcsname
       \expandafter\let\expandafter\@cftasnumb\csname cft#2aftersnumb\endcsname
       \advance\leftskip\@tempdima \null\nobreak\hskip -\leftskip
       {\@nameuse{cft#2font}##1}\nobreak
       \@nameuse{cft#2fillnum}{##2}}
    \fi
  }  % end of \l@#2

  \expandafter\newlength\csname cftbefore#2skip\endcsname
    \setlength{\@nameuse{cftbefore#2skip}}{\z@ \@plus .2\p@}
  \expandafter\newlength\csname cft#2indent\endcsname
  \expandafter\newlength\csname cft#2numwidth\endcsname
  \ifcase #4\relax  % 0   (level 1)
    \setlength{\@nameuse{cft#2indent}}{0em}
    \setlength{\@nameuse{cft#2numwidth}}{2.3em}
  \or               % 1   (level 2)
    \setlength{\@nameuse{cft#2indent}}{2.3em}
    \setlength{\@nameuse{cft#2numwidth}}{3.2em}
  \or               % 2   (level 3)
    \setlength{\@nameuse{cft#2indent}}{5.5em}
    \setlength{\@nameuse{cft#2numwidth}}{4.1em}
  \or               % 3   (level 4)
    \setlength{\@nameuse{cft#2indent}}{8.5em}
    \setlength{\@nameuse{cft#2numwidth}}{5.0em}
  \else             % anything else
    \setlength{\@nameuse{cft#2indent}}{10.5em}
    \setlength{\@nameuse{cft#2numwidth}}{6.0em}
  \fi
  \@namedef{cft#2font}{\normalfont}
  \@namedef{cft#2presnum}{}
  \@namedef{cft#2aftersnum}{}
  \@namedef{cft#2aftersnumb}{}
  \@namedef{cft#2dotsep}{\cftdotsep}
  \@namedef{cft#2leader}{\normalfont\cftdotfill{\@nameuse{cft#2dotsep}}}
  \@namedef{cft#2pagefont}{\normalfont}
  \@namedef{cft#2afterpnum}{}
  \@namedef{toclevel@#2}{#4}
  \@namedef{cft#2fillnum}##1{%
    {\@nameuse{cft#2leader}}\nobreak
    \hb@xt@\@pnumwidth{\hfil\@nameuse{cft#2pagefont}##1}\@nameuse{cft#2afterpnum}\par}
} % end \newlistentry

\newcommand{\newsubfloat}[1]{%
  \newlistentry[#1]{sub#1}{\@nameuse{ext@#1}}{1}
  \@namedef{ext@sub#1}{\csname ext@#1\endcsname}
  \@namedef{thesub#1}{(\alph{sub#1})}
  \@namedef{@thesub#1}{{\subcaplabelfont\@nameuse{thesub#1}}\space}
  \@namedef{p@sub#1}{\csname the#1\endcsname}
  \@namedef{@makesub#1caption}{\@makesubfigurecaption}
}

\newcommand{\newfixedcaption}[3][\caption]{%
  \newcommand{#2}{\def\@captype{#3}#1}}
\newcommand{\renewfixedcaption}[3][\caption]{%
  \renewcommand{#2}{\def\@captype{#3}#1}}
\newcommand{\providefixedcaption}[3][\caption]{%
  \providecommand{#2}{\def\@captype{#3}#1}}

\newcommand{\newlistof}[3]{%
  \@namedef{ext@#2}{#2}
  \@ifundefined{c@#2depth}{\newcounter{#2depth}}{}
  \setcounter{#2depth}{1}

  \@namedef{#2mark}{%
    \@mkboth{#3}{#3}}

  \@namedef{#1}{\@ifstar{\@nameuse{@star#2}}{\@nameuse{@plain#2}}}
  \@namedef{@star#2}{%
    \ensureonecol
    \par
    \begingroup
%%%      \parindent\z@ \parskip\cftparskip
      \@nameuse{@#2maketitle}
      \@starttoc{#2}%
    \endgroup
    \restorefromonecol}

  \@namedef{@plain#2}{%
    \ensureonecol
    \par
    \begingroup
%%%      \parindent\z@ \parskip\cftparskip
      \@nameuse{@#2maketitle}
      \phantomsection
      \addcontentsline{toc}{chapter}{#3}
      \@starttoc{#2}%
    \endgroup
    \restorefromonecol}

  \@namedef{@#2maketitle}{%
    \@nameuse{#2headstart}
   {\parindent\z@ \parskip\cftparskip
    \interlinepenalty\@M
    \@nameuse{print#2title}{#3}%
    \@nameuse{#2mark}%
    \thispagestyle{chapter}%
    \@nameuse{after#2title}
   }
    \@afterheading}

  \@namedef{#2headstart}{\chapterheadstart}
  \@namedef {after#2title}{\afterchaptertitle}
    \@namedef{print#2title}##1{\printchaptertitle{##1}}
} % end \newlistof
%*******************************************************************
%%***********END*COPIED from MEMOIR CLASS***************************
%*******************************************************************
}{%FALSE TEXT -- nothing needed
}

%
%Plates Commands
\newcommand{\platename}{Plate}%
\newcommand{\platenames}{{\platename}s}%
\renewcommand{\setplatename}[1]{\renewcommand{\platename}{#1}}

\newcommand{\listplatename}{List of \platenames}%
\newlistof{listofplates}{plt}{\listplatename}%
\newfloat{plate}{plt}{\platename}%
\newfixedcaption{\fpltcaption}{plt}%
\newlistentry{plate}{plt}{0}%
\newsubfloat{plate}%
\renewcommand{\theplate}{\Roman{plate}}%
%
%Make sure plate pages don't have seciton headers
%%kuchicomment
%\makepagestyle{floatcomp}%
%% \headwidth has already been defined for the companion style
%\makeheadrule{floatcomp}{\headwidth}%
%{\ifonlyfloats{0pt}{\normalrulethickness}}%
%\makeheadposition{floatcomp}{flushright}{flushleft}{}{}%
%\makepsmarks{floatcomp}{\companionpshook}%
%\makeevenhead{floatcomp}{\ifonlyfloats{}{\normalfont\bfseries\thepage}}%
%{}{\ifonlyfloats{}{\normalfont\bfseries\leftmark}}%
%\makeoddhead{floatcomp}{\ifonlyfloats{}{\normalfont\bfseries\rightmark}}%
%{}{\ifonlyfloats{}{\normalfont\bfseries\thepage}}%
%\makeevenfoot{floatcomp}{}{\ifonlyfloats{\thepage}{}}{}%
%\makeoddfoot{floatcomp}{}{\ifonlyfloats{\thepage}{}}{}%
%
%Ensure proper autoref for subplates
\newcommand{\subplateautorefname}{\mbox{\platename}}%
%
\RequirePackage[nolists,noheads,nomarkers]{endplate}[2002/03/18]%
%
%Set up what is to happen when \ProcessPlates is issued
%
%Command to print out the plates at given location
\newcommand{\ProcessPlates}{\processdelayedfloats}%

    %If option MEMOIR is supplied then add an appendix page to front of plates
\ifthenelse{\value{optmemoir} = 1}{%
\renewcommand{\ProcessPlates}{%
\AtBeginPlates{\renewcommand{\appendixpagename}{\platenames}%
\renewcommand{\appendixname}{\platenames}%
\renewcommand{\appendixtocname}{\platenames}%
\appendixpage%
\clearforchapter}%
\processdelayedfloats}
}{}
%
%Allow multiple floats per page
\renewcommand{\efloatseparator}{}
%Option for one float per page
\ifthenelse{\value{optonefloatperpage} = 1}{%
\renewcommand{\efloatseparator}{\cleardoublepage}}{}
%
}{%OPTION FIGURES is being used, so we just make the plate enviornment a figure enviornment
\newenvironment{plate}{\begin{figure}}{\end{figure}}%
%ProcessPlates command now does nothing!%
\newcommand{\ProcessPlates}{}%
%\listofplates now does nothing
\newcommand{\listofplates}{}

}


\endinput