% +--------------------------------------------------+
% | Typeset titlesec.tex to get the documentation.   |
% +--------------------------------------------------+
%
% Copyright (c) 1998-2005 by Javier Bezos.
% All Rights Reserved.
%
% This file is part of the titlesec distribution release 2.6
% -----------------------------------------------------------
%
% This program can be redistributed and/or modified under the terms
% of the LaTeX Project Public License Distributed from CTAN
% archives in directory macros/latex/base/lppl.txt; either
% version 1 of the License, or any later version.

% 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
%
% 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{titlesec} 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}[2005/01/22]

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

\ifx\ttl@compatps\@undefined\else
  \PackageWarningNoLine{titlesec}
      {You are using an old interface for page styles\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}

\newcommand\newtitlemark{%
  \@ifstar{\@tempswafalse\ttl@newmk@i}%
          {\@tempswatrue\ttl@newmk@i}}
  
\def\ttl@newmk@i#1{%
  \edef\ttl@a{{\expandafter\@gobble\string#1}}%
  \expandafter\ttl@newmk@ii\ttl@a}

\def\ttl@newmk@ii#1{%
  \if@tempswa
    \expandafter\def\expandafter\ttl@moremarks\expandafter{%
      \ttl@moremarks
      \protect\@namedef{#1}{\@nameuse{#1}}}%
  \else
    \expandafter\def\expandafter\ttl@moremarks\expandafter{%
      \ttl@moremarks
      \protect\@nameuse{#1}=\the\@nameuse{#1}\relax}%
  \fi}

\def\ttl@prevmarks{\ttl@marks}
\let\ttl@marks\relax

\let\ttl@moremarks\@empty

% 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}

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

\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}}%
   \markboth{}{\ttl@enccode}%
   \expandafter\let\csname#1mark\endcsname\@gobble
   \xdef\ttl@marks{\ttl@marks}%
   \gdef\@themark{{\ttl@marks}{\ttl@prevmarks}}%
   \ttl@c{\@themark}%
   \if@nobreak\ifvmode\nobreak\fi\fi
 \endgroup}

\newcommand\setmarks[2]{\def\ttl@marksset{{#1}{#2}}}

\def\ttl@setmarks#1#2{%
  \expandafter\def\csname#1mark\endcsname##1{%
    \expandafter\gdef\csname#1title\endcsname{##1}%
    \ttl@setifthe{#1}%
    \def\ttl@marks{%
      \protect\@namedef{#1title}{##1}%
      \protect\@namedef{#2title}{}%
      \protect\@namedef{the#1}{\@nameuse{the#1}}%
      \protect\@namedef{the#2}{}%
      \protect\@namedef{ifthe#1}{\@nameuse{ifthe#1}}%
      \protect\@namedef{ifthe#2}{\protect\@secondoftwo}%
      \ttl@moremarks}%
    \ttl@markboth{#1}}%
  \expandafter\def\csname#2mark\endcsname##1{%
    \expandafter\gdef\csname#2title\endcsname{##1}%
    \ttl@setifthe{#2}%
    \def\ttl@marks{%
      \protect\@namedef{#1title}{\@nameuse{#1title}}%
      \protect\@namedef{#2title}{##1}%
      \protect\@namedef{the#1}{\@nameuse{the#1}}%
      \protect\@namedef{the#2}{\@nameuse{the#2}}%
      \protect\@namedef{ifthe#1}{\@nameuse{ifthe#1}}%
      \protect\@namedef{ifthe#2}{\@nameuse{ifthe#2}}%
      \ttl@moremarks}%
    \ttl@markboth{#1}}}%

\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\setmarks####1####2{\def\ttl@marksset{{####1}{####2}}}}}

\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
    \let\setmarks\ttl@setmarks
    \expandafter\setmarks\ttl@marksset}

\@ifundefined{chapter}%
    {\setmarks{section}{subsection}}%
    {\setmarks{chapter}{section}}

\newcommand\usepage{\protect\thepage}

\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@}

% 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
