% $Id: etoolbox.sty,v 1.3 2007/10/08 17:42:31 lehman stable $

% Copyright (c) 2007 Philipp Lehman, author-maintained.
%
% Permission is granted to copy, distribute and/or modify this
% software under the terms of the LaTeX Project Public License
% (LPPL), version 1.3.
%
% 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}[2007/10/08 v1.3 eTeX 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\elx@restore{%
  \catcode\number`\&=\the\catcode`\&\relax
  \catcode\number`\|=\the\catcode`\|\relax}
\catcode`\&=3
\catcode`\|=3

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

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

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

\def\elx@new@command#1{\@testopt{\elx@newcommand#1}0}
\def\elx@newcommand#1[#2]{%
  \@ifnextchar[%]
    {\elx@xargdef#1[#2]}
    {\@argdef#1[#2]}}
\long\def\elx@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\elx@renew@command}
    {\def\l@ngrel@x{\protected\long}\elx@renew@command}}

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

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

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

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

% {<cstoken>}

\@onlypreamble\robustify
\newrobustcmd*{\robustify}[1]{%
  \ifundef#1%
    {\@latex@error{\string#1 undefined}\@ehc}
    {\begingroup
     \edef\elx@resrvda{\meaning#1}%
     \edef\elx@resrvdb{%
       \noexpand\protect\expandafter\noexpand
       \csname\expandafter\@gobble\string#1 \endcsname}%
     \edef\elx@resrvdb{\meaning\elx@resrvdb}%
     \ifx\elx@resrvda\elx@resrvdb
       \letcs#1{\expandafter\@gobble\string#1 }%
       \cslet{\expandafter\@gobble\string#1 }\@undefined
     \fi
     \edef\elx@resrvda{%
       \def\noexpand\elx@resrvda####1\detokenize{macro:}####2->####3&{%
         \protected####1\def\detokenize{#1}####2{####3}}%
       \edef\noexpand\elx@resrvda{%
         \noexpand\elx@resrvda\meaning#1&}}%
     \elx@resrvda
     \elx@scantoks\endgroup\elx@resrvda}}

% {<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*{\ifdefvoid}[1]{%
  \ifundef#1%
    {\@firstoftwo}
    {\ifx#1\@empty
       \expandafter\@firstoftwo
     \else
       \expandafter\@secondoftwo
     \fi}}

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

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

% {<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\expandafter
        \expandafter\ifx
        \expandafter\expandafter
        \csname#1\endcsname
        \csname#2\endcsname
          \expandafter\@firstoftwo
        \else
          \expandafter\@secondoftwo
        \fi}}}

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

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

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

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

% {<name>}

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

\newrobustcmd*{\providebool}[1]{%
  \ifcsundef{if#1}
    {\expandafter\newif\csname if#1\endcsname}
    {\begingroup
     \edef\@tempa{\expandafter\meaning\csname if#1\endcsname}%
     \ifx\@tempa\elx@isfalse
     \else
       \ifx\@tempa\elx@istrue
       \else
         \@latex@error{`\@backslashchar if#1' defined
	               but not a switch}\@eha
       \fi
     \fi
     \endgroup}}

\edef\elx@istrue{\meaning\iftrue}
\edef\elx@isfalse{\meaning\iffalse}

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

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

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

\newcommand*{\ifbool}[1]{%
  \ifcsundef{if#1}
    {\elx@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}
    {\elx@err@nobool{#1}\@gobbletwo}
    {\csname if#1\endcsname
       \expandafter\@secondoftwo
     \else
       \expandafter\@firstoftwo
     \fi}}

\protected\def\elx@err@nobool#1{%
  \let\relax\relax
  \@latex@error{Switch `\@backslashchar if#1' undefined}\@eha}

% {<name>}

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

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

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

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

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

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

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

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

\protected\def\elx@err@notoggle#1{%
  \let\relax\relax
  \@latex@error{Toggle `#1' undefined}\@eha}

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

\@onlypreamble\ifpatchable
\newrobustcmd{\ifpatchable}{%
  \begingroup
  \@makeother\#%
  \elx@ifpatchable}

\def\elx@ifpatchable#1#2{%
  \endgroup
  \ifundef{#1}
    {\@firstoftwo}
    {\begingroup
     \edef\elx@resrvda{%
       \def\noexpand\elx@resrvda####1->####2&{%
         \noexpand\elx@resrvdb####2\detokenize{#2}&}%
       \def\noexpand\elx@resrvdb####1\detokenize{#2}####2&{%
         \endgroup\noexpand\noexpand\noexpand\ifblank{####2}}%
       \edef\noexpand\elx@resrvda{%
         \noexpand\elx@resrvda\meaning#1&}%
       \noexpand\elx@resrvda}%
     \elx@resrvda}
    {\@secondoftwo}
    {\@firstoftwo}}

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

\@onlypreamble\patchcmd
\newrobustcmd*{\patchcmd}{%
  \begingroup
  \@makeother\#%
  \elx@patchcmd}

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

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

% {<cstoken>}{<code>}

\@onlypreamble\apptocmd
\newrobustcmd*{\apptocmd}{%
  \begingroup
  \@makeother\#%
  \elx@hooktocmd\elx@apptocmd}

\@onlypreamble\pretocmd
\newrobustcmd*{\pretocmd}{%
  \begingroup
  \@makeother\#%
  \elx@hooktocmd\elx@pretocmd}

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

\long\def\elx@apptocmd#1#2{#1#2}
\long\def\elx@pretocmd#1#2{#2#1}

% {<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
    \elx@protecting\@firstofone
  \fi
  \ifx\protect\@unexpandable@protect
    \elx@protecting\elx@unexpandable
  \fi
  \ifx\protect\noexpand
    \elx@protecting\unexpanded
  \fi
  \ifx\protect\string
    \elx@protecting\detokenize
  \fi
  \relax\@firstofone}

\def\elx@protecting#1#2\relax\@firstofone{\fi#1}
\long\def\elx@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}{\elx@protected\csedef}
\newrobustcmd*{\protected@csxdef}{\elx@protected\csxdef}

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

\newcommand*{\undef}{}

% {<csname>}

\newcommand*{\csundef}{}

\begingroup
\catcode`\%=11

\protected\gdef\undef#1{\let#1\elx@un%de%fi%ned}
\protected\gdef\csundef#1{\cslet{#1}\elx@un%de%fi%ned}

\endgroup

% {<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}{\elx@protected\eappto}
\newrobustcmd*{\protected@xappto}{\elx@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}{\elx@protected\epreto}
\newrobustcmd*{\protected@xpreto}{\elx@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}{\elx@protected\cseappto}
\newrobustcmd*{\protected@csxappto}{\elx@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}{\elx@protected\csepreto}
\newrobustcmd*{\protected@csxpreto}{\elx@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}

% {<numeral>}

\newcommand*{\rmntonum}{}

\begingroup
\catcode`\%=12
\catcode`\&=14

\gdef\rmntonum#1{\number\numexpr\elx@rti@i#10%}

\gdef\elx@rti@i#1#2{&
  \expandafter\if\expandafter%\detokenize{#2}&
    +\elx@rti@ii{#1}\expandafter\relax
  \else
    \ifnum\elx@rti@ii{#1}<\elx@rti@ii{#2} &
      +\elx@rti@ii{#2}-\elx@rti@ii{#1}&
      \expandafter\expandafter\expandafter\elx@rti@i
    \else
      +\elx@rti@ii{#1}&
      \expandafter\expandafter\expandafter\elx@rti@i
      \expandafter\expandafter\expandafter#2&
    \fi
  \fi}

\endgroup

\def\elx@rti@ii#1{%
  \ifcsname elx@rmn@\detokenize{#1}\endcsname
    \csname elx@rmn@\detokenize{#1}\expandafter\endcsname
  \else
    \expandafter\z@
  \fi}

\def\elx@rmn@i{1}
\def\elx@rmn@v{5}
\def\elx@rmn@x{10}
\def\elx@rmn@l{50}
\def\elx@rmn@c{100}
\def\elx@rmn@d{500}
\def\elx@rmn@m{1000}

\def\elx@rmn@I{1}
\def\elx@rmn@V{5}
\def\elx@rmn@X{10}
\def\elx@rmn@L{50}
\def\elx@rmn@C{100}
\def\elx@rmn@D{500}
\def\elx@rmn@M{1000}

% {<command>}{<separator>}

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

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

\long\def\elx@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\elx@tempa##1|#1|##2&{\endgroup
    \ifblank{##2}\@secondoftwo\@firstoftwo}%
  \expandafter\elx@tempa\expandafter|#2|#1|&}

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

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

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

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

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

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

\DeclareListParser{\elx@listloop}{|}

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

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

% {<code>}

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

\patchcmd{\document}
  {\endgroup}
  {\endgroup
   \let\AtEndPreamble\@firstofone
   \@endpreamblehook
   \global\let\@endpreamblehook\@undefined}
  {}
  {\PackageError{etoolbox}
     {Patching \string\document\space failed}
     {This is an internal error typically caused by a
      conflict\MessageBreak between etoolbox and at least
      one other package. Loading\MessageBreak the etoolbox
      package earlier may avoid this problem.}}

% {<code>}

\newcommand*{\AfterEndDocument}{\gappto\@afterdocumenthook}
\newcommand*{\@afterdocumenthook}{}

\patchcmd\enddocument
  {\deadcycles}
  {\let\AfterEndDocument\@firstofone
   \@afterdocumenthook
   \deadcycles}
  {}
  {\PackageInfo{etoolbox}{%
     Patching \string\enddocument\space failed.\MessageBreak
     Will try to fork \string\@@end\@gobble}%
   \let\elx@@end\@@end
   \def\@@end{%
     \let\AfterEndDocument\@firstofone
     \@afterdocumenthook
     \elx@@end}}

% {<code>}

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

\endinput
