%%
%% 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}
    [2019/01/11 v1.00 Key/value interface for floats and subfloats.]




\RequirePackage{etoolbox}[2011/01/03]%
\RequirePackage{xparse}
\RequirePackage{xkeyval}
\RequirePackage{graphicx}
\RequirePackage{caption}[2010/10/31]% v3.2 to support \phantomcaption
\RequirePackage{subcaption}
\RequirePackage{calc}
\RequirePackage{rotating}
\RequirePackage{placeins}
\RequirePackage{wrapfig}
\@ifpackageloaded{floatrow}
{
\PackageError{keyfloat}
{%
    The keyfloat package conflicts with the floatrow package.
    Remove floatrow to use keyfloat.%
}
{%
    Keyfloat uses the caption and subcaption packages to
    provide similar functionality to floatrow.%
}
}
{}

\PassOptionsToPackage{expand}{gettitlestring}




\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}{}
\providecommand{\tdtextjustify}{}
\providecommand{\tdtextcenter}{}
\providecommand{\tdtextleft}{}
\providecommand{\tdtextright}{}
\providecommand{\tdnamejustify}{}
\providecommand{\tdnamecenter}{}
\providecommand{\tdnameleft}{}
\providecommand{\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}

\define@key{KFLT@keys}{mo}{\setlength{\KFLT@mo}{#1}}
\newlength{\KFLT@mo}

\define@key{KFLT@keys}{wp}{\renewcommand{\KFLT@wp}{#1}}
\newcommand{\KFLT@wp}{O}

\define@key{KFLT@keys}{va}{\renewcommand{\KFLT@va}{#1}}
\newcommand{\KFLT@va}{c}


\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}{%
    \ifbool{KFLT@ft}% tight frame?
        {\setlength{\KFLT@boxwidth}{\linewidth - 2\KFLTtightframewidth}}%
        {% not tight frame
            \ifbool{KFLT@f}% loose frame?
                {\setlength{\KFLT@boxwidth}{\linewidth - 2\KFLTlooseframewidth}}%
                {\setlength{\KFLT@boxwidth}{\linewidth}}% no frame
        }% not tight frame
    \ifdimgreater{\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]
{%
    \ifbool{KFLT@ft}%
        {\KFLTtightframe{#1}}%
        {% not tightframe
            \ifbool{KFLT@f}%
                {\KFLTlooseframe{#1}}%
                {#1}% no frame
        }% not looseframe
}
\newcommand{\KFLT@findenvboxwidth}{%
    \settowidth{\KFLTimageboxwidth}{\usebox{\KFLT@envbox}}%
    \ifbool{KFLT@ft}%
        {\addtolength{\KFLTimageboxwidth}{2\KFLTtightframewidth}}%
        {% not tightframe
            \ifbool{KFLT@f}%
                {\addtolength{\KFLTimageboxwidth}{2\KFLTlooseframewidth}}%
                {}% no frame
        }% not looseframe
}
\NewDocumentCommand{\KFLT@onefigureimage}{}
{%
    \begin{lrbox}{\KFLT@envbox}%
    \ifdefempty{\KFLT@lw}%
    {% not linewidth
        \ifdimgreater{\KFLT@w}{0pt}%
        {% width is given
            \ifdimgreater{\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
            \ifdimgreater{\KFLT@h}{0pt}%
                {\includegraphics[scale=\KFLT@s,height=\KFLT@h]{\KFLT@i}}%
                {\includegraphics[scale=\KFLT@s]{\KFLT@i}}%
        }% width is not given
    }% not linewidth
    {% linewidth given
        \includegraphics[scale=\KFLT@s,width=\KFLT@imagewidth]{\KFLT@i}%
    }%
    \end{lrbox}%
    \unskip%
    \KFLT@findenvboxwidth%
    \begin{turn}{\KFLT@r}%
    \KFLT@frame{\usebox{\KFLT@envbox}}%
    \unskip%
    \end{turn}%
}

\newcommand*{\KFLT@captioniftype}[2]{%
    \ifcsstring{KFLT@#2type}{#1}%
        {\KFLT@caption{#2}}%
        {}%
}

\NewDocumentCommand{\KFLT@dosimplecaption}{m m m}
{%
    \unskip%
    \IfBooleanTF{#1}% star?
        {\IfValueTF{#2}{\caption*[#2]{#3}}{\caption*{#3}}}%
        {\IfValueTF{#2}{\caption[#2]{#3}}{\caption{#3}}}%
}
\@ifpackageloaded{tocdata}
{% tocdata loaded
\NewDocumentCommand{\KFLT@docaption}{s o m m}
{%
    \ifcsstring{KFLT@#4type}{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@#4al}%
    {}% no artist
    {% yes artist
        \ifcsempty{KFLT@#4af}%
            {\index{\csname KFLT@#4al\endcsname}}%
            {\index{\csname KFLT@#4al\endcsname, \csname KFLT@#4af\endcsname}}%
    }% yes artist
}% KFLT@docaption
}% no tocdata

\newcommand{\KFLT@caption}[1]{%
    \ifbool{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}%
    \setlength{\KFLT@mo}{-1.2ex}%
    \renewcommand{\KFLT@wp}{O}%
    \renewcommand{\KFLT@va}{c}%
}


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

\newcommand*{\KFLT@maybeendfloatrow}{%
    \ifnumless{\value{KFLT@thiscol}}{\value{KFLT@numcols}}%
    {}% thiscol < numcols
    {% >=

        \addvspace{.75\floatsep}

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


\newcommand{\KFLT@trackrows}
{%
    \ifboolexpr{%
        test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{0}} or
        bool{KFLT@inkeysubfloats}%
    }%
    {% nested
        \KFLT@maybestartfloatrow%
        \ifnumgreater{\value{KFLT@thiscol}}{1}%
            {\hfill}%
            {}%
    }% nested
    {}% not nested
}

\newcommand{\KFLT@addtext}[1]
{%
    \ifcsempty{KFLT@#1t}%
    {}% no text
    {% text to add
        {% local
        \unskip%
        \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]
{%
    \ifblank{#1}%
        {}%
        {#1~}%
}
\@ifpackageloaded{tocdata}
{% tocdata loaded
\newcommand{\KFLT@addartisttext}[1]
{%
    \ifcsstring{KFLT@#1type}{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

\newlength{\KFLTimageboxwidth}

\newsavebox{\KFLT@envbox}

\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{2ex}%
}% 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}
{% boxouter
    \ifbool{KFLT@inkeysubfloats}%
    {\csname sub\KFLT@type\endcsname{\KFLT@rowboxwidth}}% subfloat
    {% not subfloat:
        \ifnumgreater{\value{KFLT@keyfloatdepth}}{0}%
        {% keyfloats
            \ifbool{KFLT@keywrap}%
                {\minipage[t]{\KFLT@rowboxwidth}}%
                {\minipage[\KFLT@va]{\KFLT@rowboxwidth}}%
            \captionsetup*{type=\KFLT@type}%
        }% keyfloats
        {% not keyfloats
            \ifbool{KFLT@keywrap}%
            {%
                \par\addvspace{\baselineskip}%
                \noindent\minipage[t]{\linewidth}%
                \captionsetup{type=\KFLT@type}%
            }%
            {% not a keywrap
                \ifstrequal{#2}{W}%
                {% [W]
                \KFLT@findwidths%
                \csname wrap\KFLT@type\endcsname{\KFLT@wp}%
                {\KFLT@imagewidth+2\KFLTlooseframewidth}%
                \renewcommand{\KFLT@lw}{}%
                \renewcommand{\KFLT@w}{\KFLT@imagewidth}%
                }% [W]
                {% not [W]
                    \ifstrequal{#2}{M}%
                    {% [M]
                        \csname margin\KFLT@type\endcsname[\KFLT@mo]%
                        \captionsetup{type=\KFLT@type}%
                    }% [M]
                    {% not [M}
                        \ifstrequal{#2}{H}%
                        {% [H]
                            \par\addvspace{\baselineskip}%
                            \noindent\minipage[\KFLT@va]{\linewidth}%
                            \captionsetup{type=\KFLT@type}%
                        }% [H]
                        {% not [H]
                            \IfBooleanTF{#1}%
                                {\csname \KFLT@type*\endcsname[#2]}%
                                {\csname \KFLT@type\endcsname[#2]}%
                        }% not [H]
                    }% not [M]
                }% not [W]
            }% not keywrap
        }% not keyfloats
    }% not subfloat
    \ifbool{KFLT@cont}{\ContinuedFloat}{}%
    \KFLT@findwidths%
    \KFLT@captioniftype{table}{}%
    \center\unskip%
}% boxouter
{% endboxouter
    \endcenter\unskip%
    \KFLT@addartisttext{}%
    \KFLT@captioniftype{figure}{}%
    \ifbool{KFLT@inkeysubfloats}%
    {%
        \csname endsub\KFLT@type\endcsname%
    }% subfloat
    {% not subfloat
        \ifnumgreater{\value{KFLT@keyfloatdepth}}{0}% keyfloats?
        {\endminipage}% keyfloats
        {% not keyfloats
            \ifbool{KFLT@keywrap}{%
                \endminipage%
                \par\addvspace{\baselineskip}%
            }%
            {% not keywrap
                \ifstrequal{#2}{W}%
                {% [W]
                    \csname endwrap\KFLT@type\endcsname%
                }% [W]
                {% not[W]
                    \ifstrequal{#2}{M}%
                    {% [M]
                        \csname endmargin\KFLT@type\endcsname%
                    }% [M]
                    {% not [M]
                        \ifstrequal{#2}{H}%
                        {%
                            \endminipage% [H]
                            \par\addvspace{\baselineskip}%
                        }%
                        {% not [H]
                            \IfBooleanTF{#1}% starred float?
                                {\csname end\KFLT@type*\endcsname}%
                                {\csname end\KFLT@type\endcsname}%
                        }% not [H]
                    }% not [M]
                }% not [W]
            }% not keywrap
        }% 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}%
    \begingroup%
    \KFLT@boxouter{#1}{#2}%
    \KFLT@onefigureimage%
    \endKFLT@boxouter%
    \endgroup%
}


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


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


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


\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}{%
    \ifboolexpr{%
        test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{0}} or
        bool {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}%
    \ifboolexpr{%
        test {\ifstrequal{#2}{H}} or
        test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{1}} or
        bool {KFLT@inkeysubfloats} or
        bool {KFLT@keywrap}
    }%
    {% [H] or nested
        \ifboolexpr{%
            test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{1}} or
            bool {KFLT@inkeysubfloats}
        }%
        {%
            \noindent%
            \begin{minipage}{\KFLT@rowboxwidth}%
        }%
        {%
            \bigbreak%
            \noindent\begin{minipage}{\linewidth}%
        }%
        \ifbool{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
    \ifboolexpr{%
        test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{1}} or
        bool {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
    \ifboolexpr{%
        test {\ifstrequal{#2}{H}} or
        test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{1}} or
        bool {KFLT@inkeysubfloats} or
        bool {KFLT@keywrap}
    }%
    {% was [H], etc.
        \end{minipage}%
        \ifboolexpr{%
            test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{0}} or
            bool {KFLT@keywrap}
        }%
        {}{\bigbreak}%
    }% was [H], etc.
    {% not [H], etc.
        \IfBooleanTF{#1}% starred figure?
            {\end{figure*}}{\end{figure}}%
    }% not [H], etc.
    \addtocounter{KFLT@keyfloatdepth}{-1}%
}

\BeforeBeginEnvironment{keyfloats}{%
    \ifboolexpr{%
        test {\ifnumgreater{\value{KFLT@keyfloatdepth}}{0}} or
        bool {KFLT@inkeysubfloats}
    }%
        {\KFLT@maybestartfloatrow}%
        {}%
    \ifnumgreater{\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}%
    \KFLT@setsubgrpfigure%
    \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}}}%
    \ifboolexpr{%
        test {\ifstrequal{#2}{H}} or
        bool {KFLT@keywrap}
    }%
    {%
        \bigbreak\noindent\begin{minipage}{\linewidth}%
    }%
    {%
        \IfBooleanTF{#1}%
            {\begin{\KFLT@subgrptype*}[#2]}%
            {\begin{\KFLT@subgrptype}[#2]}%
    }%
    \captionsetup*{type=\KFLT@subgrptype}%
    \ifbool{KFLT@subgrpcont}%
        {\ContinuedFloat}%
        {}%
    \center\unskip%
    \KFLT@captioniftype{table}{subgrp}%
    \defcounter{KFLT@numcols}{#3}%
    \defcounter{KFLT@thiscol}{0}%
    \begingroup%
}

\newcommand*{\KFLT@endsubfloats}[2]{%
    \endgroup%
    \unskip\endcenter%
    \par\addvspace{\bigskipamount}%
    \KFLT@addartisttext{subgrp}%
    \KFLT@captioniftype{figure}{subgrp}%
    \ifboolexpr{%
        test {\ifstrequal{#2}{H}} or
        bool{KFLT@keywrap}
    }%
        {\end{minipage}\bigbreak}% was [H]
        {% not [H]:
            \IfBooleanTF{#1}% starred?
                {\end{\KFLT@subgrptype*}}%
                {\end{\KFLT@subgrptype}}%
        }% 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}%
}
\newsavebox{\KFLT@marginfloatbox}

\NewDocumentEnvironment{KFLT@marginfloat}{O{-1.2ex} m}
{% start
    \FloatBarrier% keep floats in order
    \begin{lrbox}{\KFLT@marginfloatbox}%
    \begin{minipage}{\marginparwidth}%
    \captionsetup{type=#2}%
    \hbox{}\vspace*{#1}%
    \noindent%
}% start
{% end
    \end{minipage}%
    \end{lrbox}%
    \marginpar{\usebox{\KFLT@marginfloatbox}}%
}% end
\ProvideDocumentEnvironment{marginfigure}{O{-1.2ex}}
  {\begin{KFLT@marginfloat}[#1]{figure}}
  {\end{KFLT@marginfloat}}
\ProvideDocumentEnvironment{margintable}{O{-1.2ex}}
  {\begin{KFLT@marginfloat}[#1]{table}}
  {\end{KFLT@marginfloat}}
\newboolean{KFLT@keywrap}
\boolfalse{KFLT@keywrap}
\newlength{\KFLT@keywrapwidth}
\newlength{\KFLT@keywrapparskip}
\newlength{\KFLT@keywrapparindent}
\DeclareDocumentEnvironment{keywrap}{m +m}
{%
    \par%
    \setlength{\KFLT@keywrapwidth}{\linewidth}%
    \addtolength{\KFLT@keywrapwidth}{-#1}%
    \addtolength{\KFLT@keywrapwidth}{-2em}%
    \minipage[t]{\KFLT@keywrapwidth}%
    %
    \setlength{\parskip}{\KFLT@keywrapparskip}%
    \setlength{\parindent}{\KFLT@keywrapparindent}%
    \booltrue{KFLT@keywrap}%
}
{%
    \par%
    \endminipage%
    \hfill%
    \begin{minipage}[t]{#1}%
    \booltrue{KFLT@keywrap}%
    #2%
    \par%
    \unskip\vspace{\smallskipamount}%
    \end{minipage}%
    \par%
}

\BeforeBeginEnvironment{keywrap}{%
    \setlength{\KFLT@keywrapparskip}{\parskip}%
    \setlength{\KFLT@keywrapparindent}{\parindent}%
}
\endinput
%%
%% End of file `keyfloat.sty'.
