%%
%% This is file `pageslts.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% pageslts.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Project: pageslts
%% Version: 2014/01/19 v1.2c
%% 
%% Copyright (C) 2010 - 2014 by
%%     H.-Martin M"unch <Martin dot Muench at Uni-Bonn dot de>
%% Portions of code copyrighted by other people as marked.
%% 
%% IMPORTANT NOTICE:
%% On the first page backslash pagenumbering should be called
%% (with the appropriate argument out of e.g.
%%  * roman    - Lowercase Roman numerals: i, ii, iii, iv,...
%%  * Roman    - Uppercase Roman numerals: I, II, III, IV,...
%%  * arabic   - Arabic numerals: 1, 2, 3, 4,...
%%  * alph     - Lowercase letters: a, b, c, d,...
%%  * Alph     - Uppercase letters: A, B, C, D,...
%%  * fnsymbol - Footnote symbols: *, ^^e2^^80^^a0 (ddagger), ^^e2^^80^^a1 (dddagger),
%%                                  ^^c2^^a7 (mathsection),...
%% ). The command backslash pagenumbering is renewed, but no change
%% should be visible to the output text, because only labels are added.
%% backslash OrigPagenumbering can be used to get the original meaning
%% of backslash pagenumbering, but in that case this package does not
%% see the change in the page numbering scheme and will make mistakes.
%% The package takes options.
%% 
%% The usual disclaimer applies:
%% 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 package first started as a revision of the lastpage
%% package of Jeffrey P. Goldberg (jeffrey+news at goldmark dot org),
%% but it became obvious that a replacement was needed.
%% 
%% This work consists of the main source file pageslts.dtx
%% and the derived files
%%    pageslts.sty, pageslts.pdf, pageslts.ins, pageslts.drv,
%%    pageslts-example.tex.
%% 
%% In memoriam Tommy Muench + 2014/01/02.
%% 
\NeedsTeXFormat{LaTeX2e}[2011/06/27]
\ProvidesPackage{pageslts}[2014/01/19 v1.2c
            Refers to special pages' numbers/names (HMM)]

%% Allows for things like\\
%% |Page \thepage\ (\theCurrentPage; local: \theCurrentPageLocal) of %
%%  \lastpageref{pagesLTS.roman}(\lastpageref{pagesLTS.roman.local}) + %
%%  \lastpageref{pagesLTS.Roman}(\lastpageref{pagesLTS.Roman.local}) + %
%%  \lastpageref{pagesLTS.arabic}(\lastpageref{pagesLTS.arabic.local}) + %
%%  \lastpageref{pagesLTS.fnsymbol}(\lastpageref{pagesLTS.fnsymbol.local}) + %
%%  \lastpageref{pagesLTS.alph}(\lastpageref{pagesLTS.alph.local}) + %
%%  \lastpageref{pagesLTS.Alph}(\lastpageref{pagesLTS.Alph.local}) = %
%%  \lastpageref{LastPages} pages.|\\
%% to get\\
%% 'Page d (57; local: 4) of ii(2) + XX(20) + *(1) + 30(30) + e(5) + C(3) = 61 pages.'.

\@ifpackageloaded{pagesLTS}{%
  \def\pagesLTS@doubleload{p@gesLTSlo@ded}
  \ifx\pagesLTS@loaded\pagesLTS@doubleload%
    \PackageError{pageslts}{Requested pagesLTS instead of pageslts}{%
      You have requested package `pagesLTS'. This package is now named `pageslts'.\MessageBreak%
      Requesting `pagesLTS' as well as `pageslts' leads to loading the same package\MessageBreak%
      twice (and results in errors). Loading of `pageslts' will therefore be\MessageBreak%
      abandoned now. To fix this problem, please look in the \jobname.log file for\MessageBreak%
      LaTeX Warning: You have requested package `pagesLTS',\MessageBreak%
      \space \space \space \space \space \space \space \space \space \space \space \space \space%
      \space \space but the package provides `pageslts'.\MessageBreak%
      and change the package call in the calling style/document.\MessageBreak%
      If a style file made the mistake, please inform its maintainer and/or the\MessageBreak%
      maintainer of the pageslts package about it (after making sure you have\MessageBreak%
      the recent version of that style file).\MessageBreak%
      Thank you and sorry for the inconvenience!\MessageBreak%
     }
    \endinput
  \fi
  }{% \relax
   }

\gdef\pagesLTS@loaded{p@gesLTSlo@ded}

\RequirePackage{atveryend}[2011/06/30]%      v1.8
\RequirePackage{everyshi}[2001/05/15]%       v3.00
\RequirePackage{letltxmacro}[2010/09/02]%    v1.4
\RequirePackage{ltxcmds}[2011/11/09]%        v1.22
\RequirePackage{kvoptions}[2011/06/30]%      v3.11
\RequirePackage{undolabl}[2012/01/01]%       v1.0k
\RequirePackage{rerunfilecheck}[2011/04/15]% v1.7
%% pageslts package uses Prelim@EveryShipout code from the
%% prelim2e package [2009/05/29 v1.3] by Martin Schr\"{o}der, thanks!

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

%% The recent version of the endfloat package is v2.5d as of 2011/12/25.
%% The pageslts package is not fully compatible with version 2.0
%% (and earlier) of the endfloat package, because those versions
%% redefined the \enddocument command.

\SetupKeyvalOptions{family = pagesLTS,prefix = pagesLTS@}
\DeclareBoolOption[true]{pagecontinue}% \pagesLTS@pagecontinue
\DeclareStringOption[ab]{alphMult}
\DeclareStringOption[AB]{AlphMulti}
\DeclareBoolOption[true]{romanMult}
\DeclareBoolOption[true]{RomanMulti}
\DeclareBoolOption[true]{fnsymbolmult}

\ProcessKeyvalOptions*

\def\pagesLTS@zero{0}
\def\pagesLTS@one{1}
\def\pagesLTS@two{2}
\def\pagesLTS@three{3}

\ifpagesLTS@pagecontinue%
  \PackageInfo{pageslts}{Option pagecontinue enabled\MessageBreak%
    (maybe by default):\MessageBreak%
    The pageslts package will continue the page numbering,\MessageBreak%
    when the same page numbering scheme is used twice.\MessageBreak%
    If you do not want this, call pageslts with option\MessageBreak%
    pagecontinue=false (or use \string\setcounter{page}=1).\MessageBreak%
  }
\else
  \PackageWarningNoLine{pageslts}{Option pagecontinue is false:\MessageBreak%
    The pageslts package was used, but the option\MessageBreak%
    pagecontinue was set to false.\MessageBreak%
    If you want the page numbers to be continued,\MessageBreak%
    when the same page numbering scheme is used twice,\MessageBreak%
    please call pageslts with option pagecontinue=true,\MessageBreak%
    otherwise the page number is reset to one each time\MessageBreak%
    the page numbering scheme is changed.\MessageBreak%
    For details please see the documentation%
  }
\fi

\def\pagesLTS@ab{ab}
\def\pagesLTS@bb{bb}
\def\pagesLTS@ABi{AB}
\def\pagesLTS@BBi{BB}

\ifx\pagesLTS@alphMult\pagesLTS@ab%
\else
  \ifx\pagesLTS@alphMult\pagesLTS@bb%
  \else
    \ifx\pagesLTS@alphMult\pagesLTS@zero%
      \PackageWarningNoLine{pageslts}{%
        Option alphMult=0 found:\MessageBreak%
        The pageslts package was used, but the option\MessageBreak%
        alphMult was set to 0 (zero).\MessageBreak%
        If you want the page numbers to be extended\MessageBreak%
        after z, you have to organize this yourself now.\MessageBreak%
        For automatic continuation, please use the\MessageBreak%
        alphalph package and call pageslts\MessageBreak%
        with option alphMult=ab (for aa, ab, ac, ad,...) or\MessageBreak%
        with option alphMult=bb (for aa, bb, cc, dd,...).\MessageBreak%
        For details please see the documentation!%
       }
    \else
      \PackageError{pageslts}{Unknown option value}{%
        The pageslts package was used with option\MessageBreak%
        alphMult= \pagesLTS@alphMult . Only values\MessageBreak%
        ab, bb, and 0 (zero) are valid.\MessageBreak%
        The default ab is set.\MessageBreak%
        For details please see the documentation!%
       }
      \setkeys{pagesLTS}{alphMult=ab}
    \fi
  \fi
\fi

\ifx\pagesLTS@AlphMulti\pagesLTS@ABi%
\else
  \ifx\pagesLTS@AlphMulti\pagesLTS@BBi%
  \else
    \ifx\pagesLTS@AlphMulti\pagesLTS@zero%
      \PackageWarningNoLine{pageslts}{%
        Option AlphMulti=0 found:\MessageBreak%
        The pageslts package was used, but the option\MessageBreak%
        AlphMulti was set to 0 (zero).\MessageBreak%
        If you want the page numbers to be extended\MessageBreak%
        after Z, you have to organize this yourself now.\MessageBreak%
        For automatic continuation, please use the\MessageBreak%
        alphalph package and call pageslts\MessageBreak%
        with option AlphMulti=AB (for AA, AB, AC, AD,...) or\MessageBreak%
        with option AlphMulti=BB (for AA, BB, CC, DD,...).\MessageBreak%
        For details please see the documentation!%
       }
    \else
      \PackageError{pageslts}{Unknown option value}{%
        The pageslts package was used with option\MessageBreak%
        AlphMulti= \pagesLTS@AlphMulti . Only values\MessageBreak%
        AB, BB, and 0 (zero) are valid.\MessageBreak%
        The default AB is set.\MessageBreak%
        For details please see the documentation!%
       }
      \setkeys{pagesLTS}{AlphMulti=AB}
    \fi
  \fi
\fi

\ifx\pagesLTS@alphMult\pagesLTS@zero% \relax
\else \RequirePackage{alphalph}[2011/05/13]% v2.4
\fi

\ifx\pagesLTS@AlphMulti\pagesLTS@zero% \relax
\else \RequirePackage{alphalph}[2011/05/13]% v2.4
\fi

\ifpagesLTS@fnsymbolmult%
\RequirePackage{alphalph}[2011/05/13]% v2.4
\fi

\ifpagesLTS@romanMult%
  \PackageInfo{pageslts}{Option romanMult enabled\MessageBreak%
    (maybe by default):\MessageBreak%
    The pageslts package will extend the page numbering\MessageBreak%
    of the roman scheme below i with\MessageBreak%
     0, -i, -ii, -iii, -iv,...\MessageBreak%
    If you do not want this, call pageslts with option\MessageBreak%
    romanMult=false.\MessageBreak%
  }
\else
  \PackageWarningNoLine{pageslts}{Option romanMult is set to false:\MessageBreak%
  The pageslts package was used, but the option\MessageBreak%
  romanMult was set to false.\MessageBreak%
  If you want the page numbering of the roman scheme\MessageBreak%
  to be extended below i,\MessageBreak%
  please call pageslts with option romanMult=true,\MessageBreak%
  otherwise zero and negative page numbers of the\MessageBreak%
  roman scheme will need to be defined otherwise.\MessageBreak%
  For details please see the documentation!\MessageBreak%
  }
\fi

\ifpagesLTS@RomanMulti%
  \PackageInfo{pageslts}{Option RomanMulti enabled\MessageBreak%
    (maybe by default):\MessageBreak%
    The pageslts package will extend the page numbering\MessageBreak%
    of the Roman scheme below I with\MessageBreak%
    0, -I, -II, -III, -IV,...\MessageBreak%
    If you do not want this, call pageslts with option\MessageBreak%
    RomanMulti=false.\MessageBreak%
   }
\else
  \PackageWarningNoLine{pageslts}{Option RomanMulti is set to false:\MessageBreak%
    The pageslts package was used, but the option\MessageBreak%
    RomanMulti was set to false.\MessageBreak%
    If you want the page numbering of the Roman scheme\MessageBreak%
    to be extended below i,\MessageBreak%
    please call pageslts with option RomanMulti=true,\MessageBreak%
    otherwise zero and negative page numbers of the\MessageBreak%
    Roman scheme will need to be defined otherwise.\MessageBreak%
    For details please see the documentation!\MessageBreak%
   }
\fi

\ifpagesLTS@fnsymbolmult%
  \PackageInfo{pageslts}{Option fnsymbolmult enabled\MessageBreak%
    (maybe by default):\MessageBreak%
    The pageslts package will extend the page numbering\MessageBreak%
    of the footnotesymbol scheme using the alphalph\MessageBreak%
    package.\MessageBreak%
    If you do not want this, call pageslts with option\MessageBreak%
    fnsymbolmult=false.\MessageBreak%
   }
\else
  \PackageWarningNoLine{pageslts}{%
    Option fnsymbolmult is set to false:\MessageBreak%
    The pageslts package was used, but the option\MessageBreak%
    fnsymbolmult was set to false.\MessageBreak%
    If you want the page numbering of the footnotesymbol\MessageBreak%
    scheme to be extended using the alphalph package,\MessageBreak%
    please call pageslts with option fnsymbolmult=true,\MessageBreak%
    otherwise page numbers of the footnotesymbol scheme\MessageBreak%
    greater than nine will need to be defined otherwise.\MessageBreak%
    For details please see the documentation!\MessageBreak%
   }
\fi

\def\pagesLTS@pnc{0}
\def\pagesLTS@called{0}
\def\pagesLTS@fns{fnsymbol}
\def\pagesLTS@alph{alph}
\def\pagesLTS@Alph{Alph}
\def\pagesLTS@rerun{0}
\def\pagesLTS@eso{0}
\def\pagesLTS@esov{0}
\def\lastpageref{\lastpagereftxt}
\def\pagesLTS@undolable{none}
\def\pncmissing{0}
\def\pagesLTS@SK{0}
\def\pagesLTS@messageNPN{%
 The pageslts package was used, but\MessageBreak%
 \string\pagenumbering \MessageBreak%
 was not called at the beginning of the document\MessageBreak%
 (maybe earlier or later).\MessageBreak%
 Please use \string\pagenumbering \MessageBreak%
 (with an argument like arabic, roman, Roman,\MessageBreak%
 fnsymbol, alph, or Alph) at the beginning\MessageBreak%
 of your document! Otherwise your document\MessageBreak%
 will probably compile, but the pageslts\MessageBreak%
 package might not be able to get all labels\MessageBreak%
 for the references to the respective pages\MessageBreak%
 right.\MessageBreak%
 }

\if@filesw% \relax
\else
  \PackageError{pageslts}{No auxiliary file allowed.}{%
    The pageslts package was not allowed to write to an .aux file.\MessageBreak
    This package does not work without access to an .aux file.\MessageBreak
    Press Ctrl+Z to exit.\MessageBreak
   }
\fi

\LetLtxMacro{\OrigPagenumbering}{\pagenumbering}

\newcounter{CurrentPage}
\setcounter{CurrentPage}{1}
\def\theCurrentPageLocal{\arabic{pagesLTS.current.local.\pagesLTS@pnc}}
\newcounter{pagesLTS.pagenr}
\newcounter{pagesLTS.current.local.0}
\setcounter{pagesLTS.current.local.0}{1}
\newcounter{pagesLTS.pnc.0}

\newcommand{\xroman}[1]{%
  \ifnum\value{#1}>0%
    \roman{#1}%
  \else%
    \ifnum\value{#1}<0%
      -\romannumeral\number-\arabic{#1}%
    \else%
      0%
    \fi%
  \fi%
  }

\newcommand{\XRoman}[1]{%
  \ifnum\value{#1}>0%
    \Roman{#1}%
  \else%
    \ifnum\value{#1}<0%
      -\expandafter\@slowromancap\romannumeral\number-\arabic{#1}@%
    \else%
      0%
    \fi%
  \fi%
  }

\newcommand{\XXRoman}[1]{\XRoman{#1}%
  \PackageError{pageslts}{Old command \string\XXRoman\space found}{Replaced by \string\XRoman.}%
  }

\newcommand{\pagesLTS@ifcounter}[1]{%
  \@ifundefined{c@#1}{\newcounter{#1}}{\relax}%
  }

\newcommand{\lastpages}[2]{%
  \pagesLTS@ifcounter{pagesLTS.#1.#2.local.cnt}%
  \arabic{pagesLTS.#1.#2.local.cnt}%
  }

\newcommand{\pagesLTS@writelabel}[1]{%
  \addtocounter{page}{+1}%
  \pagesLTS@putlabel{pagesLTS.#1}{\thepage}{1}
  \addtocounter{page}{-1}%
  \ifx\pagesLTS@pnc\pagesLTS@zero% \relax
  \else%
    \addtocounter{page}{+1}%
    \pagesLTS@putlabel{pagesLTS.#1.local}{\theCurrentPageLocal}{1}
    \addtocounter{page}{-1}%
  \fi%
  }


%% The following code is from Heiko Oberdiek [2010/04/18],              %%
%% expanding his alphalph package as of 2010/04/18, v2.3. (Thanks!)     %%
\newcommand*{\erroralphalph}[2]{%
  \ifnum\value{#2}>0%
    #1{\value{#2}}%
  \else%
    \ifnum\value{#2}<0%
      -#1{\expandafter\@gobble\the\value{#2}}%
    \else%
      0%
    \fi%
  \fi%
  }
%% End of code from Heiko Oberdiek                                      %%
%% Check and Error/Warning messages have been moved to \EveryShipout,   %%
%% because messages inside e.g. the \pageref command can cause trouble. %%


\newcommand{\expandPagenumbering}[1]{%
  \let\Origthepage\thepage%
  \def\pagesLTS@tmpC{arabic}%
  \ifx\pagesLTS@pnc\pagesLTS@tmpC% \relax
  \else%
    \def\pagesLTS@tmpC{roman}%
    \ifx\pagesLTS@pnc\pagesLTS@tmpC%
      \ifpagesLTS@romanMult%
        \renewcommand*{\thepage}{\xroman{page}}%
      \fi%
    \else%
      \def\pagesLTS@tmpC{Roman}%
      \ifx\pagesLTS@pnc\pagesLTS@tmpC%
        \ifpagesLTS@RomanMulti%
          \renewcommand*{\thepage}{\XRoman{page}}%
        \fi%
      \else%
        \ifx\pagesLTS@pnc\pagesLTS@alph%
          \ifx\pagesLTS@alphMult\pagesLTS@ab%
            \renewcommand*{\thepage}{\erroralphalph{\alphalph}{page}}%
          \else \ifx\pagesLTS@alphMult\pagesLTS@bb%
                  \renewcommand*{\thepage}{\erroralphalph{\alphMult}{page}}%
                \fi%
          \fi%
        \else%
          \ifx\pagesLTS@pnc\pagesLTS@Alph%
            \ifx\pagesLTS@AlphMulti\pagesLTS@ABi%
              \renewcommand*{\thepage}{\erroralphalph{\AlphAlph}{page}}%
            \else \ifx\pagesLTS@AlphMulti\pagesLTS@BBi%
                    \renewcommand*{\thepage}{\erroralphalph{\AlphMult}{page}}%
                  \fi%
            \fi%
          \else%
            \ifx\pagesLTS@pnc\pagesLTS@fns%
              \ifpagesLTS@fnsymbolmult%
                \renewcommand*{\thepage}{\erroralphalph{\fnsymbolmult}{page}}%
              \fi%
            \else%
              \PackageError{pageslts}{unknown page numbering scheme}{%
                The pageslts package encountered the unknown\MessageBreak%
                page numbering scheme\MessageBreak%
                `#1'. \MessageBreak%
                If this is no typing mistake, it might work\MessageBreak%
                - or it might not work.\MessageBreak%
                \@ehc%
                }
            \fi%
          \fi%
        \fi%
      \fi%
    \fi%
  \fi%
  \let\pagesLTS@tmpC\undefined%
  }

\renewcommand{\pagenumbering}[1]{%
  \edef\pagesLTS@tmpA{#1}%
  \ifx\pagesLTS@pnc\pagesLTS@fns%
    \pagesLTS@ifcounter{pagesLTS.fnsymbol.local}%
  \fi%
  \ifx\pagesLTS@tmpA\pagesLTS@fns%
    \pagesLTS@ifcounter{pagesLTS.fnsymbol.local}%
  \fi%
  \ifx\pagesLTS@pnc\pagesLTS@tmpA% \relax
  \else%
    \ifnum \value{CurrentPage}>1%
      \addtocounter{page}{-1}%
      \addtocounter{pagesLTS.current.local.\pagesLTS@pnc}{-1}%
      \newcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}%
      \setcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}{%
        \value{pagesLTS.current.local.\pagesLTS@pnc}}%
      \ifx\pagesLTS@pnc\pagesLTS@fns%
        \ifnum \value{pagesLTS.pnc.\pagesLTS@pnc}>1%
          \mathchardef\pagesLTS@tmpa=\arabic{pagesLTS.pnc.0}%
          \setcounter{pagesLTS.pnc.0}{\value{pagesLTS.pnc.\pagesLTS@pnc}}%
          \addtocounter{pagesLTS.pnc.0}{-1}%
          \addtocounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}{%
            -\value{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.0}.local.count}}%
          \setcounter{pagesLTS.pnc.0}{\pagesLTS@tmpa}%
        \fi%
      \else%
        \pagesLTS@writelabel{\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}}%
        \ifnum \value{pagesLTS.pnc.\pagesLTS@pnc}<2%
          \pagesLTS@writelabel{\pagesLTS@pnc}%
        \else%
          \mathchardef\pagesLTS@tmpa=\arabic{pagesLTS.pnc.0}%
          \setcounter{pagesLTS.pnc.0}{\value{pagesLTS.pnc.\pagesLTS@pnc}}%
          \addtocounter{pagesLTS.pnc.0}{-1}%
          \pagesLTS@ifcounter{pagesLTS.\pagesLTS@pnc.done}%
          \addtocounter{pagesLTS.\pagesLTS@pnc.done}{%
            \value{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.0}.local.count}}%
          \addtocounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}{%
            -\value{pagesLTS.\pagesLTS@pnc.done}}%
          \setcounter{pagesLTS.pnc.0}{\pagesLTS@tmpa}%
        \fi%
        \if@filesw%
          \immediate\write\@auxout{\string
            \pagesLTS@ifcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.cnt}}%
        \fi%
        \edef\pagesLTS@tmpB{\arabic{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}}%
        \if@filesw%
          \immediate\write\@auxout{\string
            \setcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.cnt}{\pagesLTS@tmpB}}%
        \fi%
      \fi%
      \pagesLTS@ifcounter{pagesLTS.double.\pagesLTS@pnc}%
      \setcounter{pagesLTS.double.\pagesLTS@pnc}{\value{page}}%
      \addtocounter{pagesLTS.current.local.\pagesLTS@pnc}{+1}%
      \addtocounter{page}{+1}%
      \xdef\pagesLTS@pnc{#1}%
      \OrigPagenumbering{#1}%
      \expandPagenumbering{#1}%
      \pagesLTS@ifcounter{pagesLTS.pnc.\pagesLTS@pnc}%
      \addtocounter{pagesLTS.pnc.\pagesLTS@pnc}{1}%
      \pagesLTS@ifcounter{pagesLTS.double.\pagesLTS@pnc}%
      \addtocounter{pagesLTS.double.\pagesLTS@pnc}{1}%
      \ifpagesLTS@pagecontinue%
        \setcounter{page}{\value{pagesLTS.double.\pagesLTS@pnc}}%
      \else%
        \setcounter{page}{1}%
      \fi%
      \pagesLTS@ifcounter{pagesLTS.current.local.\pagesLTS@pnc}%
      \ifnum \value{pagesLTS.double.\pagesLTS@pnc}=1%
        \setcounter{pagesLTS.current.local.\pagesLTS@pnc}{1}%
      \fi%
    \else%
      %% before the first page has shipped out
      \xdef\pagesLTS@pnc{#1}%
      \global\c@page \@ne\relax%
      \global\def\thepage{\csname \expandafter @\pagesLTS@pnc \endcsname \c@page}%
      \expandPagenumbering{#1}
      \pagesLTS@ifcounter{pagesLTS.pnc.\pagesLTS@pnc}%
      \setcounter{pagesLTS.pnc.\pagesLTS@pnc}{1}%
      \pagesLTS@ifcounter{pagesLTS.double.\pagesLTS@pnc}%
      \setcounter{pagesLTS.double.\pagesLTS@pnc}{1}%
      \pagesLTS@ifcounter{pagesLTS.current.local.\pagesLTS@pnc}%
      \setcounter{pagesLTS.current.local.\pagesLTS@pnc}{1}%
    \fi%
    \gdef\pagesLTS@called{1}%
  \fi%
  \let\pagesLTS@tmpA\undefined%
  \let\pagesLTS@tmpB\undefined%
  }

%% analogous to \HyPsd@pageref from the hyperref package by Heiko Oberdiek:
\def\lastpagereftxt#1{\pagesLTS@@pageref#1*\END}

\def\pagesLTS@@pageref#1*#2\END{%
  \ifx\\#2\\% no star
    \pagesLTS@@@pageref{#1}%
  \else% star
    \expandafter\pagesLTS@@@pagerefstar%
  \fi%
}

\def\pagesLTS@@@pageref#1{\lastpagereftext{#1}}
\def\pagesLTS@@@pagerefstar#1{\lastpagereftextstar{#1}}

\newcommand{\lastpagereftext}[1]{%
  \def\pagesLTS@tmpA{#1}%
  \def\pagesLTS@tmpB{pagesLTS.fnsymbol.local}%
  \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
    \pagesLTS@ifcounter{pagesLTS.fnsymbol.local}%
    \ltx@ifpackageloaded{hyperref}{%
      \href{\#pagesLTS.fnsymbol.local.\arabic{pagesLTS.fnsymbol.local}}%
        {\arabic{pagesLTS.fnsymbol.local}}%
     }{\arabic{pagesLTS.fnsymbol.local}}%
  \else%
    \pageref{#1}%
  \fi%
  \let\pagesLTS@tmpA\undefined%
  \let\pagesLTS@tmpB\undefined%
  }

\newcommand{\lastpagereftextstar}[1]{%
  \def\pagesLTS@tmpA{#1}%
  \def\pagesLTS@tmpB{pagesLTS.fnsymbol.local}%
  \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
     \pagesLTS@ifcounter{pagesLTS.fnsymbol.local}%
     \arabic{pagesLTS.fnsymbol.local}%
  \else%
    \ltx@ifpackageloaded{hyperref}{\pageref*{#1}}{\pageref{#1}}%
  \fi%
  \let\pagesLTS@tmpA\undefined%
  \let\pagesLTS@tmpB\undefined%
  }

\def\lastpagerefend#1{\pagesLTS@@pagerefend#1*\END}

\def\pagesLTS@@pagerefend#1*#2\END{%
  \ifx\\#2\\% no star
    \pagesLTS@@@pagerefend{#1}%
  \else% star
    \expandafter\pagesLTS@@@pagerefendstar%
  \fi%
}

\def\pagesLTS@@@pagerefend#1{\l@stpagerefend{#1}}
\def\pagesLTS@@@pagerefendstar#1{\l@stpagerefendstar{#1}}

\newcommand{\l@stpagerefend}[1]{%
  \def\pagesLTS@tmpA{#1}%
  \def\pagesLTS@tmpB{pagesLTS.fnsymbol.local}%
  \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
    \pagesLTS@ifcounter{pagesLTS.fnsymbol.local}%
    \ltx@ifpackageloaded{hyperref}{%
      \href{\#pagesLTS.fnsymbol.local.\arabic{pagesLTS.fnsymbol.local}}%
        {\arabic{pagesLTS.fnsymbol.local}}%
     }{\arabic{pagesLTS.fnsymbol.local}}%
  \else%
    \def\pagesLTS@tmpB{pagesLTS.fnsymbol}%
    \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
      \ltx@ifpackageloaded{hyperref}{%
        \href{\#pagesLTS.fnsymbol.local.\arabic{pagesLTS.fnsymbol.local}}%
          {\pagesLTS.lastpage}%
       }{\pageref{pagesLTS.fnsymbol}}%
    \else%
      \def\pagesLTS@tmpB{LastPage}%
      \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
        \ltx@ifpackageloaded{hyperref}{%
          \href{\#pagesLTS.fnsymbol.local.\pagesLTS@eso}%
            {\pagesLTS.lastpage}%
         }{\pageref{LastPage}}%
      \else%
        \def\pagesLTS@tmpB{VeryLastPage}%
        \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
          \ltx@ifpackageloaded{hyperref}{%
            \href{\#pagesLTS.fnsymbol.local.\arabic{pagesLTS.fnsymbol.local}}%
              {\pagesLTS.lastpage}%
           }{\pageref{VeryLastPage}}%
        \else%
          \def\pagesLTS@tmpB{LastPages}%
          \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
            \ltx@ifpackageloaded{hyperref}{%
              \href{\#pagesLTS.fnsymbol.local.\arabic{pagesLTS.fnsymbol.local}}%
                {\arabic{pagesLTS.pagenr}}%
             }{\pageref{LastPages}}%
          \else%
            \pageref{#1}%
          \fi%
        \fi%
      \fi%
    \fi%
  \fi%
  \let\pagesLTS@tmpA\undefined%
  \let\pagesLTS@tmpB\undefined%
  }

\newcommand{\l@stpagerefendstar}[1]{%
  \def\pagesLTS@tmpA{#1}%
  \def\pagesLTS@tmpB{pagesLTS.fnsymbol.local}%
  \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
    \pagesLTS@ifcounter{pagesLTS.fnsymbol.local}%
    \arabic{pagesLTS.fnsymbol.local}%
  \else%
    \def\pagesLTS@tmpB{pagesLTS.fnsymbol}%
    \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
      \ltx@ifpackageloaded{hyperref}{\pageref*{pagesLTS.fnsymbol}}{%
        \pageref{pagesLTS.fnsymbol}}%
    \else%
      \def\pagesLTS@tmpB{LastPage}%
      \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
        \ltx@ifpackageloaded{hyperref}{\pageref*{LastPage}}{\pageref{LastPage}}%
      \else%
        \def\pagesLTS@tmpB{VeryLastPage}%
        \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
          \ltx@ifpackageloaded{hyperref}{\pageref*{VeryLastPage}}{%
            \pageref{VeryLastPage}}%
        \else%
          \def\pagesLTS@tmpB{LastPages}%
          \ifx\pagesLTS@tmpA\pagesLTS@tmpB%
            \ltx@ifpackageloaded{hyperref}{\pageref*{LastPages}}{\pageref{LastPages}}%
          \else%
            \ltx@ifpackageloaded{hyperref}{\pageref*{#1}}{\pageref{#1}}%
            \fi%
          \fi%
        \fi%
      \fi%
    \fi%
  \fi%
  \let\pagesLTS@tmpA\undefined%
  \let\pagesLTS@tmpB\undefined%
  }

\newcommand\overrideLTSlabel[2]{%
  \@bsphack
    \ifnum \value{pagesLTS.pnc.\pagesLTS@pnc}>1%
      \edef\pagesLTStmpA{#1}%
      \edef\pagesLTStmpB{pagesLTS.\pagesLTS@pnc.local}%
      \ifx\pagesLTStmpA\pagesLTStmpB%
          \immediate\write\@auxout{\string\undonewlabel{#1}}%
          \@overriddenmessage s{#1}%
      \fi%
    \fi%
    \pagesLTS@putlabel{#1}{#2}{0}%
  \@esphack%
  }

\newcommand{\@pagesLTS@Prelim@EveryShipout}{%
%% The following code is from the prelim2e package             %%
%% [2009/05/29 v1.3] by Martin Schr\"{o}der (Thanks!):         %%
  \bgroup
    \dimen\z@=\wd\@cclv
    \dimen\@ne=\ht\@cclv
    \dimen\tw@=\dp\@cclv
    \dimen\thr@@=\dimen1
    \advance\dimen\thr@@ by \dimen\tw@
    \global\setbox\@cclv\vbox to \dimen\thr@@{%
      \hb@xt@\dimen\z@{%
        \box\@cclv%
        \hss%
        }%
      \vbox to \z@{%
        \hb@xt@\dimen\z@{%
          \let\protect\relax
%% Code not from prelim2e package:                             %%
          \ifx\pagesLTS@pnc\pagesLTS@fns%
            \pagesLTS@ifcounter{pagesLTS.fnsymbol.cont}%
            \addtocounter{pagesLTS.fnsymbol.cont}{1}%
            \ltx@ifpackageloaded{hyperref}{%
              \phantomsection%
              \hypertarget{pagesLTS.fnsymbol.local.\arabic{pagesLTS.fnsymbol.cont}}{}%
              }{}%
            \ifnum \pagesLTS@esov=\pagesLTS@zero%
              \label{pagesLTS.fnsymbol}%
            \else%
              \overridelabel{pagesLTS.fnsymbol}%
            \fi%
          \else%
            \ltx@ifpackageloaded{hyperref}{\phantomsection}{}%
            \if@filesw%
              \overridelabel{pagesLTS.\pagesLTS@pnc}%
              \addtocounter{page}{+1}%
                \overrideLTSlabel{pagesLTS.\pagesLTS@pnc.local}{\theCurrentPageLocal}%
              \addtocounter{page}{-1}%
            \fi%
          \fi%
%% Code from prelim2e package again:                           %%
        }%
        \vss%
      }%
      \vss%
    }%
    \wd\@cclv=\dimen\z@
    \ht\@cclv=\dimen\@ne
    \dp\@cclv=\dimen\tw@
  \egroup
%% End of code from the prelim2e package.                      %%
  }

\EveryShipout{%
  \ifnum\value{page}>0% \relax
  \else%
    \ifnum\value{page}=0%
      \PackageWarning{pageslts}{%
        Counter `page' is zero!\MessageBreak%
        If the page numbering scheme is not arabic\MessageBreak%
        and further not extended\MessageBreak%
        (see Page counter overflow in the pageslts\MessageBreak%
        documentation), without other measures\MessageBreak%
        this will lead to a counter overflow.\MessageBreak%
      }%
    \else%
      \ifnum\value{page}<0%
        \PackageWarning{pageslts}{%
          Counter `page' is negative: `\the\value{page}'!\MessageBreak%
          If the page numbering scheme is not arabic\MessageBreak%
          and further not extended\MessageBreak%
          (see Page counter overflow in the pageslts\MessageBreak%
          documentation), without other measures\MessageBreak%
          this will lead to a counter overflow.\MessageBreak%
        }%
      \else%
        \PackageError{pageslts}{%
          Counter `page' does not have a recognized value:\MessageBreak%
          `\the\value{page}'\MessageBreak%
          \@ehd \MessageBreak%
          }%
      \fi%
    \fi%
  \fi%
  \ifnum \value{CurrentPage}=1% This is the first shipout!
    \ifx\pagesLTS@called\pagesLTS@zero%
      \global\def\pncmissing{1}%
      \mathchardef\pagesLTS@tmpD=\arabic{page}%
%% Code from Andres L\"{o}h, Universiteit Utrecht (NL)         %%
      \def\extract#1{\expandafter\extract@ #1\END}%
      \def\extract@#1\csname @#2\endcsname#3\END{#2}%
      \edef\pagesLTS@tmpQ{\extract\thepage}%
%% End of code from Andres L\"{o}h                             %%
      \let\pagesLTS@tmpP\pagesLTS@tmpQ%
      \def\pagesLTS@pnc{0}%
      \pagenumbering{\pagesLTS@tmpP}%
      \setcounter{page}{\pagesLTS@tmpD}%
    \fi%
    \pagesLTS@writelabel{0}%
  \fi%
  \ifx\pagesLTS@pnc\pagesLTS@fns%
    \@pagesLTS@Prelim@EveryShipout%
    \gdef\pagesLTS@esov{\arabic{pagesLTS.fnsymbol.cont}}%
  \else%
    \ifnum \value{pagesLTS.pnc.\pagesLTS@pnc}>1%
      \@pagesLTS@Prelim@EveryShipout%
    \fi%
  \fi%
  \addtocounter{CurrentPage}{1}%
  \addtocounter{pagesLTS.current.local.\pagesLTS@pnc}{1}%
  }

\newcommand{\pagesLTS@putlabelhyper}[2]{%
  \ifHy@pageanchor \relax%
  \else%
    \PackageError{pagesLTS}{hyperref option pageanchor disabled}{%
      The \string\lastpageref{#1} link doesn't work\MessageBreak%
      using hyperref with disabled option `pageanchor'.\MessageBreak%
    }%
  \fi
%% The following code is from the hyperref package             %%
%% [2010/04/17 v6.80x; newer versions are available]           %%
%% by Heiko Oberdiek (Big Thanks!).                            %%
  \if@filesw
    \begingroup
      \let\@number\@firstofone
      \ifHy@pageanchor
        \ifHy@hypertexnames
          \ifHy@plainpages
            \def\Hy@temp{\arabic{page}}%
          \else
            \Hy@unicodefalse
%% Code not from hyperref package:                             %%
%% The following lines are modified from the hyperref package. %%
%% Without the modification, after the first shipout "PD1" is  %%
%% inserted each time |\pdfstringdef\Hy@temp{\thepage}| is     %%
%% executed (if |fnsymbol| is not used).                       %%
            \ifnum \value{CurrentPage}=1%
              \ifx\pagesLTS@pnc\pagesLTS@fns%
                \pdfstringdef\Hy@temp{\thepage}%
              \else%
                \def\Hy@temp{\thepage}%
              \fi%
            \else%
              \pdfstringdef\Hy@temp{\thepage}%
            \fi%
%% Code from hyperref package again:                           %%
          \fi
        \else
          \def\Hy@temp{\the\Hy@pagecounter}%
        \fi
      \fi
%% End of code from the hyperref package.                      %%
%% (The following four lines are modified                      %%
%%  from the hyperref package.)                                %%
      \immediate\write\@auxout{\string
        \newlabel{#1}{{}{#2}{}{\ifHy@pageanchor page.\Hy@temp\fi}{}}}%
    \endgroup%
  \fi%
  }

\newcommand{\pagesLTS@putlabel}[3]{%
  \addtocounter{page}{-1}%
  \ifx\pagesLTS@SK\pagesLTS@one%
    \message{^^J}%
    \message{Package pageslts Warning: Package showkeys without option final loaded,}%
    \ifnum\value{pagesLTS.pagenr}<1%
      \message{(pageslts)                but label #1 on page \thepage\space(about \theCurrentPage)}%
    \else%
      \message{(pageslts)                but label #1 on page \thepage\space(about \theCurrentPage\space of \arabic{pagesLTS.pagenr})}%
    \fi%
    \message{(pageslts)                cannot be shown, because pageslts does not use \string\label,}%
    \message{(pageslts)                but writes directly to the \jobname.aux file. ^^J}%
  \fi%
  \ltx@ifpackageloaded{hyperref}{\pagesLTS@putlabelhyper{#1}{#2}}{% else
    \if@filesw%
      \ifnum \value{pagesLTS.pnc.\pagesLTS@pnc}<2%
        \ltx@ifpackageloaded{nameref}{%
          \immediate\write\@auxout{\string\newlabel{#1}{{}{#2}{}{}{}}}}{% else
          \immediate\write\@auxout{\string\newlabel{#1}{{}{#2}}}}%
      \else%
        \edef\pagesLTStmpA{#1}%
        \edef\pagesLTStmpB{pagesLTS.\pagesLTS@pnc.local}%
        \ifx\pagesLTStmpA\pagesLTStmpB%
          \edef\pagesLTStmpA{#3}%
          \ifx\pagesLTStmpA\pagesLTS@one%
            \immediate\write\@auxout{\string\undonewlabel{#1}}%
          \fi%
        \fi%
        \ltx@ifpackageloaded{nameref}{%
          \immediate\write\@auxout{\string\newlabel{#1}{{}{#2}{}{}{}}}}{% else
          \immediate\write\@auxout{\string\newlabel{#1}{{}{#2}}}}%
      \fi%
    \fi%
  }%
  \addtocounter{page}{+1}%
  }

\newcommand{\pagesLTS@putlabels}{%
  \addtocounter{page}{-1}%
  \addtocounter{CurrentPage}{-1}%
  \addtocounter{pagesLTS.current.local.\pagesLTS@pnc}{-1}%
  \ifx\pagesLTS@pnc\pagesLTS@zero%
   \PackageWarning{pagesLTS}{No page numbering scheme found:\MessageBreak%
     \pagesLTS@messageNPN }%
  \else%
    \pagesLTS@writelabel{\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}}%
    \ifnum \value{pagesLTS.pnc.\pagesLTS@pnc}<2%
      \ifx\pagesLTS@pnc\pagesLTS@fns% \relax
      \else%
        \pagesLTS@writelabel{\pagesLTS@pnc}%
      \fi%
    \fi%
  \fi%
  \addtocounter{page}{+1}%
  \pagesLTS@putlabel{LastPages}{\theCurrentPage}{1}%
  \addtocounter{CurrentPage}{+1}%
  \addtocounter{pagesLTS.current.local.\pagesLTS@pnc}{1}%
  }

\AtBeginDocument{%
  \if@filesw%
    \immediate\write\@auxout{\string\gdef\string\pagesLTS@loaded{p@gesLTSnotlo@ded}}%
  \fi%
  \@ifpackageloaded{endfloat}%
   {\@ifpackagelater{endfloat}{1992/03/31}% April 1992 v2.0
    {\@ifpackagelater{endfloat}{2011/12/24}{% 2011/12/25 v2.5d
       \relax}%
     {\PackageWarningNoLine{pageslts}{Old endfloat package detected:\MessageBreak%
        There is a newer version of the endfloat package available.\MessageBreak%
        Please consider updating your version.\MessageBreak%
        The pageslts package might be incompatible with\MessageBreak%
        your current endfloat package.\MessageBreak%
      }%
     }%
     {\PackageError{pageslts}{Incompatible, very old endfloat package detected.}%
      {The very old version 2.0 (and earlier) of the\MessageBreak%
       endfloat package actually redefined the \string\enddocument ,\MessageBreak%
       and so interfered drastically with the LaTeX2e commands\MessageBreak%
       which make use of \string\AtEndDocument .\MessageBreak%
       Newer versions of the endfloat package exists\MessageBreak%
       (at least: v2.5d as of 2011/12/25)\MessageBreak%
       in modern documentation form,\MessageBreak%
       which should be available from CTAN.\MessageBreak%
       Please update your endfloat package\MessageBreak%
       for use with the pageslts package.\MessageBreak %
      }%
     }%
    }%
   }{}%
  \@ifpackageloaded{lastpage}%
   {\@ifpackagelater{lastpage}{2010/07/28}% 2010/07/29 v1.2a
      {\@ifpackagelater{lastpage}{2013/01/27}% 2013/01/28 v1.2l
         {\PackageWarning{pageslts}{lastpage package detected.\MessageBreak%
           With pageslts package in use, lastpage has no function.\MessageBreak%
           Just remove the lastpage package from your document.\MessageBreak%
           }%
         }{%
         \PackageWarning{pageslts}{Old lastpage package detected.\MessageBreak%
           With pageslts package in use, lastpage has no function.\MessageBreak%
           Just remove the lastpage package from your document.\MessageBreak%
           At least update it!\MessageBreak%
           }%
         }%
      }{%
       \PackageWarning{pageslts}{Incompatible package lastpage detected:\MessageBreak%
         Package pageslts was loaded, but also an old\MessageBreak%
         version of the lastpage package.\MessageBreak%
         pageslts has all functionality of the lastpage\MessageBreak%
         package (and more), so just remove the lastpage\MessageBreak%
         package from your document.\MessageBreak%
         (At least update it!)\MessageBreak%
         pageslts will now ''kill'' the lastpage@putlabel\MessageBreak%
         command of the lastpage package.\MessageBreak%
       }%
       \gdef\lastpage@putlabel{\relax}%
      }%
   }{}%
  \ltx@ifpackageloaded{alphalph}%
    {\newalphalph{\AlphMult}[mult]{\@Alph}{26}%
     \newalphalph{\alphMult}[mult]{\@alph}{26}%
     \newalphalph{\fnsymbolmult}[mult]{\@fnsymbol}{5}%
    }{}%
  \ltx@ifpackageloaded{hyperref}{%
     \@ifpackageloaded{pdfpages}%
       {\PackageWarningNoLine{pageslts}{Package pdfpages detected.\MessageBreak%
          Using hyperref with pdfpages can cause problems. See\MessageBreak%
          ftp://ftp.ctan.org/tex-archive/\MessageBreak%
          macros/latex/contrib/pax/\MessageBreak%
          for project pax (PDFAnnotExtractor)%
          }%
       }{% \relax
        }%
     \@ifpackageloaded{undolabl}%
       {\@ifpackagelater{undolabl}{2010/07/14}% 2010/07/15 v1.0d
          {\@ifpackagelater{undolabl}{2011/12/31}% 2012/01/01 v1.0k
             {% recent version as of the time of last revision of this package: OK
             }{% old, but not obsolete version
               \PackageWarningNoLine{pageslts}{Old version of undolabl package used.\MessageBreak%
                 See ftp://ftp.ctan.org/tex-archive/macros/latex/contrib/undolabl/ \MessageBreak%
                 for a new version.\MessageBreak%
                }%
             }
          }{\PackageError{pageslts}{Incompatible, obsolete version of undolabl package used.}{%
              See ftp://ftp.ctan.org/tex-archive/macros/latex/contrib/undolabl/ \MessageBreak%
              for a new version.\MessageBreak%
              Type  X <return>  to quit.\MessageBreak%
             }%
          }
       }{\PackageError{pageslts}{Package undolabl missing}{%
           Package undolabl not found.\MessageBreak%
           The pageslts package needs the undolabl package.\MessageBreak%
           See e.g.\MessageBreak%
           ftp://ftp.ctan.org/tex-archive/macros/latex/contrib/undolabl/ \MessageBreak%
           Type  X <return>  to quit.\MessageBreak%
           }%
       }
     \@ifpackagelater{hyperref}{2012/11/05}{% 2012/11/06 v6.83m
       \relax}{%
       \PackageWarningNoLine{pageslts}{Old hyperref package detected:\MessageBreak%
         There is a newer version of the\MessageBreak%
         hyperref package available.\MessageBreak%
         Please consider updating your version.\MessageBreak%
       }%
     }%
     %% pageslts supports the use of the package hyperref by
     %% Heiko Oberdiek (hyperref version 2012/11/06 v6.83m).
     %% pageslts may work with earlier versions of this packages,
     %% but this was not tested.
    }{%
      \PackageWarning{pageslts}{Package hyperref NOT detected.\MessageBreak%
        pageslts would support hyperref. The page references\MessageBreak%
        will NOT be hyperlinked!\MessageBreak }%
     }%
  \ltx@ifpackageloaded{showkeys}{%
    \@ifpackagewith{showkeys}{final}{}{\gdef\pagesLTS@SK{1}}}{}%
  }

\AtEndDocument{%
  \ifx\pncmissing\pagesLTS@one%
    \PackageError{pageslts}{pagenumbering scheme missing}{\pagesLTS@messageNPN }%
  \fi%
  \message{^^J%
    AED: pageslts setting LastPage ^^J}%
  \clearpage%
  \ifx\pagesLTS@pnc\pagesLTS@fns%
    \def\pagesLTS@tmpA{\arabic{pagesLTS.fnsymbol.local}}%
    \ifnum \pagesLTS@eso=\pagesLTS@tmpA%
      \gdef\pagesLTS@rerun{0}%
    \else%
      \gdef\pagesLTS@rerun{1}%
    \fi%
    \if@filesw%
      \immediate\write\@auxout{\string
        \gdef\string\pagesLTS@eso{\pagesLTS@tmpA}}%
    \fi%
  \fi%
  \pagesLTS@putlabel{LastPage}{\thepage}{1}%
  \let\pagesLTS@tmpA\undefined%
  }

\AfterLastShipout{%
  \if@filesw%
    \ifx\pagesLTS@esov\pagesLTS@zero%
    \else%
      \immediate\write\@auxout{\string
        \pagesLTS@ifcounter{pagesLTS.fnsymbol.local}}%
      \immediate\write\@auxout{\string
        \setcounter{pagesLTS.fnsymbol.local}{\pagesLTS@esov}}%
    \fi%
    \ltx@ifpackageloaded{hyperref}{%
      \ifx\pagesLTS@pnc\pagesLTS@fns%
          \immediate\write\@auxout{\string
            \gdef\string\lastpageref{\string\lastpagerefend}}%
      \fi%
     }{}%
  \fi%
  \pagesLTS@ifcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}%
  \setcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}{%
    \value{pagesLTS.current.local.\pagesLTS@pnc}}%
  \addtocounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}{-1}
  \ifnum \value{pagesLTS.pnc.\pagesLTS@pnc}>1%
    \mathchardef\pagesLTS@tmpD=\arabic{pagesLTS.pnc.0}%
    \setcounter{pagesLTS.pnc.0}{\value{pagesLTS.pnc.\pagesLTS@pnc}}%
    \@tempcnta=\value{pagesLTS.pnc.0}\relax%
    \loop%
      \ifnum\@tempcnta>1\relax%
        \addtocounter{pagesLTS.pnc.0}{-1}%
        \addtocounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}{%
          -\value{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.0}.local.count}}%
        \@tempcnta=\value{pagesLTS.pnc.0}\relax%
    \repeat%
    \setcounter{pagesLTS.pnc.0}{\pagesLTS@tmpD}%
  \fi%
  \if@filesw%
    \immediate\write\@auxout{\string
      \pagesLTS@ifcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.cnt}}%
    \edef\pagesLTS@tmpA{\arabic{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.count}}%
    \immediate\write\@auxout{\string
      \setcounter{pagesLTS.\pagesLTS@pnc.\arabic{pagesLTS.pnc.\pagesLTS@pnc}.local.cnt}{\pagesLTS@tmpA}}%
    \let\pagesLTS@tmpA\undefined%
  \fi%
  \addtocounter{page}{-1}%
  \edef\pagesLTS@tmpA{\thepage}%
  \if@filesw%
    \immediate\write\@auxout{\string
      \gdef\string\pagesLTS.lastpage{\pagesLTS@tmpA}}%
  \fi%
  \addtocounter{page}{+1}%
  \addtocounter{CurrentPage}{-1}%
  \edef\pagesLTS@tmpB{\arabic{CurrentPage}}%
  \if@filesw%
    \immediate\write\@auxout{\string
      \setcounter{pagesLTS.pagenr}{\pagesLTS@tmpB}}%
  \fi%
  \addtocounter{CurrentPage}{+1}%
  \message{^^J%
    AED: pageslts setting VeryLastPage via AfterLastShipout ^^J}%
  \pagesLTS@putlabel{VeryLastPage}{\thepage}{1}%
  \message{^^J%
    AED: pageslts setting LastPages via AfterLastShipout ^^J}%
  \pagesLTS@putlabels%
  \typeout{^^J}%
  \ifodd\pagesLTS@tmpB%
    \@PackageInfoNoLine{pageslts}{Total number of pages is odd}%
  \else%
    \@PackageInfoNoLine{pageslts}{Total number of pages is even}%
  \fi%
  \typeout{^^J}%
  \let\pagesLTS@tmpA\undefined%
  \let\pagesLTS@tmpB\undefined%
  }

\AtEndAfterFileList{%
  \ifx\pagesLTS@rerun\pagesLTS@one%
    \PackageWarningNoLine{pageslts}{%
      Label(s) may have changed.\MessageBreak%
      Rerun to get cross-references right%
     }%
  \fi%
  }

\newcommand*{\ifund@fined}[1]{%
  % from http://de.comp.text.tex.narkive.com/uNza5NAA/ifundef-and-ifdef
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  }

\ifund@fined{frontmatter}{\relax}{%
  \ltx@GlobalPrependToMacro{\frontmatter}{\pagesLTS@prefrontmatter}%
  \ltx@GlobalAppendToMacro{\frontmatter}{\pagesLTS@postfrontmatter}%
  }

\newcommand{\pagesLTS@prefrontmatter}{%
  \xdef\pagesLTS@FMBpncn{\pagesLTS@pnc}%
  \xdef\pagesLTS@FMBpncp{\arabic{pagesLTS.current.local.\pagesLTS@pnc}}%
  }

\newcommand{\pagesLTS@postfrontmatter}{%
  \xdef\pagesLTS@FMEpncn{\pagesLTS@pnc}%
  \ifx\pagesLTS@FMBpncn\pagesLTS@FMEpncn%
  \else%
    \ifx\pagesLTS@FMBpncn\pagesLTS@zero%
    \else%
      \ifx\pagesLTS@FMBpncp\pagesLTS@one%
        \PackageError{pageslts}{\string\pagenumbering\space before \string\frontmatter}{%
          Do not use \string\pagenumbering{\pagesLTS@FMBpncn} before \string\frontmatter \MessageBreak%
          on the same page!\MessageBreak%
          \string\frontmatter\space (re)defines the page numbering scheme to \pagesLTS@FMEpncn , \MessageBreak%
          thus earlier use of \string\pagenumbering{\pagesLTS@FMBpncn} on the same page is useless anyway.}%
      \fi%
    \fi%
  \fi%
}

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