% $Id: etoolbox.sty,v 1.8 2009/08/06 16:50:04 lehman stable $

% Copyright (c) 2007-2009 Philipp Lehman.
%
% Permission is granted to copy, distribute and/or modify this
% software under the terms of the LaTeX Project Public License
% (LPPL), version 1.3.
%
% The LPPL maintenance status of this software is
% 'author-maintained'.
%
% This software is provided 'as is', without warranty of any kind,
% either expressed or implied, including, but not limited to, the
% implied warranties of merchantability and fitness for a
% particular purpose.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{etoolbox}[2009/08/06 v1.8 e-TeX tools for LaTeX]

\begingroup
\@ifundefined{eTeXversion}
  {\PackageError{etoolbox}
     {Not running under e-TeX}
     {This package requires e-TeX. Try compiling the document
      with\MessageBreak 'elatex' instead of 'latex'. When using
      pdfTeX, try 'pdfelatex'\MessageBreak instead of 'pdflatex'.
      This is a fatal error. I'm aborting now.}%
   \aftergroup\endinput}
  {}
\endgroup

\RequirePackage{etex}

\edef\etb@restore{%
  \catcode\number`\&=\the\catcode`\&\relax
  \catcode\number`\|=\the\catcode`\|\relax
  \catcode\number`\:=\the\catcode`\:\relax
  \catcode\number`\-=\the\catcode`\-\relax
  \catcode\number`\==\the\catcode`\=\relax
  \catcode\number`\<=\the\catcode`\<\relax
  \catcode\number`\>=\the\catcode`\>\relax
}
\catcode`\&=3
\catcode`\|=3
\@makeother\:
\@makeother\-
\@makeother\=
\@makeother\<
\@makeother\>

\AtEndOfPackage{%
  \etb@restore
  \undef\etb@restore}

\protected\def\etb@error{\PackageError{etoolbox}}
\protected\def\etb@info{\PackageInfo{etoolbox}}

% {<cstoken>}[<arguments>][<optarg default>]{<definition>}

\newcommand*{\newrobustcmd}{}
\protected\def\newrobustcmd{%
  \@ifstar
    {\let\l@ngrel@x\relax\etb@new@command}
    {\let\l@ngrel@x\long\etb@new@command}}

\def\etb@new@command#1{\@testopt{\etb@newcommand#1}0}
\def\etb@newcommand#1[#2]{%
  \@ifnextchar[%]
    {\etb@xargdef#1[#2]}
    {\ifx\l@ngrel@x\relax
       \let\l@ngrel@x\protected
     \else
       \protected\def\l@ngrel@x{\protected\long}%
     \fi
     \@argdef#1[#2]}}
\long\def\etb@xargdef#1[#2][#3]#4{%
  \@ifdefinable#1{%
    \expandafter\protected
    \expandafter\def
    \expandafter#1%
    \expandafter{%
      \expandafter\@testopt
      \csname\string#1\endcsname{#3}}%
    \expandafter\@yargdef\csname\string#1\endcsname\tw@{#2}{#4}}}

% {<cstoken>}[<arguments>][<optarg default>]{<definition>}

\newrobustcmd*{\renewrobustcmd}{%
  \@ifstar
    {\let\l@ngrel@x\protected\etb@renew@command}
    {\def\l@ngrel@x{\protected\long}\etb@renew@command}}

\def\etb@renew@command#1{%
  \begingroup
    \escapechar\m@ne
    \xdef\@gtempa{{\string#1}}%
  \endgroup
  \expandafter\@ifundefined\@gtempa
     {\etb@error{\string#1 undefined}\@ehc}
     {}%
  \let\@ifdefinable\@rc@ifdefinable
  \etb@new@command#1}

% {<cstoken>}[<arguments>][<optarg default>]{<definition>}

\newrobustcmd*{\providerobustcmd}{%
  \@ifstar
    {\let\l@ngrel@x\protected\etb@provide@command}
    {\def\l@ngrel@x{\protected\long}\etb@provide@command}}

\def\etb@provide@command#1{%
  \begingroup
    \escapechar\m@ne
    \xdef\@gtempa{{\string#1}}%
  \endgroup
  \expandafter\@ifundefined\@gtempa
    {\def\reserved@a{\etb@new@command#1}}
    {\def\reserved@a{\etb@renew@command\reserved@a}}%
  \reserved@a}%

% {<cstoken>}{<true>}{<false>}

\newcommand*{\ifdef}[1]{%
  \ifdefined#1%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% {<cstoken>}{<true>}{<false>}

\newcommand*{\ifundef}[1]{%
  \ifdefined#1%
    \ifx#1\relax
      \expandafter\expandafter
      \expandafter\@firstoftwo
    \else
      \expandafter\expandafter
      \expandafter\@secondoftwo
    \fi
  \else
    \expandafter\@firstoftwo
  \fi}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsdef}[1]{%
  \ifcsname#1\endcsname
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsundef}[1]{%
  \ifcsname#1\endcsname
    \expandafter\ifx\csname#1\endcsname\relax
      \expandafter\expandafter
      \expandafter\@firstoftwo
    \else
      \expandafter\expandafter
      \expandafter\@secondoftwo
    \fi
  \else
    \expandafter\@firstoftwo
  \fi}

% {<cstoken>}{<true}{<false>}

\newcommand*{\ifdefmacro}{}
\edef\ifdefmacro#1{%
  \noexpand\expandafter\noexpand\etb@ifdefmacro
  \noexpand\meaning#1\detokenize{macro}:&}
\edef\etb@ifdefmacro{%
  \def\noexpand\etb@ifdefmacro##1\detokenize{macro}:##2&}
\etb@ifdefmacro{\ifblank{#2}\@secondoftwo\@firstoftwo}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsmacro}[1]{%
  \ifcsdef{#1}
    {\expandafter\ifdefmacro\csname#1\endcsname}
    {\@secondoftwo}}

% {<cstoken>}{<true}{<false>}

\newcommand*{\ifdefprefix}[1]{%
  \ifdefmacro{#1}
    {\etb@ifdefprefix{#1}}
    {\@secondoftwo}}
\edef\etb@ifdefprefix#1{%
  \noexpand\expandafter\noexpand\etb@ifdefprefix@i
  \noexpand\meaning#1\detokenize{macro}:&}
\edef\etb@ifdefprefix@i{%
  \def\noexpand\etb@ifdefprefix@i##1\detokenize{macro}:##2&}
\etb@ifdefprefix@i{\ifblank{#1}\@secondoftwo\@firstoftwo}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsprefix}[1]{%
  \ifcsdef{#1}
    {\expandafter\ifdefprefix\csname#1\endcsname}
    {\@secondoftwo}}

% {<cstoken>}{<true}{<false>}

\newcommand*{\ifdefparam}{}
\edef\ifdefparam#1{%
  \noexpand\expandafter\noexpand\etb@ifdefparam
  \noexpand\meaning#1\detokenize{macro}:->&}
\edef\etb@ifdefparam{%
  \def\noexpand\etb@ifdefparam##1\detokenize{macro}:##2->##3&}
\etb@ifdefparam{\ifblank{#2}\@secondoftwo\@firstoftwo}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsparam}[1]{%
  \ifcsdef{#1}
    {\expandafter\ifdefparam\csname#1\endcsname}
    {\@secondoftwo}}

% {<cstoken>}{<true}{<false>}

\newcommand*{\ifdefprotected}{}
\edef\ifdefprotected#1{%
  \noexpand\expandafter\noexpand\etb@ifdefprotected
  \noexpand\meaning#1\string\protected&}
\edef\etb@ifdefprotected{%
  \def\noexpand\etb@ifdefprotected##1\string\protected##2&}
\etb@ifdefprotected{\ifblank{#2}\@secondoftwo\@firstoftwo}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsprotected}[1]{%
  \ifcsdef{#1}
    {\expandafter\ifdefprotected\csname#1\endcsname}
    {\@secondoftwo}}

% {<cstoken>}{<true}{<false>}

\newrobustcmd*{\ifdefltxprotect}[1]{%
  \begingroup
  \edef\etb@resrvda{%
    \noexpand\protect\expandafter\noexpand
    \csname\expandafter\@gobble\string#1 \endcsname}%
  \expandafter\endgroup\ifx#1\etb@resrvda
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% {<csname>}{<true>}{<false>}

\newrobustcmd*{\ifcsltxprotect}[1]{%
  \ifcsdef{#1}
    {\expandafter\ifdefltxprotect\csname#1\endcsname}
    {\@secondoftwo}}

% {<cstoken>}{<true>}{<false>}

\newcommand*{\ifdefempty}[1]{%
  \ifundef{#1}
    {\@secondoftwo}
    {\ifdefparam{#1}
       {\@secondoftwo}
       {\etb@ifdefempty#1}}}

\def\etb@ifdefempty#1{%
  \expandafter\expandafter
  \expandafter\ifblank
  \expandafter\expandafter
  \expandafter{%
  \expandafter\strip@prefix\meaning#1}}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsempty}[1]{%
  \ifcsundef{#1}
    {\@secondoftwo}
    {\expandafter\ifdefparam\csname#1\endcsname
       {\@secondoftwo}
       {\expandafter\etb@ifdefempty\csname#1\endcsname}}}

% {<cstoken>}{<true>}{<false>}

\newcommand*{\ifdefvoid}[1]{%
  \ifundef{#1}
    {\@firstoftwo}
    {\ifdefparam{#1}
       {\@secondoftwo}
       {\etb@ifdefempty#1}}}

% {<csname>}{<true>}{<false>}

\newcommand*{\ifcsvoid}[1]{%
  \ifcsundef{#1}%
    {\@firstoftwo}
    {\expandafter\ifdefparam\csname#1\endcsname
       {\@secondoftwo}
       {\expandafter\etb@ifdefempty\csname#1\endcsname}}}

% {<cstoken1>}{<cstoken2>}{<true>}{<false>}

\newcommand*{\ifdefequal}[2]{%
  \ifundef{#1}
    {\@secondoftwo}
    {\ifundef{#2}
       {\@secondoftwo}
       {\ifx#1#2%
          \expandafter\@firstoftwo
        \else
          \expandafter\@secondoftwo
        \fi}}}

% {<csname1>}{<csname2>}{<true>}{<false>}

\newcommand*{\ifcsequal}[2]{%
  \ifcsundef{#1}
    {\@secondoftwo}
    {\ifcsundef{#2}
       {\@secondoftwo}
       {\expandafter\ifx
        \csname#1\expandafter\endcsname
        \csname#2\endcsname
          \expandafter\@firstoftwo
        \else
          \expandafter\@secondoftwo
        \fi}}}

% {<cstoken>}{<string>}{<true>}{<false>}

\newrobustcmd{\ifdefstring}[2]{%
  \ifundef{#1}
    {\@secondoftwo}
    {\begingroup
     \edef\etb@tempa{\expandafter\strip@prefix\meaning#1}%
     \edef\etb@tempb{\detokenize{#2}}%
     \ifx\etb@tempa\etb@tempb
       \aftergroup\@firstoftwo
     \else
       \aftergroup\@secondoftwo
     \fi
     \endgroup}}

% {<csname>}{<string>}{<true>}{<false>}

\newrobustcmd{\ifcsstring}[2]{%
  \ifcsundef{#1}%
    {\@secondoftwo}
    {\expandafter\ifdefstring\csname#1\endcsname{#2}}}

% {<string1>}{<string2>}{<true>}{<false>}

\newrobustcmd{\ifstrequal}[2]{%
  \begingroup
  \edef\etb@tempa{\detokenize{#1}}%
  \edef\etb@tempb{\detokenize{#2}}%
  \ifx\etb@tempa\etb@tempb
    \aftergroup\@firstoftwo
  \else
    \aftergroup\@secondoftwo
  \fi
  \endgroup}

% {<string>}{<true>}{<false>}

\newcommand{\ifstrempty}[1]{%
  \expandafter\ifx\expandafter&\detokenize{#1}&%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% {<string>}{<true>}{<false>}

\newcommand{\ifblank}[1]{% from url.sty
  \etb@ifblank@i#1&&\@secondoftwo\@firstoftwo:}
\long\def\etb@ifblank@i#1#2&#3#4#5:{#4}

% {<numexpr>}{<comp>}{<numexpr>}{<true>}{<false>}

\newcommand*{\ifnumcomp}[3]{%
  \ifnum\numexpr#1\relax
      #2\numexpr#3\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% {<numexpr>}{<numexpr>}{<true>}{<false>}

\newcommand*{\ifnumequal}[1]{%
  \ifnumcomp{#1}=}

\newcommand*{\ifnumgreater}[1]{%
  \ifnumcomp{#1}>}

\newcommand*{\ifnumless}[1]{%
  \ifnumcomp{#1}<}

% {<dimexpr>}{<comp>}{<dimexpr>}{<true>}{<false>}

\newcommand*{\ifdimcomp}[3]{%
  \ifdim\dimexpr#1\relax
      #2\dimexpr#3\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% {<dimexpr>}{<dimexpr>}{<true>}{<false>}

\newcommand*{\ifdimequal}[1]{%
  \ifdimcomp{#1}=}

\newcommand*{\ifdimgreater}[1]{%
  \ifdimcomp{#1}>}

\newcommand*{\ifdimless}[1]{%
  \ifdimcomp{#1}<}

% {<string>}{<true>}{<false>}

% {<cstoken>}

\newcommand*{\expandonce}[1]{%
  \unexpanded\expandafter{#1}}

% {<csname>}

\newcommand*{\csexpandonce}[1]{%
  \expandafter\expandonce\csname #1\endcsname}

% {<code>}

\newcommand*{\protecting}{}
\def\protecting#{%
  \ifx\protect\@typeset@protect
    \etb@protecting\@firstofone
  \fi
  \ifx\protect\@unexpandable@protect
    \etb@protecting\etb@unexpandable
  \fi
  \ifx\protect\noexpand
    \etb@protecting\unexpanded
  \fi
  \ifx\protect\string
    \etb@protecting\detokenize
  \fi
  \relax\@firstofone}

\def\etb@protecting#1#2\relax\@firstofone{\fi#1}
\long\def\etb@unexpandable#1{\unexpanded{\protecting{#1}}}

% {<csname>}

\newrobustcmd*{\csdef}[1]{\expandafter\def\csname#1\endcsname}
\newrobustcmd*{\csedef}[1]{\expandafter\edef\csname#1\endcsname}
\newrobustcmd*{\csgdef}[1]{\expandafter\gdef\csname#1\endcsname}
\newrobustcmd*{\csxdef}[1]{\expandafter\xdef\csname#1\endcsname}
\newrobustcmd*{\protected@csedef}{\etb@protected\csedef}
\newrobustcmd*{\protected@csxdef}{\etb@protected\csxdef}

\def\etb@protected{%
  \let\@@protect\protect
  \let\protect\@unexpandable@protect
  \afterassignment\restore@protect}

% {<csname>}{<cstoken>}

\newrobustcmd*{\cslet}[2]{%
  \expandafter\let\csname#1\endcsname#2}

% {<cstoken>}{<csname>}

\newrobustcmd*{\letcs}[2]{%
  \expandafter\let\expandafter#1\csname#2\endcsname}

% {<csname>}{<csname>}

\newrobustcmd*{\csletcs}[2]{%
  \expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname}

% {<csname>}

\newcommand*{\csuse}[1]{%
  \ifcsname#1\endcsname
    \csname#1\expandafter\endcsname
  \fi}

% {<cstoken>}

\newrobustcmd*{\undef}[1]{\let#1\etb@undefined}

% {<csname>}

\newrobustcmd*{\csundef}[1]{\cslet{#1}\etb@undefined}

% {<cstoken>}{<code>}

\newrobustcmd{\appto}[2]{%
  \ifundef{#1}
    {\edef#1{\unexpanded{#2}}}
    {\edef#1{\expandonce#1\unexpanded{#2}}}}
\newrobustcmd{\eappto}[2]{%
  \ifundef{#1}
    {\edef#1{#2}}
    {\edef#1{\expandonce#1#2}}}
\newrobustcmd{\gappto}[2]{%
  \ifundef{#1}
    {\xdef#1{\unexpanded{#2}}}
    {\xdef#1{\expandonce#1\unexpanded{#2}}}}
\newrobustcmd{\xappto}[2]{%
  \ifundef{#1}
    {\xdef#1{#2}}
    {\xdef#1{\expandonce#1#2}}}

\newrobustcmd*{\protected@eappto}{\etb@protected\eappto}
\newrobustcmd*{\protected@xappto}{\etb@protected\xappto}

% {<cstoken>}{<code>}

\newrobustcmd{\preto}[2]{%
  \ifundef{#1}
    {\edef#1{\unexpanded{#2}}}
    {\edef#1{\unexpanded{#2}\expandonce#1}}}
\newrobustcmd{\epreto}[2]{%
  \ifundef{#1}
    {\edef#1{#2}}
    {\edef#1{#2\expandonce#1}}}
\newrobustcmd{\gpreto}[2]{%
  \ifundef{#1}
    {\xdef#1{\unexpanded{#2}}}
    {\xdef#1{\unexpanded{#2}\expandonce#1}}}
\newrobustcmd{\xpreto}[2]{%
  \ifundef{#1}
    {\xdef#1{#2}}
    {\xdef#1{#2\expandonce#1}}}

\newrobustcmd*{\protected@epreto}{\etb@protected\epreto}
\newrobustcmd*{\protected@xpreto}{\etb@protected\xpreto}

% {<csname>}{<code>}

\newrobustcmd*{\csappto}[1]{\expandafter\appto\csname#1\endcsname}
\newrobustcmd*{\cseappto}[1]{\expandafter\eappto\csname#1\endcsname}
\newrobustcmd*{\csgappto}[1]{\expandafter\gappto\csname#1\endcsname}
\newrobustcmd*{\csxappto}[1]{\expandafter\xappto\csname#1\endcsname}
\newrobustcmd*{\protected@cseappto}{\etb@protected\cseappto}
\newrobustcmd*{\protected@csxappto}{\etb@protected\csxappto}

% {<csname>}{<code>}

\newrobustcmd*{\cspreto}[1]{\expandafter\preto\csname#1\endcsname}
\newrobustcmd*{\csepreto}[1]{\expandafter\epreto\csname#1\endcsname}
\newrobustcmd*{\csgpreto}[1]{\expandafter\gpreto\csname#1\endcsname}
\newrobustcmd*{\csxpreto}[1]{\expandafter\xpreto\csname#1\endcsname}
\newrobustcmd*{\protected@csepreto}{\etb@protected\csepreto}
\newrobustcmd*{\protected@csxpreto}{\etb@protected\csxpreto}

% {<cstoken>}{<numexpr>}

\newrobustcmd*{\numdef}[2]{%
  \ifundef#1{\let#1\z@}{}%
  \edef#1{\the\numexpr#2}}
\newrobustcmd*{\numgdef}[2]{%
  \ifundef#1{\let#1\z@}{}%
  \xdef#1{\the\numexpr#2}}

% {<csname>}{<numexpr>}

\newrobustcmd*{\csnumdef}[1]{%
  \expandafter\numdef\csname#1\endcsname}
\newrobustcmd*{\csnumgdef}[1]{%
  \expandafter\numgdef\csname#1\endcsname}

% {<cstoken>}{<dimexpr>}

\newrobustcmd*{\dimdef}[2]{%
  \ifundef#1{\let#1\z@}{}%
  \edef#1{\the\dimexpr#2}}
\newrobustcmd*{\dimgdef}[2]{%
  \ifundef#1{\let#1\z@}{}%
  \xdef#1{\the\dimexpr#2}}

% {<csname>}{<dimexpr>}

\newrobustcmd*{\csdimdef}[1]{%
  \expandafter\dimdef\csname#1\endcsname}
\newrobustcmd*{\csdimgdef}[1]{%
  \expandafter\dimgdef\csname#1\endcsname}

% {<cstoken>}{<glueexpr>}

\newrobustcmd*{\gluedef}[2]{%
  \ifundef#1{\let#1\z@skip}{}%
  \edef#1{\the\glueexpr#2}}
\newrobustcmd*{\gluegdef}[2]{%
  \ifundef#1{\let#1\z@skip}{}%
  \xdef#1{\the\glueexpr#2}}

% {<csname>}{<glueexpr>}

\newrobustcmd*{\csgluedef}[1]{%
  \expandafter\gluedef\csname#1\endcsname}
\newrobustcmd*{\csgluegdef}[1]{%
  \expandafter\gluegdef\csname#1\endcsname}

% {<cstoken>}{<muexpr>}

\newrobustcmd*{\mudef}[2]{%
  \ifundef#1{\def#1{0mu}}{}%
  \edef#1{\the\muexpr#2}}
\newrobustcmd*{\mugdef}[2]{%
  \ifundef#1{\let#1\z@}{}%
  \xdef#1{\the\muexpr#2}}

% {<csname>}{<muexpr>}

\newrobustcmd*{\csmudef}[1]{%
  \expandafter\mudef\csname#1\endcsname}
\newrobustcmd*{\csmugdef}[1]{%
  \expandafter\mugdef\csname#1\endcsname}

% {<counter>}{<numexpr>}

\newrobustcmd*{\defcounter}[2]{%
  \ifcsundef{c@#1}
    {\etb@noglobal\@nocounterr{#1}}%
    {\csname c@#1\endcsname\numexpr#2\relax}}

% {<length>}{<glueexpr>}

\newrobustcmd*{\deflength}[2]{%
  \ifundef{#1}
    {\etb@noglobal\etb@err@nolen{#1}}%
    {#1\glueexpr#2\relax}}

\protected\def\etb@err@nolen#1{%
  \etb@error{Length \string#1 undefined}\@eha}

% {<name>}

\newrobustcmd*{\newbool}[1]{%
  \expandafter\@ifdefinable\csname if#1\endcsname{%
    \expandafter\newif\csname if#1\endcsname}}

% {<name>}

\newrobustcmd*{\providebool}[1]{%
  \ifcsundef{if#1}
    {\expandafter\newif\csname if#1\endcsname}
    {\begingroup
     \edef\@tempa{\expandafter\meaning\csname if#1\endcsname}%
     \ifx\@tempa\etb@isfalse
     \else
       \ifx\@tempa\etb@istrue
       \else
         \etb@error{\@backslashchar if#1 not a boolean}\@eha
       \fi
     \fi
     \endgroup}}

% {<name>}{<true>|<false>}

\newrobustcmd*{\setbool}[2]{%
  \ifcsundef{if#1}
    {\etb@noglobal\etb@err@nobool{#1}}
    {\ifcsundef{#1#2}
       {\etb@noglobal\etb@err@boolval{#2}}
       {\csname#1#2\endcsname}}}

% {<name>}

\newrobustcmd*{\booltrue}[1]{%
  \ifcsundef{if#1}
    {\etb@noglobal\etb@err@nobool{#1}}
    {\csname#1true\endcsname}}

% {<name>}

\newrobustcmd*{\boolfalse}[1]{%
  \ifcsundef{if#1}
    {\etb@noglobal\etb@err@nobool{#1}}
    {\csname#1false\endcsname}}

\edef\etb@istrue{\meaning\iftrue}
\edef\etb@isfalse{\meaning\iffalse}
\protected\def\etb@noglobal{\let\relax\relax}

% {<name>}{<true}{<false>}

\newcommand*{\ifbool}[1]{%
  \ifcsundef{if#1}
    {\etb@err@nobool{#1}\@gobbletwo}
    {\csname if#1\endcsname
       \expandafter\@firstoftwo
     \else
       \expandafter\@secondoftwo
     \fi}}

% {<name>}{<not true}{<not false>}

\newcommand*{\notbool}[1]{%
  \ifcsundef{if#1}
    {\etb@err@nobool{#1}\@gobbletwo}
    {\csname if#1\endcsname
       \expandafter\@secondoftwo
     \else
       \expandafter\@firstoftwo
     \fi}}

\protected\def\etb@err@nobool#1{%
  \etb@error{\@backslashchar if#1 undefined}\@eha}

\def\etb@err@boolval#1{%
  \etb@error
    {Invalid boolean value '#1'}
    {Valid boolean values are 'true' and 'false'.}}

% {<name>}

\newrobustcmd*{\newtoggle}[1]{%
  \ifcsdef{etb@tgl@#1}
    {\etb@error{Toggle '#1' already defined}\@eha}
    {\cslet{etb@tgl@#1}\@secondoftwo}}

% {<name>}

\newrobustcmd*{\providetoggle}[1]{%
  \ifcsdef{etb@tgl@#1}
    {}
    {\cslet{etb@tgl@#1}\@secondoftwo}}

% {<name>}{<true>|<false>}

\newrobustcmd*{\settoggle}[2]{%
  \ifcsdef{etb@tgl@#1}
    {\ifcsdef{etb@toggle#2}
       {\csletcs{etb@tgl@#1}{etb@toggle#2}}
       {\etb@noglobal\etb@err@boolval{#2}}}
    {\etb@noglobal\etb@err@notoggle{#1}}}

% {<name>}

\newrobustcmd*{\toggletrue}[1]{%
  \ifcsdef{etb@tgl@#1}
    {\cslet{etb@tgl@#1}\etb@toggletrue}
    {\etb@noglobal\etb@err@notoggle{#1}}}

% {<name>}

\newrobustcmd*{\togglefalse}[1]{%
  \ifcsdef{etb@tgl@#1}
    {\cslet{etb@tgl@#1}\etb@togglefalse}
    {\etb@noglobal\etb@err@notoggle{#1}}}

\let\etb@toggletrue\@firstoftwo
\let\etb@togglefalse\@secondoftwo

% {<name>}{<true}{<false>}

\newcommand*{\iftoggle}[1]{%
  \ifcsdef{etb@tgl@#1}
    {\csname etb@tgl@#1\endcsname}
    {\etb@err@notoggle{#1}\@gobbletwo}}

% {<name>}{<not true}{<not false>}

\newcommand*{\nottoggle}[1]{%
  \ifcsdef{etb@tgl@#1}
    {\csname etb@tgl@#1\endcsname\@secondoftwo\@firstoftwo}
    {\etb@err@notoggle{#1}\@gobbletwo}}

\protected\def\etb@err@notoggle#1{%
  \etb@error{Toggle '#1' undefined}\@eha}

% {<cstoken>}{<true}{<false>}

\protected\def\etb@ifscanable#1{%
  \begingroup
  \edef\etb@resrvda{%
    \def\noexpand\etb@resrvda####1\detokenize{macro}:####2->####3&{%
      ####1\def\string\etb@resrvda####2{####3}}%
    \edef\noexpand\etb@resrvda{\noexpand\etb@resrvda\meaning#1&}}%
  \etb@resrvda
  \makeatletter
  \scantokens\expandafter{\etb@resrvda}%
  \expandafter\endgroup\ifx#1\etb@resrvda
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% {<cstoken>}{<search>}{<true}{<false>}

\protected\def\etb@ifpattern#1#2{%
  \begingroup
  \edef\etb@resrvda{%
    \def\noexpand\etb@resrvda####1\detokenize{#2}####2&{%
      \endgroup\noexpand\noexpand\noexpand\ifblank{####2}}%
    \edef\noexpand\etb@resrvda{\noexpand\etb@resrvda
      \expandafter\strip@prefix\meaning#1\detokenize{#2}&}%
    \noexpand\etb@resrvda}
  \etb@resrvda\@secondoftwo\@firstoftwo}

% {<string>}{<true}{<false>}

\protected\def\etb@ifhashcheck#1{%
  \begingroup
  \edef\etb@resrvda{\detokenize{#1}}%
  \expandafter\endgroup
  \expandafter\etb@ifhashcheck@i\meaning\etb@resrvda&}

\edef\etb@ifhashcheck@i#1&{%
  \noexpand\expandafter
  \noexpand\etb@ifhashcheck@ii
  \noexpand\strip@prefix#1\string#\string#&}

\edef\etb@ifhashcheck@ii{%
  \def\noexpand\etb@ifhashcheck@ii##1\string#\string###2&}
\etb@ifhashcheck@ii{\ifblank{#2}}

% {<cstoken>}

\newrobustcmd*{\robustify}[1]{%
  \ifundef{#1}
    {\etb@error{\string#1 undefined}\@eha}
    {\ifdefmacro{#1}
       {\ifdefltxprotect{#1}
	  {\letcs\etb@tempa{\expandafter\@gobble\string#1 }%
           \@tempswatrue}
	  {\let\etb@tempa#1%
           \@tempswafalse}%
	\ifdefparam\etb@tempa
	  {\etb@ifscanable\etb@tempa
	     {\etb@robustify\etb@tempa
	      \let#1\etb@tempa}
	     {\etb@error{Failed to robustify \string#1}
		{The command is special and cannot be
		 handled by \string\robustify.}%
	      \@tempswafalse}}
	  {\protected\edef#1{\expandonce\etb@tempa}}
	\if@tempswa
	  \ifcsdef{\string#1 }
	    {}
	    {\csundef{\expandafter\@gobble\string#1 }}%
	\fi
	\undef\etb@tempa}
       {\etb@error{\string#1 not a macro}\@eha}}}

\def\etb@robustify#1{%
  \begingroup
  \edef\etb@resrvda{%
    \def\noexpand\etb@resrvda####1\detokenize{macro}:####2->####3&{%
      \protected####1\def\string#1####2{####3}}%
    \edef\noexpand\etb@resrvda{%
      \noexpand\etb@resrvda\meaning#1&}}%
  \etb@resrvda
  \etb@scantoks\endgroup\etb@resrvda}

%  {<cstoken>}{<search>}{<true}{<false>}
% *{<cstoken>}{<true}{<false>}

\newrobustcmd{\ifpatchable}{%
  \etb@dbg@trce\ifpatchable
  \begingroup
  \@makeother\#%
  \@ifstar\etb@ifpatchable@i\etb@ifpatchable}

\def\etb@ifpatchable#1#2{%
  \endgroup
  \etb@dbg@init{#1}%
  \ifundef{#1}
    {\etb@dbg@fail{def}\@secondoftwo}
    {\etb@dbg@info{def}%
     \ifdefmacro{#1}
       {\etb@dbg@info{mac}%
        \etb@ifscanable{#1}
          {\etb@ifhashcheck{#2}
	     {\etb@dbg@info{tok}%
	      \etb@ifpattern{#1}{#2}
		 {\etb@dbg@info{pat}%
		  \etb@dbg@info{pos}\@firstoftwo}
		 {\etb@dbg@fail{pat}\@secondoftwo}}
	     {\etb@dbg@fail{hsh}\@secondoftwo}}
	  {\etb@dbg@fail{tok}\@secondoftwo}}
       {\etb@dbg@fail{mac}\@secondoftwo}}}

\def\etb@ifpatchable@i#1{%
  \endgroup
  \etb@dbg@init{#1}%
  \ifundef{#1}
    {\etb@dbg@fail{def}\@secondoftwo}
    {\etb@dbg@info{def}%
     \ifdefmacro{#1}
       {\etb@dbg@info{mac}%
        \ifdefparam{#1}
	  {\etb@dbg@info{prm}%
	   \etb@ifscanable{#1}
	     {\etb@dbg@info{tok}%
	      \etb@dbg@info{pos}\@firstoftwo}
	     {\etb@dbg@fail{tok}\@secondoftwo}}
	  {\etb@dbg@info{prl}%
	   \ifdefprotected{#1}
	     {\etb@dbg@info{pro}}
	     {}%
	   \etb@dbg@info{pos}\@firstoftwo}}
       {\etb@dbg@fail{mac}\@secondoftwo}}}

% [<prefix>]{<cstoken>}{<search>}{<replace>}{<success>}{<failure>}

\newrobustcmd*{\patchcmd}{%
  \etb@dbg@trce\patchcmd
  \begingroup
  \@makeother\#%
  \etb@patchcmd}

\newcommand{\etb@patchcmd}[4][########1]{%
  \etb@ifpatchable{#2}{#3}
    {\etb@dbg@succ{ret}%
     \begingroup
     \edef\etb@resrvda{%
       \def\noexpand\etb@resrvda####1\detokenize{macro:}####2->####3&{%
         #1\def\string#2####2{\noexpand\etb@resrvdb####3&}}%
       \def\noexpand\etb@resrvdb####1\detokenize{#3}####2&{%
         ####1\detokenize{#4}####2}%
       \edef\noexpand\etb@resrvda{%
         \noexpand\etb@resrvda\meaning#2&}}%
     \etb@resrvda
     \etb@scantoks\endgroup\etb@resrvda
     \@firstoftwo}
    {\@secondoftwo}}

\def\etb@scantoks#1#2{%
  \begingroup
  \edef\etb@resrvda{\endgroup
    \unexpanded{#1\makeatletter\scantokens}{#2}%
    \catcode\number`\@=\the\catcode`\@\relax}%
  \etb@resrvda}

% {<cstoken>}{<code>}

\newrobustcmd*{\apptocmd}{%
  \etb@dbg@trce\apptocmd
  \begingroup
  \@makeother\#%
  \etb@hooktocmd\etb@append}

\newrobustcmd*{\pretocmd}{%
  \etb@dbg@trce\pretocmd
  \begingroup
  \@makeother\#%
  \etb@hooktocmd\etb@prepend}

\long\def\etb@hooktocmd#1#2#3{%
  \endgroup
  \etb@dbg@init{#2}%
  \ifundef{#2}
    {\etb@dbg@fail{def}\@secondoftwo}
    {\etb@dbg@info{def}%
     \ifdefmacro{#2}
       {\etb@dbg@info{mac}%
        \ifdefparam{#2}
          {\etb@dbg@info{prm}%
	   \etb@ifscanable{#2}
	     {\etb@ifhashcheck{#3}
	        {\etb@dbg@info{tok}%
		 \etb@dbg@succ{ret}%
		 \etb@hooktocmd@i{#1}{#2}{#3}%
		 \@firstoftwo}
		{\etb@dbg@fail{hsh}\@secondoftwo}}
	     {\etb@dbg@fail{tok}\@secondoftwo}}
	  {\etb@dbg@info{prl}%
	   \ifdefprotected{#2}
	     {\etb@dbg@info{pro}%
	      \etb@dbg@succ{red}%
	      \protected}
	     {\etb@dbg@succ{red}}%
	   \edef#2{#1{\expandonce#2}{\unexpanded{#3}}}%
	   \@firstoftwo}}
       {\etb@dbg@fail{mac}\@secondoftwo}}}

\long\def\etb@hooktocmd@i#1#2#3{%
  \begingroup
  \edef\etb@resrvda{%
    \def\noexpand\etb@resrvda####1\detokenize{macro}:####2->####3&{%
      ####1\def\string#2####2{#1{####3}{\detokenize{#3}}}}%
    \edef\noexpand\etb@resrvda{%
      \noexpand\etb@resrvda\meaning#2&}}%
  \etb@resrvda
  \etb@scantoks\endgroup\etb@resrvda}

\long\def\etb@append#1#2{#1#2}
\long\def\etb@prepend#1#2{#2#1}

\newrobustcmd*{\tracingpatches}{%
  \etb@info{Enabling tracing}%
  \input{etoolbox.def}%
  \global\let\tracingpatches\relax}
\@onlypreamble\tracingpatches

\let\etb@dbg@trce\@gobble
\let\etb@dbg@init\@gobble
\let\etb@dbg@info\@gobble
\let\etb@dbg@succ\@gobble
\let\etb@dbg@fail\@gobble

% {<numeral>}

\newcommand{\rmntonum}[1]{%
  \ifblank{#1}
    {}
    {\expandafter\etb@rti@end\number\numexpr
     \expandafter\etb@rti@prs\detokenize{#1}&\relax}}

\def\etb@rti@prs#1#2{%
  \ifx&#1%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#1#2}
  {\ifx&#2%
     \expandafter\@firstoftwo
   \else
     \expandafter\@secondoftwo
   \fi
   {\etb@rti@chk#1+\etb@rti@num#1#2}
   {\etb@rti@chk#1\etb@rti@chk#2%
    \ifnum\etb@rti@num#1<\etb@rti@num#2 %
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
    {+\etb@rti@num#2-\etb@rti@num#1\etb@rti@prs}
    {+\etb@rti@num#1\etb@rti@prs#2}}}}

\def\etb@rti@chk#1{%
  \ifcsname etb@rmn@#1\endcsname
  \else
    \expandafter\etb@rti@brk
  \fi}

\def\etb@rti@brk#1&{+\z@&-1}
\def\etb@rti@end#1&#2\relax{\ifblank{#2}{#1}{#2}}
\def\etb@rti@num#1{\csname etb@rmn@#1\endcsname}

\chardef\etb@rmn@i=1
\chardef\etb@rmn@I=1
\chardef\etb@rmn@v=5
\chardef\etb@rmn@V=5
\chardef\etb@rmn@x=10
\chardef\etb@rmn@X=10
\chardef\etb@rmn@l=50
\chardef\etb@rmn@L=50
\chardef\etb@rmn@c=100
\chardef\etb@rmn@C=100
\mathchardef\etb@rmn@d=500
\mathchardef\etb@rmn@D=500
\mathchardef\etb@rmn@m=1000
\mathchardef\etb@rmn@M=1000

% {<numeral>}{<true>}{<false>}

\newcommand{\ifrmnum}[1]{%
  \ifblank{#1}
    {\@secondoftwo}
    {\expandafter\etb@ifr@prs\detokenize{#1}\relax}}

\def\etb@ifr@prs#1{%
  \ifx\relax#1%
    \expandafter\@firstoftwo
  \else
    \ifcsname etb@rmn@#1\endcsname
      \expandafter\expandafter
      \expandafter\etb@ifr@prs
    \else
      \expandafter\expandafter
      \expandafter\etb@ifr@brk
    \fi
  \fi}

\def\etb@ifr@brk#1\relax{\@secondoftwo}

% {<command>}{<separator>}

\newrobustcmd*{\DeclareListParser}[2]{%
  \@ifdefinable#1{%
    \expandafter\expandafter\expandafter\etb@defparser
    \expandafter\expandafter\expandafter{%
      \expandafter\@gobble\string#1}{#2}}}

\def\etb@defparser#1#2{%
  \begingroup
  \edef\@tempa{\endgroup
    \long\csdef{#1}####1{\expandafter\noexpand
      \csname etb@lst@#1\endcsname####1\noexpand#2&}%
    \long\csdef{etb@lst@#1}####1\noexpand#2####2&{%
      \noexpand\ifblank{####1}
	{}
	{\noexpand\etb@doitem####1&}%
      \noexpand\ifblank{####2}
	{\noexpand\listbreak}
	{\expandafter\noexpand
	 \csname etb@lst@#1\endcsname####2}&}}%
  \@tempa}

\long\def\etb@doitem#1#2&{\do{#1#2}}
\newcommand*{\listbreak}{}
\long\def\listbreak#1&{}

% {<item1>,<item2>,...} => \do{<item1>}\do{<item2>}...

\DeclareListParser{\docsvlist}{,}

% {<listmacro>}{<string>}

\newrobustcmd{\listadd}[2]{%
  \ifblank{#2}{}{\appto#1{#2|}}}
\newrobustcmd{\listeadd}[2]{%
  \ifblank{#2}{}{\eappto#1{#2|}}}
\newrobustcmd{\listgadd}[2]{%
  \ifblank{#2}{}{\gappto#1{#2|}}}
\newrobustcmd{\listxadd}[2]{%
  \ifblank{#2}{}{\xappto#1{#2|}}}

% {<listcsname>}{<string>}

\newrobustcmd{\listcsadd}[1]{%
  \expandafter\listadd\csname#1\endcsname}
\newrobustcmd{\listcseadd}[1]{%
  \expandafter\listeadd\csname#1\endcsname}
\newrobustcmd{\listcsgadd}[1]{%
  \expandafter\listgadd\csname#1\endcsname}
\newrobustcmd{\listcsxadd}[1]{%
  \expandafter\listxadd\csname#1\endcsname}

% {<string>}{<listmacro>}{<true>}{<false>}

\newrobustcmd{\ifinlist}[2]{%
  \begingroup
  \def\etb@tempa##1|#1|##2&{\endgroup
    \ifblank{##2}\@secondoftwo\@firstoftwo}%
  \expandafter\etb@tempa\expandafter|#2|#1|&}

\newrobustcmd{\xifinlist}[1]{%
  \begingroup
  \edef\etb@tempa{\endgroup\ifinlist{#1}}%
  \etb@tempa}

% {<string>}{<listcsname>}{<true>}{<false>}

\newrobustcmd{\ifinlistcs}[2]{%
  \expandafter\etb@ifinlistcs@i\csname #2\endcsname{#1}}
\long\def\etb@ifinlistcs@i#1#2{\ifinlist{#2}{#1}}

\newrobustcmd{\xifinlistcs}[1]{%
  \begingroup
  \edef\etb@tempa{\endgroup\ifinlistcs{#1}}%
  \etb@tempa}

% {<listmacro>} => \do{<item1>}\do{<item2>}...

\newcommand*{\dolistloop}[1]{%
  \expandafter\etb@listloop\expandafter{#1}}

\DeclareListParser{\etb@listloop}{|}

% {<listcsname>} => \do{<item1>}\do{<item2>}...

\newcommand*{\dolistcsloop}[1]{%
  \expandafter\dolistloop\csname#1\endcsname}

% {<code>}

\newrobustcmd*{\AtEndPreamble}{\gappto\@endpreamblehook}
\newcommand*{\@endpreamblehook}{}

\preto\document{%
  \endgroup
  \let\AtEndPreamble\@firstofone
  \@endpreamblehook
  \protected\def\AtEndPreamble{\@notprerr\@gobble}%
  \undef\@endpreamblehook
  \begingroup}

% {<code>}

\newrobustcmd*{\AfterPreamble}{\AtBeginDocument}
\AtEndPreamble{\let\AfterPreamble\@firstofone}

% {<code>}

\newrobustcmd*{\AfterEndPreamble}{\gappto\@afterendpreamblehook}
\newcommand*{\@afterendpreamblehook}{}

\appto\document{%
  \let\AfterEndPreamble\@firstofone
  \@afterendpreamblehook
  \protected\def\AfterEndPreamble{\@notprerr\@gobble}%
  \undef\@afterendpreamblehook
  \ignorespaces}

% {<code>}

\newrobustcmd*{\AfterEndDocument}{\gappto\@afterenddocumenthook}
\newcommand*{\@afterenddocumenthook}{}

\patchcmd\enddocument
  {\deadcycles}
  {\let\AfterEndDocument\@firstofone
   \@afterenddocumenthook
   \deadcycles}
  {}
  {\let\etb@@end\@@end
   \def\@@end{%
     \let\AfterEndDocument\@firstofone
     \@afterenddocumenthook
     \etb@@end}}

\endinput