%%
%% This is file `tablefootnote.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% tablefootnote.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Project: tablefootnote
%% Version: 2011/11/06 v1.0c
%% 
%% Copyright (C) 2011 by
%%     H.-Martin M"unch <Martin dot Muench at Uni-Bonn dot de>
%% 
%% The usual disclaimer applys:
%% If it doesn't work right that's your problem.
%% (Nevertheless, send an e-mail to the maintainer
%%  when you find an error in this package.)
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c of this license or (at your option) any later
%% version. This version of this license is in
%%    http://www.latex-project.org/lppl/lppl-1-3c.txt
%% and the latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%% 
%% The Current Maintainer of this work is H.-Martin Muench.
%% 
%% This work consists of the main source file tablefootnote.dtx,
%% the README, and the derived files
%%    tablefootnote.sty, tablefootnote.pdf,
%%    tablefootnote.ins, tablefootnote.drv,
%%    tablefootnote-example.tex, tablefootnote-example.pdf.
%% 
\NeedsTeXFormat{LaTeX2e}[2011/06/27]
\ProvidesPackage{tablefootnote}[2011/11/06 v1.0c
              Table foot notes (HMM)]

%% Provides the \tablefootnoteote{...} command
%% for footnotes in (sideways)tables.

\RequirePackage{ltxcmds}[2011/04/18]%      v1.20

\@ifpackageloaded{rotating}{%
  \gdef\tfn@rotating{1}
  \@ifpackagelater{rotating}{2009/03/28}%  v2.16a
   {% >= 2009/03/28, OK
   }{% else
     \PackageWarningNoLine{tablefootnote}{%
       It is required version\MessageBreak
       2009/03/28 v2.16a (or later) of package rotating,\MessageBreak
       but only version\MessageBreak
      `\csname ver@rotating.sty\endcsname'\MessageBreak
      is available}%
   }%
  \RequirePackage{amstext}[2000/06/29]%    v2.01
  \RequirePackage{setspace}[2000/12/01]%   v6.7
  \RequirePackage{subcaption}[2011/09/12]% v1.1b
 }{% else
  \gdef\tfn@rotating{0}%
 }

%% tablefootnote may work with earlier versions of LaTeX and those
%% packages, but this was not tested. Please consider updating
%% your LaTeX and packages to the most recent version
%% (if they are not already the most recent version).

\def\tfn@footnotetablecount{0}
\def\tfn@footnotetableprint{0}
\def\tfn@footnotezero{0}
\def\tfn@fnt{0}
\def\tfn@swt{0}% _s_ide_w_ays_t_able

\newcommand{\tfn@footnotemarks}{%
  \footnotemark%
  \count255=\tfn@footnotetablecount \relax%
  \count255=\numexpr \count255 - 1 \relax%
  \xdef\tfn@footnotetablecount{\the\count255}%
}

\newcommand{\tfn@footnotetext}[1]{%
  \ifx\tfn@footnotetableprint\tfn@footnotezero%
    % This is the first footnotetext after the (sideways)table.
    \addtocounter{footnote}{\tfn@footnotetablecount}%
    \ltx@ifpackageloaded{hyperref}{%
      \addtocounter{Hfootnote}{\tfn@footnotetablecount}%
     }{}%
    \gdef\tfn@footnotetableprint{1}%
  \fi%
  \addtocounter{footnote}{+1}%
  \ltx@ifpackageloaded{hyperref}{%
    \addtocounter{Hfootnote}{+1}%
    % Code form the hyperref package, 2011/10/01, v6.82j,
    % by Heiko Oberdiek
    \global\let\Hy@saved@currentHref\@currentHref%
    \hyper@makecurrent{Hfootnote}%
    \global\let\Hy@footnote@currentHref\@currentHref%
    \global\let\@currentHref\Hy@saved@currentHref%
    % End of code form the hyperref package
   }{}%
  \ifx\tfn@swt\tfn@footnotezero%
    \footnotetext{#1}%
  \else%
    \footnotetext{${}^{\text{\textup{\thefootnote}}}$\thinspace #1}%
  \fi%
  \count255=\tfn@footnotetablecount \relax%
  \count255=\numexpr \count255 + 1 \relax%
  \xdef\tfn@footnotetablecount{\the\count255}%
  \ifx\tfn@footnotetablecount\tfn@footnotezero% all footnotes placed
    \gdef\tfn@footnotetableprint{0}%
    \global\let\tfn@tablefootnoteprintout\relax% brute force
 %\else there is at least one \footnotemark which needs \footnotetext
  \fi
}

\newcommand{\tfn@tablefootnoteprintout}{\relax}

\newcommand{\tablefootnote}[1]{%
\tfn@footnotemarks%
\ltx@GlobalAppendToMacro{%
\tfn@tablefootnoteprintout}{\tfn@footnotetext{#1}}%
\gdef\tfn@fnt{1}%
}

\newcommand{\tfn@swtbox}{%
\vspace{0.8cm}%
\begin{spacing}{0.1}%
\subcaptionbox*{}[\linewidth][l]{\tfn@tablefootnoteprintout}%
\end{spacing}%
}

\IfFileExists{etoolbox.sty}{%
  \RequirePackage{etoolbox}[2011/01/03]%   v2.1
  \AfterEndEnvironment{table}{%
    \tfn@tablefootnoteprintout%
    \gdef\tfn@fnt{0}%
   }
  \ltx@ifpackageloaded{rotating}{%
    \AtBeginEnvironment{sidewaystable}{%
      \gdef\tfn@swt{1}%
    }
    \AtEndEnvironment{sidewaystable}{%
      \ifx\tfn@fnt\tfn@footnotezero% \relax
      \else%
        \tfn@swtbox%
      \fi%
      \gdef\tfn@swt{0}%
      \gdef\tfn@fnt{0}%
     }%
   }{}%
}{%
  \PackageWarning{tablefootnote}{%
    Package etoolbox not available, therefore\MessageBreak%
    substituting commands \string\AtEndEnvironment\space %
    and\MessageBreak%
    \string\AfterEndEnvironment\space for tablefootnotes%
    \MessageBreak%
   }%
  \RequirePackage{letltxmacro}[2010/09/02]% v1.4
  \@ifpackagelater{letltxmacro}{2010/09/02}%
   {% >= 2010/09/02, OK
    \GlobalLetLtxMacro{\tfnorigtable}{\table}
    \GlobalLetLtxMacro{\tfnendorigtable}{\endtable}
   }{% else
     \PackageError{tablefootnote}{%
       Outdated version of letltxmacro package used%
      }{Version 2010/09/02 v1.4 or newer needed but not found!%
        \MessageBreak%
        Update the used letltxmacro package.}
        \LetLtxMacro{\tfnorigtable}{\table}
        \LetLtxMacro{\tfnendorigtable}{\endtable}
    }
  \ltx@ifpackageloaded{float}{%
    \@ifpackagelater{float}{2001/11/08}%   v1.3d
     {% >= 2001/11/08, OK
     }{% else
       \PackageWarningNoLine{tablefootnote}{%
         It is required version\MessageBreak
         2001/11/08 v1.3d (or later) of package float,\MessageBreak
         but only version\MessageBreak
        `\csname ver@float.sty\endcsname'\MessageBreak
        is available}%
     }%
   }{\relax}
  \renewenvironment{table}{%
    \ltx@ifpackageloaded{float}{%
      \ltx@LocalAppendToMacro{\float@endH}{%
        \tfn@tablefootnoteprintout%
        \gdef\tfn@fnt{0}%
       }%
     }{\relax}
    \tfnorigtable%
   }{%
     \tfnendorigtable%
     \tfn@tablefootnoteprintout%
     \gdef\tfn@fnt{0}%
    }%
  \ltx@ifpackageloaded{rotating}{%
    \let\tfnorigsidewaystable\sidewaystable
    \let\tfnendorigsidewaystable\endsidewaystable
    \renewenvironment{sidewaystable}{%
      \gdef\tfn@swt{1}%
      \tfnorigsidewaystable%
    }{%
      \ifx\tfn@fnt\tfn@footnotezero% \relax
      \else%
        \tfn@swtbox%
      \fi%
      \gdef\tfn@swt{0}%
      \gdef\tfn@fnt{0}%
      \tfnendorigsidewaystable%
     }%
   }{}%
}

\AtBeginDocument{%
  \ltx@ifpackageloaded{rotating}{%
    \ifx\tfn@rotating\tfn@footnotezero%
      \PackageError{tablefootnote}{%
        Package rotating loaded after package tablefootnote}{%
        When using both the rotating and the tablefootnote package,%
        \MessageBreak%
        the rotating package must be loaded before the tablefootnote%
        package!%
       }%
    \fi%
   }{}
  }

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