% +--------------------------------------------------+
% | Typeset titlesec.tex to get the documentation.   |
% +--------------------------------------------------+
%
% Copyright (c) 1998-2016 by Javier Bezos.
% All Rights Reserved.
%
% This file is part of the titlesec distribution release 2.10.2
% -----------------------------------------------------------
%
% It 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 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% The Current Maintainer of this work is Javier Bezos.

% History of v1.0 (actually not 1.0)
% ~~~~~~~~~~~~~~~
% 99/11/03: Split from titlesec.sty
% 99/11/03: Changed \setmarks, \ttl@sethead, \ttl@setfoot to follow
%           the new scheme
% 99/11/03: Removed the redefinition of \markboth. The arguments are
%           expanded on the fly by the corresponding \...mark. 
% 00/01/06: Added the \newtitlemark device.
% 00/01/19: Rejected the idea of a separate package (for the time 
%           being). Now it's ttlps.def.
% 00/01/20: Starred versions of \widenhead, \sethead, \setfoot.
% 00/01/23: Added \ifthe..., \iftitle, etc.
% 00/02/05: Modified \ifthe... mechanism
% 00/02/25: Fixed a bug which made enter in an endless loop.
%           Now \ttl@marks is just \relax. The new scheme made
%           compatible with babel. (2.3.1)
% 00/03/01: Fixed a bug in the previous bug fix. (2.3.2)
% 00/03/08: Removed the code for AMS and babel. A general procedure
%           to catch the changes of \markboth is introduced, with
%           \ttl@enccode and \ttl@running. (2.3.3)
% 00/05/05: \setmarks may be used outside of \newpagestyle, with
%           the help of \ttl@setmarks and \ttl@marksset (2.3.4)
% 00/05/25: Tests for \ttl@toclabel moved to the redefinition of
%           \secdef in titlesec. (2.3.5)
% 00/06/07: Added the first attempt for pagestyles with floats.
%           With slight changes, it will become the final form
%           for that. (2.3.5)
% 00/06/14: The default \ifthe... are now defined with \ttl@setifthe
%           so that they are robust from the very beginning. (2.3.5)
% 01/01/21: Replaced some \defs by \newcommand
% 10/12/03: A little addition to the warning, for those who don't
%           read the docs.
%
% History of v1.1
% ~~~~~~~~~~~~~~~
% 11/11/26: Add the extra mark mechanim
% 11/12/07: Enlarged, eg, multilevel \newtitlemark, \newtitlemark*,
%           premarks
%
% As before, all marks has two parts but now they don't refer to left or 
% right pages at all.  There are some issues related to top marks which 
% are explained by Knuth in \textit{The \TeX book}, pp.  259f, as well 
% as an uncompatibility between them and \LaTeX{} floats.  To overcome 
% both limitations, in the \textsf{titleps} page styles, the second 
% part in |\cs{firstmark}| is a \emph{fixed} top mark and the first one 
% the actual first mark; the right way to get the bot mark is from the 
% second part.  Marks are stored at each section and used before and 
% after the title (straight class); the first part contains the values 
% of current title, but the second one contains the previolusly stored 
% values in the mark before the title, and the current values in the 
% mark after.

\ProvidesFile{ttlps.def}[2016/03/15]

% 
% Page Styles
% ~~~~~~~~~~~

\ifx\ttl@compatps\@undefined\else
  \PackageWarningNoLine{titlesec}
      {You are using an old interface for page styles\MessageBreak
      (or you forgot the package option 'pagestyles').\MessageBreak
      You could proceed but don't complain if you run\MessageBreak
      into errors}
\fi

\ttl@pstrue

\let\parttitle\@empty
\let\chaptertitle\@empty
\let\sectiontitle\@empty
\let\subsectiontitle\@empty
\let\subsubsectiontitle\@empty
\let\paragraphtitle\@empty
\let\subparagraphtitle\@empty

\newcommand\iftitle[1]{%
  \expandafter\ifx\csname #1title\endcsname\@empty
    \expandafter\@secondoftwo
  \else
    \expandafter\@firstoftwo
  \fi}

% \newtitlemarks belongs to a nameless markset (ie, \@empty).
% For named extra marksets see below. 

\newcommand\newtitlemark{%
  \@ifstar{\@tempswafalse\ttl@newmk@i\@empty}%
          {\@tempswatrue\ttl@newmk@i\@empty}}

\def\ttl@newmk@i#1#2{%   markset, macro/var
  \edef\ttl@a{\expandafter\@gobble\string#2}%
  \expandafter\ifx\csname\ttl@a\endcsname#2\else
    \edef\ttl@a{c@#2}%
  \fi
  \expandafter\ttl@newmk@ii\expandafter{\ttl@a}{#1}}

\def\ttl@newmk@ii#1#2{%   macro/var, markset
  \expandafter\let\expandafter\ttl@a\csname ttl@mks@#2\endcsname
  \if@tempswa
    \expandafter\def\expandafter\ttl@a\expandafter{%
      \ttl@a
      \protect\@namedef{#1}{\@nameuse{#1}}}%
  \else
    \expandafter\def\expandafter\ttl@a\expandafter{%
      \ttl@a
      \protect\@nameuse{#1}=\the\@nameuse{#1}\relax}%
  \fi
  \expandafter\let\csname ttl@mks@#2\endcsname\ttl@a}

\let\ttl@mks@\@empty
\def\ttl@moremarks{\ttl@mks@} % backward compat
  
\def\ttl@prevmarks{\ttl@marks}
\let\ttl@marks\relax

% The following command sets the ifthe... commands to
% be used in heads. When the call is from inside a sectioning
% command, toclabel is either true (in most of cases) or false
% (a chapter in the front matter, for example). Otherwise (an
% explicit \...mark) is false

\def\ttl@setifthe#1{%
  \expandafter\protected@edef\csname ifthe#1\endcsname{%
    \ifttl@toclabel
      \protect\@firstoftwo
    \else
      \protect\@secondoftwo
    \fi}}

% At this point \ttl@toclabel is always false

\ttl@setifthe{part}
\ttl@setifthe{chapter}
\ttl@setifthe{section}
\ttl@setifthe{subsection}
\ttl@setifthe{subsubsection}
\ttl@setifthe{paragraph}
\ttl@setifthe{subparagraph}

% Putting marks
% -------------

\newtitlemark{\ttl@running}
\let\ttl@enccode\relax
\def\ttl@running{\ttl@enccode}

% \ttl@markboth redefines temporarily \mark to fetch changes made by
% babel or ams, saved in \ttl@running.  When this is done, the actual
% \mark es emitted, which has \ttl@running as part of the markset (see
% the \newtitlemark above). 

\def\ttl@mb@mark{\markboth}
\def\ttl@mb@warn{%
  \PackageWarningNoLine{titleps}
    {Direct use of \string\markboth\space and \string\markleft\space
     can lead\MessageBreak
     to unpredictable results. Please, read the manual\MessageBreak
     for an explanation of this warning.}}

\def\ttl@markboth#1{%
 \begingroup
   \let\protect\@unexpandable@protect
   \let\@mkboth\@gobbletwo
   \let\ttl@enccode\relax
   \let\label\relax
   \let\index\relax
   \let\glossary\relax
   \let\ttl@c\mark
   \def\mark##1{\xdef\ttl@running{\expandafter\@gobble##1}}%
   \ttl@mb@mark{}{\ttl@enccode}%
   \expandafter\let\csname#1mark\endcsname\@gobble
   \xdef\ttl@marks{\ttl@marks}%
   \gdef\@themark{{\ttl@marks}{\ttl@prevmarks}}%
   \ttl@c{\@themark\let\noexpand\ttl@mb@warn\relax}%
   \@nameuse{ttl@tem@#1}%
   \if@nobreak\ifvmode\nobreak\fi\fi
 \endgroup}

\newcommand\settitlemarks{\@ifstar\ttl@svmarks@s\ttl@svmarks@x}

\def\ttl@svmarks@x#1{\def\ttl@marksset{{#1}}}
\def\ttl@svmarks@s#1{\def\ttl@marksset{*{#1}}}

\newcommand\setmarks[2]{\settitlemarks{#1,#2}} % back compat

\def\ttl@settopmark#1\@@{%
  \expandafter\def\csname#1mark\endcsname##1{%
    \expandafter\gdef\csname#1title\endcsname{##1}%
    \ttl@setifthe{#1}%
    \expandafter\let\expandafter\ttl@marks\csname ttl@tm@#1\endcsname
    \ttl@markboth{#1}}%
  \global\@namedef{ttl@tm@#1}{%
    \protect\@namedef{#1title}{\@nameuse{#1title}}%
    \protect\@namedef{the#1}{\@nameuse{the#1}}%
    \protect\@namedef{ifthe#1}{\@nameuse{ifthe#1}}}%
  \def\ttl@elt##1{%
    \expandafter\ttl@setsubmark\ttl@a\@@{##1}}%
  \ttl@c}%

\def\ttl@setsubmark#1\@@#2{%
  \expandafter\g@addto@macro\csname ttl@tm@#2\endcsname{%
    \protect\@namedef{#1title}{}%
    \protect\@namedef{the#1}{}%
    \protect\@namedef{ifthe#1}{\protect\@secondoftwo}}
  \expandafter\g@addto@macro\csname ttl@tm@#1\endcsname{%
    \protect\@namedef{#2title}{\@nameuse{#2title}}%
    \protect\@namedef{the#2}{\@nameuse{the#2}}%
    \protect\@namedef{ifthe#2}{\@nameuse{ifthe#2}}}}%

\def\ttl@setmarks@x#1{%
  \let\ttl@c\@empty % sub list
  \@for\ttl@a:=#1\do{%
    \expandafter\ttl@settopmark\ttl@a\@@
    \let\ttl@elt\relax
    \xdef\ttl@c{\ttl@c\ttl@elt{\ttl@a}}}%
  \@for\ttl@a:=#1\do{%
    \expandafter\g@addto@macro\csname ttl@tm@\ttl@a\endcsname{%
      \ttl@moremarks}}}

\def\ttl@setmarks@s#1{% Solo con extramarks
  \PackageError{titleps}%
     {You need `extramarks' for \string\settitlemarks*}%
     {\string\settitlemarks* requires the package option `extramarks'}}

\newcommand\pretitlemark{%
  \global\ttl@premarktrue
  \@ifstar{\ttl@pretitlemark\z@}%
          {\ttl@pretitlemark\@ne}}

\def\ttl@pretitlemark#1#2#3{%
  \addtocounter{#2}#1%
  \ttl@premark{#2}{#3}%
  \addtocounter{#2}{-#1}}
  
% Rules
% -----

\newcommand\headrule{\setheadrule{.4\p@}}
\newcommand\footrule{\setfootrule{.4\p@}}

\newcommand\setheadrule[1]{%
  \ifdim#1=\z@
    \let\makeheadrule\@empty
  \else
    \def\makeheadrule{\rule[-.3\baselineskip]{\linewidth}{#1}}%
  \fi}
\newcommand\setfootrule[1]{%
  \ifdim#1=\z@
    \let\makefootrule\@empty
  \else
    \def\makefootrule{\rule[.7\baselineskip]{\linewidth}{#1}}%
  \fi}
  
\renewcommand\newpagestyle[1]{%
  \begingroup
  \catcode`\^^M=9
  \@ifnextchar[%
    {\ttl@pagestyle\newcommand{#1}}%
    {\ttl@pagestyle\newcommand{#1}[]}}
    
\renewcommand\renewpagestyle[1]{%
  \begingroup
  \catcode`\^^M=9
  \@ifnextchar[%
    {\ttl@pagestyle\renewcommand{#1}}%
    {\ttl@pagestyle\renewcommand{#1}[]}}

\def\ttl@pagestyle#1#2[#3]#4{%
  \endgroup
  \expandafter#1\csname ps@#2\endcsname{%
    \ttl@defaultps
    \def\ttl@headfmt{#3}%
    #4%
    \def\settitlemarks{\@ifstar\ttl@svmarks@s\ttl@svmarks@x}}}

\def\ttl@userunning#1#2{\csname ttlr@#1#2\endcsname}

\def\ttl@defaultps{%
  \let\makeheadrule\@empty
  \let\makefootrule\@empty
  \def\@mkboth{\ttl@savemark\@gobbletwo}%
  \def\@oddfoot{\ttl@userunning of}%
  \def\@evenfoot{\ttl@userunning ef}%
  \def\@oddhead{\ttl@userunning oh}%
  \def\@evenhead{\ttl@userunning eh}%
  \def\ttlr@of{\ttl@makefoot\@empty\@@\ttl@hiol\ttl@hior}%
  \def\ttlr@ef{\ttl@makefoot\@empty\@@\ttl@hiel\ttl@hier}%
  \def\ttlr@oh{\ttl@makehead\@empty\@@\ttl@hiol\ttl@hior}%
  \def\ttlr@eh{\ttl@makehead\@empty\@@\ttl@hiel\ttl@hier}%
  \let\ttlr@of@b\relax  \let\ttlr@of@p\relax
  \let\ttlr@ef@b\relax  \let\ttlr@ef@p\relax
  \let\ttlr@oh@t\relax  \let\ttlr@oh@p\relax
  \let\ttlr@eh@t\relax  \let\ttlr@eh@p\relax
  \def\settitlemarks{\@ifstar\ttl@setmarks@s\ttl@setmarks@x}%
  \expandafter\settitlemarks\ttl@marksset}

\@ifundefined{chapter}%
  {\settitlemarks{section,subsection}}%
  {\settitlemarks{chapter,section}}

\newcommand\usepage{\protect\thepage} % back compat

\newcommand\toptitlemarks{\expandafter\@secondoftwo\firstmark{}{}{}}
\def\firsttitlemarks{%
   \toks@\expandafter\expandafter\expandafter{%
      \expandafter\@secondoftwo \firstmark{}{}{}}%
   \@temptokena\expandafter\expandafter\expandafter{%
      \expandafter\@secondoftwo \botmark{}{}{}}%
   \edef\ttl@a{\the\toks@}%
   \edef\ttl@b{\the\@temptokena}%
   \ifx\ttl@a\ttl@b
     \expandafter\@secondoftwo\firstmark{}{}{}%
   \else
     \expandafter\@firstoftwo \firstmark{}{}{}%
   \fi}
\newcommand\bottitlemarks{\expandafter\@secondoftwo\botmark{}{}{}}
\newcommand\nexttoptitlemarks{\expandafter\@firstoftwo\botmark{}{}{}}
\newcommand\outertitlemarks{%
  \if@twoside
    \ifodd\c@page\relax
      \bottitlemarks
    \else
      \toptitlemarks
    \fi
  \else
    \toptitlemarks
  \fi}
\newcommand\innertitlemarks{%
  \if@twoside
    \ifodd\c@page\relax
      \firsttitlemarks
    \else
      \bottitlemarks
    \fi
  \else
    \bottitlemarks
  \fi}

\def\ttl@duplthreeargs#1#2#3#4{#1[#2][#3][#4]{#2}{#3}{#4}}
\def\ttl@dupltwoargs#1#2#3{#1[#2][#3]{#2}{#3}}
\def\ttl@duplthreeargsrev#1#2#3#4{#1[#4][#3][#2]{#2}{#3}{#4}}
\def\ttl@dupltwoargsrev#1#2#3{#1[#3][#2]{#2}{#3}}

\def\ttl@setany#1{%
  \@ifstar{\ttl@duplthreeargsrev#1}%
          {\@ifnextchar[{#1}{\ttl@duplthreeargs#1}}}

\newcommand\setfoot{\ttl@setany\ttl@setfoot}
\newcommand\sethead{\ttl@setany\ttl@sethead}

\def\ttl@setfoot[#1][#2][#3]#4#5#6{%
  \def\ttlr@ef{\ttl@makefoot{#1}{#2}{#3}\@@\ttl@hiel\ttl@hier}%
  \def\ttlr@of{\ttl@makefoot{#4}{#5}{#6}\@@\ttl@hiol\ttl@hior}}

\def\ttl@sethead[#1][#2][#3]#4#5#6{%
  \def\ttlr@eh{\ttl@makehead{#1}{#2}{#3}\@@\ttl@hiel\ttl@hier}%
  \def\ttlr@oh{\ttl@makehead{#4}{#5}{#6}\@@\ttl@hiol\ttl@hior}}

\def\ttl@headinline#1#2#3{%
  \ttl@headfmt
  \def\ttl@a{#1#3}\def\ttl@b{#2}%
  \ifx\ttl@a\@empty
    \hfil{#2}\hfil
  \else\ifx\ttl@b\@empty
    {#1}\hfil{#3}%
  \else
    \sbox\z@ {#1}%
    \sbox\tw@{#3}%
    \copy\z@
    \ifdim\wd\z@<\wd\tw@
      \kern-\wd\z@\kern\wd\tw@
    \fi
    \hfil{#2}\hfil
    \ifdim\wd\z@>\wd\tw@
      \kern-\wd\tw@\kern\wd\z@
    \fi
    \box\tw@
  \fi\fi}

\def\ttl@makeboth#1#2#3#4{%
  \ttl@calcneg\hspace{#3}%
  \normalsize
  \linewidth\textwidth
  \addtolength\linewidth{#3}%
  \addtolength\linewidth{#4}%
  \ifx#2\@empty\else   
    \setbox\z@\hb@xt@\linewidth{%
      \color@begingroup
      #2%
      \color@endgroup}%
      \wd\z@\z@
      \ht\z@\z@
      \dp\z@\z@
      \box\z@
  \fi
  \ttl@titlemarks % Must precede the format. Defines \ttl@running
  \def\ttl@enccode{\ttl@headinline#1{}{}{}}% which contains ttl@enccode
  \ttl@running
  \ttl@calcneg\hspace{#4}}% 

\def\ttl@makehead#1\@@{\ttl@makeboth{#1}\makeheadrule}
\def\ttl@makefoot#1\@@{\ttl@makeboth{#1}\makefootrule}

\renewcommand\widenhead{%
  \@ifstar{\ttl@dupltwoargsrev\ttl@widenhd}%
          {\@ifnextchar[{\ttl@widenhd}{\ttl@dupltwoargs\ttl@widenhd}}}
  
\def\ttl@widenhd[#1][#2]#3#4{%
  \def\ttl@hiel{#1}\def\ttl@hier{#2}%
  \def\ttl@hiol{#3}\def\ttl@hior{#4}}
  
\let\setheadindent\widenhead
  
\def\ttl@hiel{\z@}\def\ttl@hier{\z@}
\def\ttl@hiol{\z@}\def\ttl@hior{\z@}

% A tool:

\def\ifsamemark#1#2{%
  {#1\global\let\ttl@c#2}%
  \ifx\ttl@c#2%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% Another tool:

\newcommand\setmarkboth{%
  \ifx\markboth\ttl@mb@use\else
    \let\ttl@mb@mark\markboth
    \let\markboth\ttl@mb@use
  \fi
  \def\ttl@mb@new##1##2}

% \ttl@mb@temp is a trick to allow resetting inside \setmarkboth.

\def\ttl@mb@use#1#2{%
  \let\markboth\ttl@mb@mark
  \def\ttl@mb@temp{\let\markboth\ttl@mb@use}%
  \ttl@mb@new{#1}{#2}%
  \ttl@mb@temp
  \let\ttl@mb@temp\@undefined}
  
\newcommand\resetmarkboth{%
  \ifx\ttl@mb@temp\@undefined
    \let\markboth\ttl@mb@mark
    \def\ttl@mb@mark{\markboth}%
  \else
    \def\ttl@mb@temp{%
      \let\markboth\ttl@mb@mark
      \def\ttl@mb@mark{\markboth}}%
  \fi}

% ===========
% EXTRA MARKS
% ===========

\ifx\ttl@fetchmark\@empty

\ifx\newmarks\@undefined
  \PackageInfo{titleps}{%
    You have requested `extramarks' but etex or similar\MessageBreak
    has not been loaded. I'll do it for you.}
  \RequirePackage{etex}
\fi

\def\ttl@setmarks@s#1{%
   \@for\ttl@a:=#1\do{%
     \@ifundefined{ttl@mkc@\ttl@a}{%
       \expandafter\newmarks\csname ttl@mkc@\ttl@a\endcsname}{}%
     \expandafter\xdef\csname ttl@tem@\ttl@a\endcsname{%
       \marks\expandafter\noexpand\csname ttl@mkc@\ttl@a\endcsname
       {\noexpand\@themark}}}%
  \ttl@setmarks@x{#1}}%

\newcommand\newmarkset[1]{%
  \expandafter\newmarks\csname ttl@mkc@#1\endcsname
  \@namedef{ttl@mks@#1}{}%
  \@namedef{ttl@premks@#1}{\@nameuse{ttl@mks@#1}}}

\newcommand\newextramark{%
  \@ifstar{\@tempswafalse\ttl@newmk@i}%
          {\@tempswatrue\ttl@newmk@i}}

\def\ttl@extramark#1{%
 \begingroup
   \let\protect\@unexpandable@protect
   \let\@mkboth\@gobbletwo
   \let\label\relax
   \let\index\relax
   \let\glossary\relax\endgroup}

\newcommand\extramark[1]{%
  \begingroup
    \let\protect\@unexpandable@protect
    \let\@mkboth\@gobbletwo
    \let\label\relax
    \let\index\relax
    \let\glossary\relax
    \marks\csname ttl@mkc@#1\endcsname{%
      {\@nameuse{ttl@mks@#1}}%
      {\@nameuse{ttl@mks@#1}}}%
    \expandafter\xdef\csname ttl@premks@#1\endcsname
      {\@nameuse{ttl@mks@#1}}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}%
  
\newcommand\preextramark[1]{%
  \begingroup
    \let\protect\@unexpandable@protect
    \let\@mkboth\@gobbletwo
    \let\label\relax
    \let\index\relax
    \let\glossary\relax
    \marks\csname ttl@mkc@#1\endcsname{%
      {\csname ttl@mks@#1\endcsname}%
      {\csname ttl@premks@#1\endcsname}}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}

\newcommand\newshortmark[1]{%
  \newmarkset{\string#1}%
  \newextramark{\string#1}#1}

\newcommand\shortmark[1]{\extramark{\string#1}}
\newcommand\preshortmark[1]{\preextramark{\string#1}}

\newcommand\topshortmark[1]{{\topextramarks{\string#1}#1}}
\newcommand\firstshortmark[1]{{\firstextramarks{\string#1}#1}}
\newcommand\botshortmark[1]{{\botextramarks{\string#1}#1}}
\newcommand\nexttopshortmark[1]{{\nexttopextramarks{\string#1}#1}}

\def\ttl@fetchmark#1#2#3{%
  \expandafter#1#2\csname ttl@mkc@#3\endcsname{}{}{}}

\newcommand\topextramarks[1]{\ttl@fetchmark\@secondoftwo\firstmarks{#1}}
\newcommand\firstextramarks[1]{%
   \toks@\expandafter\expandafter\expandafter{%
      \ttl@fetchmark\@secondoftwo\firstmarks{#1}}%
   \@temptokena\expandafter\expandafter\expandafter{%
      \ttl@fetchmark\@secondoftwo\botmarks{#1}}%
   \edef\ttl@a{\the\toks@}%
   \edef\ttl@b{\the\@temptokena}%
   \ifx\ttl@a\ttl@b
     \ttl@fetchmark\@secondoftwo\firstmarks{#1}%
   \else
     \ttl@fetchmark\@firstoftwo\firstmarks{#1}%
   \fi}
\newcommand\botextramarks[1]{\ttl@fetchmark\@secondoftwo\botmarks{#1}}
\newcommand\nexttopextramarks[1]{\ttl@fetchmark\@firstoftwo\botmarks{#1}}

\newcommand\outerextramarks[1]{%
  \if@twoside
    \ifodd\c@page\relax
      \botextramarks{#1}%
    \else
      \topextramarks{#1}%
    \fi
  \else
    \topextramarks{#1}%
  \fi}
\newcommand\innerextramarks[1]{%
  \if@twoside
    \ifodd\c@page\relax
      \firstextramarks{#1}%
    \else
      \botextramarks{#1}%
    \fi
  \else
    \botextramarks{#1}%
  \fi}

\fi

% ======
% FLOATS
% ======
%
% Pagestyles with floats. There macros are defined only with the
% psfloats package option.

\@ifundefined{ttl@replace}{\endinput}{}

% User interface

\newcommand\setfloatfoot{%
  \let\ttl@c\@empty % <- current float, empty if general
  \ttl@setany\ttl@setftfoot}

\newcommand\setfloathead{%
  \let\ttl@c\@empty % <- current float, empty if general
  \ttl@setany\ttl@setfthead}

\def\ttl@setftfoot[#1][#2][#3]#4#5#6#7{%
  \@ifnextchar[{\ttl@setftfoot@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}}%
               {\ttl@setftfoot@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}[bp]}}

\def\ttl@setfthead[#1][#2][#3]#4#5#6#7{%
  \@ifnextchar[{\ttl@setfthead@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}}%
               {\ttl@setfthead@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}[tp]}}

\def\ttl@setftfoot@i#1#2#3#4#5#6#7[#8]{%
  \@tfor\ttl@b:=#8\do{%
    \@namedef{ttlr@ef@\ttl@b\ttl@c}%
       {#7\ttl@makefoot{#1}{#2}{#3}\@@\ttl@hiel\ttl@hier}%
    \@namedef{ttlr@of@\ttl@b\ttl@c}%
       {#7\ttl@makefoot{#4}{#5}{#6}\@@\ttl@hiol\ttl@hior}}}

\def\ttl@setfthead@i#1#2#3#4#5#6#7[#8]{%
  \@tfor\ttl@b:=#8\do{%
    \@namedef{ttlr@eh@\ttl@b\ttl@c}%
       {#7\ttl@makehead{#1}{#2}{#3}\@@\ttl@hiel\ttl@hier}%
    \@namedef{ttlr@oh@\ttl@b\ttl@c}%
       {#7\ttl@makehead{#4}{#5}{#6}\@@\ttl@hiol\ttl@hior}}}

\newcommand\nextfloathead{%
  \ttl@nextfree  % returns ttl@c with the next float
  \ttl@setany\ttl@setfthead}

\newcommand\nextfloatfoot{%
  \ttl@nextfree  % returns ttl@c with the next float
  \ttl@setany\ttl@setftfoot}

% Compat mode with floatps

\ifx\ttl@replace\space

  \let\ttl@setnewfoot\ttl@setftfoot
  \let\ttl@setnewhead\ttl@setfthead

  \def\ttl@setftfoot[#1][#2][#3]#4#5#6{%
    \ttl@setnewfoot[#1][#2][#3]{#4}{#5}{#6}{}}
  \def\ttl@setfthead[#1][#2][#3]#4#5#6{%
    \ttl@setnewhead[#1][#2][#3]{#4}{#5}{#6}{}}

\fi

% The simple default value of \ttl@userunning is
% replaced by one more elaborated

\def\ttl@userunning#1#2{%
  \if@fcolmade  % From a post by D. Arseneau to comp.text.tex
    \def\@elt##1{\edef\ttl@pageft{\string##1}}%
    \@flsucceed
    \let\@elt\relax
    \if#2h%
      \ttl@replace #1hp\ttl@pageft
    \else
      \ttl@replace #1fp\ttl@pageft
      \ttl@killftps\ttl@pageft
    \fi
  \else\if#2h%
    \ifx\ttl@topft\@empty\else
      \ttl@replace #1ht\ttl@topft
    \fi
  \else
    \ifx\ttl@botft\@empty\else
      \ttl@replace #1fb\ttl@botft
    \fi
  \fi\fi
  \csname ttlr@#1#2\endcsname}

% [Don't move above as \ttl@replace is used as a flag.]

\def\ttl@replace#1#2#3#4{%
    \@ifundefined{ttlr@#1#2@#3#4}%
      {\@ifundefined{ttlr@#1#2@#3}{}%
         {\@namedef{ttlr@#1#2}{\@nameuse{ttlr@#1#2@#3}}}}%
      {\csname ttlr@xx@x#4\endcsname\@gobble
       {\expandafter\let\csname ttlr@#1#2\expandafter\endcsname
         \csname ttlr@#1#2@#3#4\endcsname}}}

\def\ttl@nextfree{%
  \def\@elt##1{%
    \edef\ttl@c{\string##1}%
    \let\@elt\@gobble}%
  \@freelist
  \let\@elt\relax
  \@ifundefined{ttlr@xx@x\ttl@c}%
    {\expandafter\let\csname ttlr@xx@x\ttl@c\endcsname\@secondoftwo
     \@tfor\ttl@a:={eh@t}{oh@t}{ef@b}{of@b}{eh@p}{oh@p}{ef@p}{of@p}\do{%
       \expandafter\global\expandafter
       \let\csname ttlr@\ttl@a\ttl@c\endcsname\relax}}{}}

\def\ttl@killftps#1{%
  \expandafter\global\expandafter
  \let\csname ttlr@xx@x#1\endcsname\relax}

\let\ttl@topft\@empty
\let\ttl@botft\@empty
\let\ttl@pageft\@empty

\def\ttl@combinefloats{%
 \ifx\@toplist\@empty\else   
   \def\@elt##1{%
     \edef\ttl@topft{\string##1}%
     \def\@elt####1{\ttl@killftps{\string####1}}}%
   \@toplist
  \fi
  \ifx\@botlist\@empty\else
    \def\@elt##1{%
      \def\@elt####1{%
        \def\@elt####1{\ttl@killftps\ttl@botft}%
        \edef\ttl@botft{\string####1}}%
      \edef\ttl@botft{\string##1}}%
    \@botlist
  \fi
  \let\@elt\relax
  \ttl@combinefloats@x}

\AtBeginDocument{%
  \let\ttl@combinefloats@x\@combinefloats
  \let\@combinefloats\ttl@combinefloats}

\endinput
