%%
%% This is file `keyfloat.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% keyfloat.dtx  (with options: `package')
%% This is a generated file.
%% Copyright 2016 Brian Dunn
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{keyfloat}
    [2016/12/02 v0.11 Key/value interface for floats and the subcaption package.]




\RequirePackage{etoolbox}[2011/01/03]%

\RequirePackage{xparse}

\RequirePackage{xifthen}
\RequirePackage{xkeyval}
\RequirePackage{graphicx}
\RequirePackage{caption}[2010/10/31]% v3.2 to support \phantomcaption
\RequirePackage{subcaption}
\RequirePackage{calc}
\RequirePackage{rotating}




\ProvideDocumentEnvironment{tablehere}{}
  {\bigbreak\noindent\minipage{\linewidth}\def\@captype{table}}
  {\endminipage\bigbreak}

\ProvideDocumentEnvironment{figurehere}{}
  {\bigbreak\noindent\minipage{\linewidth}\def\@captype{figure}}
  {\endminipage\bigbreak}



\newcounter{KFLT@numcols}

\newcounter{KFLT@thiscol}

\newlength{\KFLT@rowboxwidth}


\newboolean{KFLT@cont}{}
\define@key{KFLT@keys}{cont}[true]{\setboolean{KFLT@cont}{#1}}
\newcommand{\KFLT@c}{}
\newboolean{KFLT@cstar}
\define@key{KFLT@keys}{c}%
{\renewcommand{\KFLT@c}{#1}\setboolean{KFLT@cstar}{false}}
\define@key{KFLT@keys}{cstar}%
{\renewcommand{\KFLT@c}{#1}\setboolean{KFLT@cstar}{true}}
\define@key{KFLT@keys}{sc}{%
\renewcommand{\KFLT@sc}{#1}%
\setboolean{KFLT@scgiven}{true}%
}
\newcommand{\KFLT@sc}{}
\newboolean{KFLT@scgiven}
\newcommand*{\KFLT@type}{}
\newcommand*{\KFLT@listtype}{}
\define@key{KFLT@keys}{l}{\renewcommand{\KFLT@l}{#1}}
\newcommand*{\KFLT@l}{}
\define@key{KFLT@keys}{ap}{\renewcommand{\KFLT@ap}{#1}}
\newcommand*{\KFLT@ap}{}
\define@key{KFLT@keys}{af}{\renewcommand{\KFLT@af}{#1}}
\newcommand*{\KFLT@af}{}
\define@key{KFLT@keys}{al}{\renewcommand{\KFLT@al}{#1}}
\newcommand*{\KFLT@al}{}
\define@key{KFLT@keys}{as}{\renewcommand{\KFLT@as}{#1}}
\newcommand*{\KFLT@as}{}
\newcommand*{\KFLT@textalign}{}
\newcommand{\KFLT@t}{}
\@ifpackageloaded{tocdata}
{}
{
\newcommand{\tdtextjustify}{}
\newcommand{\tdtextcenter}{}
\newcommand{\tdtextleft}{}
\newcommand{\tdtextright}{}
\newcommand{\tdnamejustify}{}
\newcommand{\tdnamecenter}{}
\newcommand{\tdnameleft}{}
\newcommand{\tdnameright}{}
}
\define@key{KFLT@keys}{t}{
\renewcommand{\KFLT@t}{#1}
\renewcommand{\KFLT@textalign}{}
\tdtextjustify
}
\define@key{KFLT@keys}{tc}{
\renewcommand{\KFLT@t}{#1}
\renewcommand{\KFLT@textalign}{\centering}
\tdtextcenter
}
\define@key{KFLT@keys}{tr}{
\renewcommand{\KFLT@t}{#1}
\renewcommand{\KFLT@textalign}{\raggedleft}
\tdtextright
}
\define@key{KFLT@keys}{tl}{
\renewcommand{\KFLT@t}{#1}
\renewcommand{\KFLT@textalign}{\raggedright}
\tdtextleft
}
\newcommand*{\KFLT@i}{}
\define@key{KFLT@keys}{lw}{%
\renewcommand{\KFLT@lw}{#1}%
\setlength{\KFLT@w}{0pt}%
}
\newcommand*{\KFLT@lw}{}
\define@key{KFLT@keys}{w}{%
\setlength{\KFLT@w}{#1}%
\renewcommand{\KFLT@lw}{}%
}
\newlength{\KFLT@w}
\define@key{KFLT@keys}{h}{\setlength{\KFLT@h}{#1}}
\newlength{\KFLT@h}
\define@key{KFLT@keys}{s}{\renewcommand{\KFLT@s}{#1}}
\newcommand*{\KFLT@s}{1}
\define@key{KFLT@keys}{r}{\renewcommand{\KFLT@r}{#1}}
\newcommand*{\KFLT@r}{0}
\define@key{KFLT@keys}{f}[true]{\setboolean{KFLT@f}{#1}}
\newboolean{KFLT@f}

\define@key{KFLT@keys}{ft}[true]{\setboolean{KFLT@ft}{#1}}
\newboolean{KFLT@ft}

\define@key{KFLT@keys}{stretch}{\renewcommand{\KFLT@stretch}{#1}}
\newcommand*{\KFLT@stretch}{1}


\newcounter{KFLT@keyfloatdepth}
\setcounter{KFLT@keyfloatdepth}{0}

\newboolean{KFLT@inkeysubfloats}
\setboolean{KFLT@inkeysubfloats}{false}


\newboolean{KFLT@subgrpcont}{}
\define@key{KFLT@subgrpkeys}{cont}[true]{%
\setboolean{KFLT@subgrpcont}{#1}%
}

\newcommand{\KFLT@subgrpc}{}

\newboolean{KFLT@subgrpcstar}
\define@key{KFLT@subgrpkeys}{c}
{\renewcommand{\KFLT@subgrpc}{#1}\setboolean{KFLT@subgrpcstar}{false}}
\define@key{KFLT@subgrpkeys}{cstar}
{\renewcommand{\KFLT@subgrpc}{#1}\setboolean{KFLT@subgrpcstar}{true}}
\define@key{KFLT@subgrpkeys}{sc}{%
\renewcommand{\KFLT@subgrpsc}{#1}%
\setboolean{KFLT@subgrpscgiven}{true}%
}

\newcommand{\KFLT@subgrpsc}{}

\newboolean{KFLT@subgrpscgiven}

\newcommand*{\KFLT@subgrptype}{}

\newcommand*{\KFLT@subgrplisttype}{}

\newcommand*{\KFLT@setsubgrpfigure}{%
\renewcommand{\KFLT@subgrptype}{figure}%
\renewcommand{\KFLT@subgrplisttype}{lof}%
}

\newcommand*{\KFLT@setsubgrptable}{%
\renewcommand{\KFLT@subgrptype}{table}%
\renewcommand{\KFLT@subgrplisttype}{lot}%
}
\define@key{KFLT@subgrpkeys}{l}{\renewcommand{\KFLT@subgrpl}{#1}}
\newcommand*{\KFLT@subgrpl}{}
\newcommand*{\KFLT@subgrptextalign}{}
\newcommand{\KFLT@subgrpt}{}
\define@key{KFLT@subgrpkeys}{t}{
\renewcommand{\KFLT@subgrpt}{#1}
\renewcommand{\KFLT@subgrptextalign}{}
\tdtextjustify
}
\define@key{KFLT@subgrpkeys}{tc}{
\renewcommand{\KFLT@subgrpt}{#1}
\renewcommand{\KFLT@subgrptextalign}{\centering}
\tdtextcenter
}
\define@key{KFLT@subgrpkeys}{tl}{
\renewcommand{\KFLT@subgrpt}{#1}
\renewcommand{\KFLT@subgrptextalign}{\raggedright}
\tdtextleft
}
\define@key{KFLT@subgrpkeys}{tr}{
\renewcommand{\KFLT@subgrpt}{#1}
\renewcommand{\KFLT@subgrptextalign}{\raggedleft}
\tdtextright
}
\define@key{KFLT@subgrpkeys}{ap}{\renewcommand{\KFLT@subgrpap}{#1}}
\newcommand*{\KFLT@subgrpap}{}
\define@key{KFLT@subgrpkeys}{af}{\renewcommand{\KFLT@subgrpaf}{#1}}
\newcommand*{\KFLT@subgrpaf}{}
\define@key{KFLT@subgrpkeys}{al}{\renewcommand{\KFLT@subgrpal}{#1}}
\newcommand*{\KFLT@subgrpal}{}
\define@key{KFLT@subgrpkeys}{as}{\renewcommand{\KFLT@subgrpas}{#1}}
\newcommand*{\KFLT@subgrpas}{}
%%


\newlength{\KFLT@imagewidth}

\newlength{\KFLT@boxwidth}
\newcommand*{\KFLT@findwidths}{%
\ifthenelse{\boolean{KFLT@ft}}% tight frame?
{\setlength{\KFLT@boxwidth}{\linewidth - 2\KFLTtightframewidth}}%
{% not tight frame
\ifthenelse{\boolean{KFLT@f}}% loose frame?
{\setlength{\KFLT@boxwidth}{\linewidth - 2\KFLTlooseframewidth}}%
{\setlength{\KFLT@boxwidth}{\linewidth}}% no frame
}% not tight frame
\ifthenelse{\dimtest{\KFLT@w}{>}{0pt}}%
{\setlength{\KFLT@imagewidth}{\KFLT@w}}%
{% width not given
\ifcsempty{\KFLT@lw}%
{\setlength{\KFLT@imagewidth}{\KFLT@boxwidth}}%
{\setlength{\KFLT@imagewidth}{\KFLT@lw\KFLT@boxwidth}}%
}% width not given
}
\newcommand{\KFLTtightframe}[1]{%
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{.4pt}%
\fbox{#1}%
}

\newlength{\KFLTtightframewidth}
\setlength{\KFLTtightframewidth}{.4pt}
\newcommand{\KFLTlooseframe}[1]{%
\setlength{\fboxsep}{3pt}%
\setlength{\fboxrule}{.4pt}%
\fbox{#1}%
}
\newlength{\KFLTlooseframewidth}
\setlength{\KFLTlooseframewidth}{3.4pt}
\newcommand{\KFLT@frame}[1]
{%
\ifthenelse{\boolean{KFLT@ft}}%
{\KFLTtightframe{#1}}%
{% not tightframe
\ifthenelse{\boolean{KFLT@f}}%
{\KFLTlooseframe{#1}}%
{#1}% no frame
}% not looseframe
}
\newcommand{\KFLT@findenvboxwidth}{%
\settowidth{\KFLT@envboxwidth}{\usebox{\KFLT@envbox}}%
\ifthenelse{\boolean{KFLT@ft}}%
{\addtolength{\KFLT@envboxwidth}{2\KFLTtightframewidth}}%
{% not tightframe
\ifthenelse{\boolean{KFLT@f}}%
{\addtolength{\KFLT@envboxwidth}{2\KFLTlooseframewidth}}%
{}% no frame
}% not looseframe
}
\NewDocumentCommand{\KFLT@onefigureimage}{}
{%
\begin{lrbox}{\KFLT@envbox}%
\ifthenelse{\NOT\equal{\KFLT@lw}{}}%
{\includegraphics%
[scale=\KFLT@s,width=\KFLT@imagewidth]{\KFLT@i}}%
{% not linewidth
\ifthenelse{\dimtest{\KFLT@w}{>}{0pt}}%
{% width is given
\ifthenelse{\dimtest{\KFLT@h}{>}{0pt}}%
{% w and h
\includegraphics%
[scale=\KFLT@s,%
width=\KFLT@imagewidth,height=\KFLT@h]{\KFLT@i}%
}% w and h
{% only w
\includegraphics%
[scale=\KFLT@s,width=\KFLT@imagewidth]{\KFLT@i}%
}% only w
}% width is given
{% width is not given
\ifthenelse{\dimtest{\KFLT@h}{>}{0pt}}%
{\includegraphics%
[scale=\KFLT@s,height=\KFLT@h]{\KFLT@i}}%
{\includegraphics%
[scale=\KFLT@s]{\KFLT@i}}%
}% width is not given
}% not linewidth
\end{lrbox}%
\unskip%
\KFLT@findenvboxwidth%
\begin{turn}{\KFLT@r}%
\begin{minipage}{\KFLT@envboxwidth}%
\KFLT@frame{\usebox{\KFLT@envbox}}%
\end{minipage}%
\unskip%
\end{turn}%
\par%
\addvspace{2ex}%
}

\newcommand*{\KFLT@captioniftype}[2]{%
\ifthenelse{\equal{\csname KFLT@#2type\endcsname}{#1}}%
{\KFLT@caption{#2}}%
{}%
}

\NewDocumentCommand{\KFLT@dosimplecaption}{m m m}
{%
\unskip%
\IfBooleanTF{#1}% star?
{% star
\IfValueTF{#2}{\caption*[#2]{#3}}{\caption*{#3}}%
}% star
{% no star
\IfValueTF{#2}{\caption[#2]{#3}}{\caption{#3}}%
}% no star
}
\@ifpackageloaded{tocdata}
{% tocdata loaded
\NewDocumentCommand{\KFLT@docaption}{s o m m}
{%
\ifthenelse{\equal{\csname KFLT@#4type\endcsname}{figure}}%
{% figure
\ifcsempty{KFLT@#4al}%
{% figure w/o artist
\KFLT@dosimplecaption{#1}{#2}{#3}%
}% figure w/o artist
{% figure with an artist
\IfBooleanTF{#1}{% star
\captionartist*[#2]{#3}%
[\csname KFLT@#4t\endcsname]%
[\csname KFLT@#4ap\endcsname]%
{\csname KFLT@#4af\endcsname}%
{\csname KFLT@#4al\endcsname}%
[\csname KFLT@#4as\endcsname]%
}% star
{% no star
\captionartist[#2]{#3}%
[\csname KFLT@#4t\endcsname]%
[\csname KFLT@#4ap\endcsname]%
{\csname KFLT@#4af\endcsname}%
{\csname KFLT@#4al\endcsname}%
[\csname KFLT@#4as\endcsname]%
}% no star
}% figure with an artist
}% figure
{% not a figure, ignore artist information:
\KFLT@dosimplecaption{#1}{#2}{#3}%
}% not a figure
}% KFLT@tocdata
}% tocdata loaded
{% no tocdata
\NewDocumentCommand{\KFLT@docaption}{s o m m}
{%
\KFLT@dosimplecaption{#1}{#2}{#3}%
\ifcsempty{KFLT@#4af}%
{\index{\csname KFLT@#4al\endcsname}}%
{\index{\csname KFLT@#4al\endcsname, \csname KFLT@#4af\endcsname}}%
}% KFLT@docaption
}% no tocdata

\newcommand{\KFLT@caption}[1]{%
\ifthenelse{\boolean{KFLT@#1cstar}}% starred caption?
{%starred caption
\ifcsempty{KFLT@#1c}% cstar={}?
{}%
{% non-empty starred caption
\ifcsempty{KFLT@#1sc}%
{}%
{% non-empty cstar and sc:
\addcontentsline{\KFLT@listtype}%
{\csname KFLT@#1type\endcsname}{\KFLT@sc}%
}% non-empty cstar and sc
\KFLT@docaption*{\csname KFLT@#1c\endcsname}{#1}%
}%
}% starred caption
{% unstarred caption
\ifcsempty{KFLT@#1sc}%
{% no short cap
\KFLT@docaption{\csname KFLT@#1c\endcsname}{#1}%
}% no short cap
{% short cap
\KFLT@docaption[\csname KFLT@#1sc\endcsname]%
{\csname KFLT@#1c\endcsname}{#1}%
}% short cap
\ifcsempty{KFLT@#1l}%
{}%
{\label{\csname KFLT@#1l\endcsname}}%
}% unstarred caption
}


\newcommand*{\KFLT@defaults}{%
\setboolean{KFLT@cont}{false}%
\renewcommand{\KFLT@c}{}%
\setboolean{KFLT@cstar}{false}%
\renewcommand{\KFLT@sc}{}%
\setboolean{KFLT@scgiven}{false}%
\renewcommand{\KFLT@type}{figure}%
\renewcommand{\KFLT@listtype}{lof}%
\renewcommand{\KFLT@l}{}%
\renewcommand{\KFLT@ap}{}%
\renewcommand{\KFLT@af}{}%
\renewcommand{\KFLT@al}{}%
\renewcommand{\KFLT@as}{}%
\renewcommand{\KFLT@t}{}%
\renewcommand{\KFLT@textalign}{}%
\tdtextjustify%
\renewcommand{\KFLT@i}{}%
\renewcommand{\KFLT@lw}{}%
\setlength{\KFLT@w}{0pt}%
\setlength{\KFLT@h}{0pt}%
\renewcommand{\KFLT@s}{1}%
\renewcommand{\KFLT@r}{0}%
\setboolean{KFLT@f}{false}%
\setboolean{KFLT@ft}{false}%
\renewcommand{\KFLT@stretch}{1}%
}


\newcommand*{\KFLT@maybestartfloatrow}{%
\KFLT@maybeendfloatrow%
\defcounter{KFLT@thiscol}{\value{KFLT@thiscol}+1}%
}

\newcommand*{\KFLT@maybeendfloatrow}{%
\ifthenelse{\cnttest{\value{KFLT@thiscol}}{>=}{\value{KFLT@numcols}}}%
{%

\addvspace{.75\floatsep}%

\defcounter{KFLT@thiscol}{0}%
}{}%
}%


\newcommand{\KFLT@trackrows}
{%
\ifthenelse{%
\cnttest{\value{KFLT@keyfloatdepth}}>{0}%
\OR\boolean{KFLT@inkeysubfloats}%
}%
{% nested
\KFLT@maybestartfloatrow%
\ifthenelse{\cnttest{\value{KFLT@thiscol}}{>}{1}}%
{\hfill}{}%
}% nested
{}% not nested
}

\newcommand{\KFLT@addtext}[1]
{%
\ifcsempty{KFLT@#1t}%
{}% no text
{% text to add
{% local
\addvspace{2ex}%
\begin{minipage}{\linewidth}%
\csname KFLT@#1textalign\endcsname%
\footnotesize%
\setlength{\parskip}{1.5ex}%
\setlength{\parindent}{0em}%
\csname KFLT@#1t\endcsname%
\end{minipage}%
\par\addvspace{2ex}%
}% local
}% text to add
}
\newcommand{\KFLT@optionalname}[1]
{%
\ifthenelse{\equal{#1}{}}%
{}%
{#1~}%
}
\@ifpackageloaded{tocdata}
{% tocdata loaded
\newcommand{\KFLT@addartisttext}[1]
{%
\ifthenelse{\equal{\csname KFLT@#1type\endcsname}{figure}}%
{% figure
\ifcsempty{KFLT@#1al}%
{\KFLT@addtext{#1}}%
{}% fig w/ artist: text will be added by \captionartist in \KFLT@caption
}% figure
{\KFLT@addtext{#1}}%
}% KFLT@addartisttext
}% tocdata loaded
{% tocdata not loaded
\newcommand{\KFLT@addartisttext}[1]
{%
\ifcsempty{KFLT@#1al}%
{}% last name not given
{% last name given
\addvspace{2ex}%
\begin{minipage}{\linewidth}%
\centering\footnotesize\textsc{%
\KFLT@optionalname{\csname KFLT@#1ap\endcsname}%
\KFLT@optionalname{\csname KFLT@#1af\endcsname}%
\csname KFLT@#1al\endcsname\csname KFLT@#1as\endcsname%
}%
\end{minipage}%
\par\addvspace{2ex}%
}% last name given
\KFLT@addtext{#1}%
}% KFLT@addartisttext
}% tocdata not loaded

\newsavebox{\KFLT@envbox}
\newlength{\KFLT@envboxwidth}

\NewDocumentEnvironment{KFLT@boxinner}{}
{% keyboxinner
\begin{lrbox}{\KFLT@envbox}%
\turn{\KFLT@r}%
\minipage{\KFLT@imagewidth}%
\setlength{\parskip}{2ex}%
\renewcommand{\arraystretch}{\KFLT@stretch}%
}% keyboxinner
{% endkeyboxinner
\endminipage%
\endturn%
\end{lrbox}%
\KFLT@frame{\usebox{\KFLT@envbox}}%
\par\addvspace{\medskipamount}%
}% endkeyboxinner

\NewDocumentCommand{\KFLT@boxkeys}{+m m m}
{%
\KFLT@defaults%
\renewcommand{\KFLT@type}{#2}%
\renewcommand{\KFLT@listtype}{#3}%
\setkeys{KFLT@keys}{#1}%
}

\NewDocumentEnvironment{KFLT@boxouter}{m m}
{% keyboxouter
\ifthenelse{\boolean{KFLT@inkeysubfloats}}%
{\csname sub\KFLT@type\endcsname{\KFLT@rowboxwidth}}% subfloat
{% not subfloat:
\ifthenelse{\cnttest{\value{KFLT@keyfloatdepth}}>{0}}%
{% keyfloats
\minipage{\KFLT@rowboxwidth}%
\captionsetup{type=\KFLT@type}%
}% keyfloats
{% not keyfloats
\ifthenelse{\equal{#2}{H}}%
{% [H]
\noindent\minipage{\linewidth}%
\captionsetup{type=\KFLT@type}%
}% [H]
{% not [H]
\IfBooleanTF{#1}%
{\csname \KFLT@type*\endcsname[#2]}{\csname \KFLT@type\endcsname[#2]}%
}% not [H]
}% not keyfloats
}% not subfloat
\ifthenelse{\boolean{KFLT@cont}}{\ContinuedFloat}{}%
\KFLT@findwidths%
\KFLT@captioniftype{table}{}%
\begin{center}%
\unskip%
\minipage{\linewidth}%
\centering%
}% keyboxouter
{% endboxouter
\endminipage%
\end{center}%
\unskip\addvspace{\medskipamount}%
\KFLT@addartisttext{}%
\KFLT@captioniftype{figure}{}%
\ifthenelse{\boolean{KFLT@inkeysubfloats}}%
{\csname endsub\KFLT@type\endcsname}% subfloat
{% not subfloat
\ifthenelse{\cnttest{\value{KFLT@keyfloatdepth}}>{0}}% keyfloat?
{\endminipage}% keyfloats
{% not keyfloats
\ifthenelse{\equal{#2}{H}}%
{%
\endminipage% [H]
}%
{% not [H]
\IfBooleanTF{#1}% starred table?
{\csname end\KFLT@type*\endcsname}{\csname end\KFLT@type\endcsname}%
}% not [H]
}% not keyfloats
}% not subfloat
}% endkeyboxouter


\NewDocumentEnvironment{keyfigure}{s O{tbp} +m}
{%
\KFLT@boxkeys{#3}{figure}{lof}%
\KFLT@boxouter{#1}{#2}%
\KFLT@boxinner%
}%
{%
\endKFLT@boxinner%
\endKFLT@boxouter%
}

\BeforeBeginEnvironment{keyfigure}{%
\KFLT@trackrows%
}


\NewDocumentCommand{\keyfig}{s O{tbp} +m m}
{%
\KFLT@trackrows%
\KFLT@boxkeys{#3}{figure}{lof}%
\renewcommand{\KFLT@i}{#4}%
\KFLT@boxouter{#1}{#2}%
\KFLT@onefigureimage%
\endKFLT@boxouter%
}


\NewDocumentCommand{\keyfigbox}{s O{tbp} +m +m}
{%
\KFLT@trackrows%
\KFLT@boxkeys{#3}{figure}{lof}%
\KFLT@boxouter{#1}{#2}%
\KFLT@boxinner%
#4%
\endKFLT@boxinner%
\endKFLT@boxouter%
}


\NewDocumentCommand{\keyparbox}{s O{tbp} +m +m}
{%
\KFLT@trackrows%
\KFLT@boxkeys{#3}{figure}{lof}%
\renewcommand{\KFLT@c}{}%
\setboolean{KFLT@cstar}{true}%
\KFLT@boxouter{#1}{#2}%
\KFLT@boxinner%
#4%
\endKFLT@boxinner%
\endKFLT@boxouter%
}


\NewDocumentCommand{\keytab}{s O{tbp} +m +m}
{%
\KFLT@trackrows%
\KFLT@boxkeys{#3}{table}{lot}%
\KFLT@boxouter{#1}{#2}%
\KFLT@boxinner%
\centering%
#4%
\endKFLT@boxinner%
\endKFLT@boxouter%
}


\NewDocumentEnvironment{keytable}{s O{tbp} +m}
{%
\KFLT@boxkeys{#3}{table}{lot}%
\KFLT@boxouter{#1}{#2}%
\KFLT@boxinner%
\centering%
}%
{%
\endKFLT@boxinner%
\endKFLT@boxouter%
}

\BeforeBeginEnvironment{keytable}{%
\KFLT@trackrows%
}


\newcommand*{\KFLT@nonest}{%
\ifthenelse{%
\cnttest{\value{KFLT@keyfloatdepth}}>{0}%
\OR\boolean{KFLT@inkeysubfloats}%
}%
{%
\PackageError{keyfloat}{Cannot nest keysubfigs or keysubtabs.%
(Not in outer par mode.)}%
{The subcaption package do not support nested environments, so%
the keyfloat package cannot place a keysubfigs or keysubtabs%
environment inside another, or inside a keyfloats.}%
}%
{}%
}

\NewDocumentEnvironment{keyfloats}{s O{tbp} m}
{%
\addtocounter{KFLT@keyfloatdepth}{1}%
\ifthenelse{%
\equal{#2}{H}%
\OR\cnttest{\value{KFLT@keyfloatdepth}}>{1}%
\OR\boolean{KFLT@inkeysubfloats}%
}%
{% [H] or nested
\ifthenelse{%
\cnttest{\value{KFLT@keyfloatdepth}}>{1}%
\OR\boolean{KFLT@inkeysubfloats}%
}%
{\noindent%
\begin{minipage}{\KFLT@rowboxwidth}}%
{\bigbreak%
\noindent\begin{minipage}{\linewidth}}%
\ifthenelse{\boolean{KFLT@inkeysubfloats}}%
{}{\captionsetup{type=figure}}%
}% [H] or nested
{% figure
\IfBooleanTF{#1}% starred figure, two-col figure in a two-col format
{\begin{figure*}[#2]}{\begin{figure}[#2]}%
}% figure
\ifthenelse{%
\cnttest{\value{KFLT@keyfloatdepth}}>{1}%
\OR\boolean{KFLT@inkeysubfloats}%
}%
{\setlength{\KFLT@rowboxwidth}{.9\KFLT@rowboxwidth/\real{#3}}}%
{\setlength{\KFLT@rowboxwidth}{.9\linewidth/\real{#3}}}%
\centering%
\defcounter{KFLT@numcols}{#3}%
\defcounter{KFLT@thiscol}{0}%
}% starting keyfloats environment
{% ending keyfloats environment
\ifthenelse{%
\equal{#2}{H}%
\OR\cnttest{\value{KFLT@keyfloatdepth}}>{1}%
\OR\boolean{KFLT@inkeysubfloats}%
}%
{\end{minipage}%
\ifthenelse{\cnttest{\value{KFLT@keyfloatdepth}}>{0}}%
{}{\bigbreak}%
}% was [H]
{% not [H]
\IfBooleanTF{#1}% starred figure?
{\end{figure*}}{\end{figure}}%
}% not [H]
\addtocounter{KFLT@keyfloatdepth}{-1}%
}

\BeforeBeginEnvironment{keyfloats}{%
\ifthenelse{%
\cnttest{\value{KFLT@keyfloatdepth}}>{0}%
\OR\boolean{KFLT@inkeysubfloats}%
}%
{\KFLT@maybestartfloatrow}{}%
\ifthenelse{\cnttest{\value{KFLT@thiscol}}{>}{1}}%
{\hfill}{}%
}


\newcommand*{\KFLT@subgrpdefaults}{%
\setboolean{KFLT@subgrpcont}{false}%
\renewcommand{\KFLT@subgrpc}{}%
\setboolean{KFLT@subgrpcstar}{false}%
\renewcommand{\KFLT@subgrpsc}{}%
\setboolean{KFLT@subgrpscgiven}{false}%
\renewcommand{\KFLT@subgrptype}{figure}%
\renewcommand{\KFLT@subgrplisttype}{lof}%
\renewcommand{\KFLT@subgrpl}{}%
\renewcommand{\KFLT@subgrpap}{}%
\renewcommand{\KFLT@subgrpaf}{}%
\renewcommand{\KFLT@subgrpal}{}%
\renewcommand{\KFLT@subgrpas}{}%
\renewcommand{\KFLT@subgrpt}{}%
\renewcommand{\KFLT@subgrptextalign}{}
\tdtextjustify
}

\NewDocumentCommand{\KFLT@subfloats}{m m m +m}
{%
\setkeys{KFLT@subgrpkeys}{#4}%
\setboolean{KFLT@inkeysubfloats}{true}%
\IfBooleanTF{#1}%
{\setlength{\KFLT@rowboxwidth}{.9\textwidth/\real{#3}}}%
{\setlength{\KFLT@rowboxwidth}{.9\linewidth/\real{#3}}}%
\ifthenelse{\equal{#2}{H}}%
{%
\bigbreak\noindent\begin{minipage}{\linewidth}%
}%
{%
\IfBooleanTF{#1}%
{\begin{\KFLT@type*}[#2]}{\begin{\KFLT@type}[#2]}%
}%
\captionsetup{type=\KFLT@subgrptype}%
\ifthenelse{\boolean{KFLT@subgrpcont}}{\ContinuedFloat}{}%
\centering%
\KFLT@captioniftype{table}{subgrp}%
\defcounter{KFLT@numcols}{#3}%
\defcounter{KFLT@thiscol}{0}%
\begingroup%
}

\newcommand*{\KFLT@endsubfloats}[2]{%
\endgroup%
\par\addvspace{\bigskipamount}%
\KFLT@addartisttext{subgrp}%
\KFLT@captioniftype{figure}{subgrp}%
\ifthenelse{\equal{#2}{H}}%
{\end{minipage}\bigbreak}% was [H]
{% not [H]:
\IfBooleanTF{#1}% starred?
{\end{\KFLT@type*}}{\end{\KFLT@type}}%
}% not [H]
\setboolean{KFLT@inkeysubfloats}{false}%
}

\NewDocumentEnvironment{keysubfigs}{s O{tbp} m +m}
{%
\KFLT@nonest%
\KFLT@subgrpdefaults%
\KFLT@subfloats{#1}{#2}{#3}{#4}%
}% the start of the environment
{%
\KFLT@endsubfloats{#1}{#2}%
}

\NewDocumentEnvironment{keysubtabs}{s O{tbp} m +m}
{%
\KFLT@nonest%
\KFLT@subgrpdefaults%
\KFLT@setsubgrptable%
\KFLT@subfloats{#1}{#2}{#3}{#4}%
}% the start of the environment
{%
\KFLT@endsubfloats{#1}{#2}%
}



\endinput
%%
%% End of file `keyfloat.sty'.
