%%
%% This is file `extract.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% extract.dtx  (with options: `extract')
%% 
%% ---------------------------------------
%% Copyright (C) 2004-2005 Hendri Adriaens
%% ---------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%%
%% This work has the LPPL maintenance status "maintained".
%%
%% This Current Maintainer of this work is Hendri Adriaens.
%%
%% This work consists of the file extract.dtx and the derived files
%% extract.sty, xtrex1.tex, xtrex2.tex, xtrex3.tex, xtrex4.tex,
%% xtrex5.tex, xtrex6.tex and xtrex7.tex.
%%
%% The following files constitute the extract package and must be
%% distributed as a whole: readme, extract.dtx, extract.pdf and
%% extract.sty.
%%
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{extract}
  [2005/05/07 v1.8 extract content from document (HA)]
\RequirePackage{verbatim}
\RequirePackage{xkeyval}
\newwrite\XTR@out
\newwrite\XTR@tmp
\newif\ifXTR@st
\newif\ifXTR@skip
\newif\ifXTR@extract
\def\XTR@err#1{\PackageError{extract}{#1}\@ehc}
\def\XTR@namelet#1#2{%
  \expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname
}
\define@boolkey[XTR]{extract.sty}[XTR@]{active}[true]{}
\define@boolkey[XTR]{extract.sty}[XTR@]{header}[true]{}
\define@boolkey[XTR]{extract.sty}[XTR@]{handles}[true]{}
\define@boolkey[XTR]{extract.sty}[XTR@]{copydocumentclass}[true]{}
\DeclareOptionX[XTR]{generate}{\lowercase{\def\XTR@file{#1}}}
\DeclareOptionX[XTR]{extract-env}{%
  \def\XTR@envs{#1}%
  \XKV@for@n{#1}\XTR@tempa\XTR@tempb
}
\DeclareOptionX[XTR]{extract-cmd}{%
  \def\XTR@cmdsargs{#1}%
  \XKV@for@n{#1}\XTR@tempa\XTR@tempb
}
\DeclareOptionX[XTR]{extract-cmdline}{%
  \def\XTR@cmdsline{#1}%
  \XKV@for@n{#1}\XTR@tempa\XTR@tempb
}
\def\XTR@tempb{%
  \DeclareOptionX[XTR]{\XTR@tempa-nrs}{%
    \expandafter\XKV@sp@deflist\csname XTR@\XKV@tkey\endcsname{##1}%
    \XTR@namelet{XTR@\XKV@tkey @cnt}{z@}%
  }%
  \DeclareOptionX[XTR]{\XTR@tempa-labels}{%
    \expandafter\XKV@sp@deflist\csname XTR@\XKV@tkey\endcsname{##1}%
  }%
}
\def\XTR@tempa{line}\XTR@tempb
\def\XTR@tempa{extract}\XTR@tempb
\def\XTR@tempa{extractskip}\XTR@tempb
\DeclareOptionX*{\XTR@err{Unknown option `\CurrentOption'}}
\ExecuteOptionsX[XTR]{header=true,handles=true,copydocumentclass=true}
\ProcessOptionsX[XTR]
\def\XTR@opentmp{\immediate\openout\XTR@tmp\jobname.xtr\relax}
\def\XTR@writetmp{\immediate\write\XTR@tmp}
\def\XTR@closetmp{\immediate\closeout\XTR@tmp}
\def\XTR@writeout{\immediate\write\XTR@out}
\ifXTR@active
  \XKV@ifundefined{XTR@file}{
    \XTR@activefalse
    \XTR@err{no file to generate; extract deactivated}
  }{}
  \XTR@opentmp
  \XTR@writetmp{%
    \string\lowercase{\string\def\string\XTR@tempa{\jobname}}%
  }
  \XTR@closetmp
  \input{\jobname.xtr}
  \ifx\XTR@tempa\XTR@file
    \XTR@activefalse
    \XTR@err{attempt to overwrite source file; extract deactivated}
  \fi
\fi
\newcount\@envdepth\@envdepth\z@
\def\begin#1{%
  \@ifundefined{#1}%
    {\def\reserved@a{\@latex@error{Environment #1 undefined}\@eha}}%
    {\def\reserved@a{\def\@currenvir{#1}%
     \edef\@currenvline{\on@line}%
     \csname #1\endcsname}}%
  \@ignorefalse
  \begingroup\@endpefalse
  \global\advance\@envdepth\@ne
  \global\@namedef{@afterendenvhook@\romannumeral\@envdepth}{}%
  \reserved@a}
\def\end#1{%
  \csname end#1\endcsname\@checkend{#1}%
  \expandafter\endgroup\if@endpe\@doendpe\fi
  \expandafter\let\expandafter\reserved@a
    \csname @afterendenvhook@\romannumeral\@envdepth\endcsname
  \global\advance\@envdepth\m@ne
  \reserved@a\relax
  \if@ignore\@ignorefalse\ignorespaces\fi}
\def\AfterEndEnv{%
  \expandafter\g@addto@macro
    \csname @afterendenvhook@\romannumeral\@envdepth\endcsname
}
\def\XTR@checkxtr#1#2{%
  \@nameuse{XTR@#1false}%
  \XTR@namelet{XTR@maketrue}{XTR@#1true}%
  \XKV@ifundefined{XTR@#2-nrs}{%
    \XKV@ifundefined{XTR@#2-labels}\XTR@maketrue{}%
  }{%
    \begingroup
      \expandafter\count@\csname XTR@#2-nrs@cnt\endcsname
      \advance\count@\@ne
      \edef\XTR@resa{\expandafter\noexpand\expandafter\gdef\expandafter
        \noexpand\csname XTR@#2-nrs@cnt\endcsname{\the\count@}}%
    \expandafter\endgroup\XTR@resa
  }%
  \@nameuse{ifXTR@#1}\else
    \XKV@ifundefined{XTR@#2-labels}{}{%
      \ifx\XTR@currentlabel\relax\else
        \@expandtwoargs\in@{,\XTR@currentlabel,}%
          {,\csname XTR@#2-labels\endcsname,}%
        \ifin@\XTR@maketrue\fi
      \fi
    }%
  \fi
  \@nameuse{ifXTR@#1}\else
    \XKV@ifundefined{XTR@#2-nrs}{}{%
      \expandafter\XTR@ch@ckxtr\csname XTR@#2-nrs\expandafter
        \endcsname\csname XTR@#2-nrs@cnt\endcsname
    }%
  \fi
  \global\let\XTR@currentlabel\relax
}
\def\XTR@ch@ckxtr#1#2{%
  \XKV@for@o#1\XTR@resa{\expandafter\XTR@ch@ck@tr\XTR@resa--\@nil#2}%
}
\def\XTR@ch@ck@tr#1-#2-#3\@nil#4{%
  \ifx\@empty#1\@empty
    \ifnum#4>#2 \else\XTR@maketrue\fi
  \else
    \ifx\@empty#2\@empty
      \ifx\@empty#3\@empty
        \ifnum#4=#1 \XTR@maketrue\fi
      \else
        \ifnum#4<#1 \else\XTR@maketrue\fi
      \fi
    \else
      \ifnum#4<#1 \else\ifnum#4>#2 \else\XTR@maketrue\fi\fi
    \fi
  \fi
}
\def\extractionlabel{\KV@@sp@def\XTR@currentlabel}
\let\XTR@currentlabel\relax
\def\extract{\XTR@stfalse\XTR@extract}
\@namedef{extract*}{\XTR@sttrue\XTR@extract}
\def\XTR@extract{%
  \@bsphack
  \let\do\@makeother\dospecials\catcode`\^^M\active
  \@testopt\XTR@@xtract\@nil
}
\def\XTR@@xtract[#1]{%
  \ifXTR@active
    \def\XTR@tempa{#1}%
    \ifx\XTR@tempa\@nnil\else
      \KV@@sp@def\XTR@currentlabel{#1}%
    \fi
    \XTR@checkxtr{extract}{extract}%
  \else
    \XTR@extractfalse
  \fi
  \ifXTR@st\XTR@opentmp\fi
  \let\verbatim@processline\XTR@processline@begin
  \verbatim@start
}
\begingroup
  \lccode`\!=`\\ \lccode`\(=`\{ \lccode`\)=`\}
\lowercase{\endgroup
\def\XTR@processline@begin{%
  \@temptokena{}%
  \edef\XTR@orig@line{\the\verbatim@line}%
  \verbatim@line{}%
  \expandafter\XTR@testbegin\XTR@orig@line!begin(extractskip)\@nil
}
\def\XTR@testbegin#1!begin(extractskip)#2\@nil{%
  \@temptokena\expandafter{\the\@temptokena#1}%
  \verbatim@line\expandafter{\the\verbatim@line#1}%
  \def\XTR@tempa{#2}%
  \ifx\XTR@tempa\@empty\XTR@processline@write\else\XKV@afterfi
    \XTR@skiplabel#2[]\@nil
    \XTR@checkxtr{skip}{extractskip}%
    \let\verbatim@processline\XTR@processline@end
    \ifx\XTR@tempa\@nnil\XKV@afterelsefi
      \XTR@t@stbegin#2\@nil
    \else\XKV@afterfi
      \expandafter\XTR@t@stbegin\XTR@tempa\@nil
    \fi
  \fi
}
\def\XTR@skiplabel#1[#2]#3\@nil{%
  \def\XTR@tempa{#1}%
  \def\XTR@tempb{#2}%
  \ifx\XTR@tempa\@empty
    \ifx\XTR@tempb\@empty
      \let\XTR@tempa\@nnil
    \else
      \KV@@sp@def\XTR@currentlabel{#2}%
      \XTR@sk@plabel#3\@nil
    \fi
  \else
    \let\XTR@tempa\@nnil
  \fi
}
\def\XTR@sk@plabel#1[]\@nil{\def\XTR@tempa{#1}}
\def\XTR@t@stbegin#1!begin(extractskip)\@nil{%
  \XTR@testend#1!end(extractskip)\@nil
}
\def\XTR@processline@end{%
  \@temptokena{}%
  \edef\XTR@orig@line{\the\verbatim@line}%
  \verbatim@line{}%
  \expandafter\XTR@testend\XTR@orig@line!end(extractskip)\@nil
}
\def\XTR@testend#1!end(extractskip)#2\@nil{%
  \@temptokena\expandafter{\the\@temptokena#1}%
  \ifXTR@skip\else\verbatim@line\expandafter{\the\verbatim@line#1}\fi
  \def\XTR@tempa{#2}%
  \ifx\XTR@tempa\@empty\XTR@processline@write\else\XKV@afterfi
    \let\verbatim@processline\XTR@processline@begin
    \XTR@t@stend#2\@nil
  \fi
}
\def\XTR@t@stend#1!end(extractskip)\@nil{%
  \XTR@testbegin#1!begin(extractskip)\@nil
}}
\def\XTR@processline@write{%
  \ifXTR@st\ifcat$\the\@temptokena$\else
    \XTR@writetmp{\the\@temptokena}%
  \fi\fi
  \ifXTR@extract\ifcat$\the\verbatim@line$\else
    \XTR@writeout{\the\verbatim@line}%
  \fi\fi
  \ifx\XTR@orig@line\@empty\XTR@writetmp{}\XTR@writeout{}\fi
}
\def\endextract{\XTR@stfalse\XTR@endextract}
\@namedef{endextract*}{\XTR@sttrue\XTR@endextract}
\def\XTR@endextract{%
  \@esphack
  \ifXTR@st
    \XTR@closetmp
    \AfterEndEnv{\input{\jobname.xtr}}%
  \fi
}
\@namedef{extractskip}{\XTR@activefalse\@nameuse{extract*}}
\XTR@namelet{endextractskip}{endextract*}
\def\extractline{%
  \XKV@ifstar{\XTR@sttrue\XTR@extractline}%
    {\XTR@stfalse\XTR@extractline}%
}
\def\XTR@extractline{%
  \begingroup
    \let\do\@makeother\dospecials\catcode`\^^M\active
    \@testopt\XTR@@xtractline\@nil
}
\begingroup
  \catcode`\~=\active\lccode`\~=`\^^M
\lowercase{\endgroup
  \def\XTR@@xtractline[#1]#2~{%
      \ifXTR@active
        \def\XTR@tempa{#1}%
        \ifx\XTR@tempa\@nnil\else
          \KV@@sp@def\XTR@currentlabel{#1}%
        \fi
        \XTR@checkxtr{extract}{line}%
      \else
        \XTR@extractfalse
      \fi
      \ifXTR@extract\XTR@writeout{#2}\fi
      \ifXTR@st\XTR@opentmp\XTR@writetmp{#2}\XTR@closetmp\fi
    \endgroup
    \ifXTR@st
      \input{\jobname.xtr}%
    \fi
  }%
}
\ifXTR@active
\immediate\openout\XTR@out\XTR@file\relax
\ifXTR@header
  \@tempcnta\time
  \divide\@tempcnta 60
  \edef\XTR@tempb{%
    \the\year/\ifnum\the\month<10 0\fi\the\month/%
    \ifnum\the\day<10 0\fi\the\day,\the\@tempcnta:%
  }
  \multiply\@tempcnta 60
  \@tempcntb\time
  \advance\@tempcntb-\@tempcnta
  \ifnum\@tempcntb<10
    \xdef\XTR@tempb{\XTR@tempb0\the\@tempcntb}
  \else
    \xdef\XTR@tempb{\XTR@tempb\the\@tempcntb}
  \fi
  \begingroup
    \catcode`\%=12
    \gdef\XTR@tempa{%%\space}
  \endgroup
  \XTR@writeout{\XTR@tempa}
  \filename@parse\XTR@file
  \ifx\filename@ext\relax\def\filename@ext{tex}\fi
  \XTR@writeout{%
    \XTR@tempa This is file, `\filename@base.\filename@ext',%
  }
  \XTR@writeout{%
    \XTR@tempa generated with the extract package.^^J\XTR@tempa
  }
  \XTR@writeout{\XTR@tempa Generated on : \space\XTR@tempb}
  \filename@parse\jobname
  \ifx\filename@ext\relax\def\filename@ext{tex}\fi
  \XTR@writeout{%
    \XTR@tempa From source \space: \space\filename@base.\filename@ext
  }
  \XTR@writeout{%
    \XTR@tempa Using options: \space\csname opt@extract.sty\endcsname
  }
  \XTR@writeout{\XTR@tempa}
\fi
\ifXTR@copydocumentclass
  \def\XTR@tempa#1.cls\@nil{\def\XTR@tempa{#1}}
  \expandafter\XTR@tempa\XKV@documentclass\@nil
  \ifx\XKV@classoptionslist\@empty
    \XTR@writeout{\string\documentclass{\XTR@tempa}}
  \else
    \@temptokena\expandafter{\XKV@classoptionslist}%
    \XTR@writeout{\string\documentclass[\the\@temptokena]{\XTR@tempa}}
  \fi
\fi
\AtBeginDocument{%
  \ifXTR@handles
    \XTR@writeout{}%
    \XTR@writeout{\string\begin{document}}%
  \fi
  \XKV@ifundefined{XTR@envs}{}{%
    \XKV@for@o\XTR@envs\XTR@tempa{%
      \XKV@ifundefined\XTR@tempa{%
        \XTR@err{%
          environment `\XTR@tempa' not defined; extraction canceled%
        }%
      }{%
        \XTR@namelet{XTR\XTR@tempa}{\XTR@tempa}%
        \@namedef{\XTR@tempa\expandafter}\expandafter{\expandafter
          \def\expandafter\XTR@tempa\expandafter{\XTR@tempa}%
          \XTR@checkxtr{extract}\XTR@tempa
          \ifXTR@extract
            \XTR@writeout{}\XTR@opentmp
            \@bsphack
            \let\do\@makeother\dospecials\catcode`\^^M\active
            \def\verbatim@processline{%
              \let\verbatim@processline\XTR@processline@begin
              \XTR@writeout{%
                \string\begin{\XTR@tempa}\the\verbatim@line
              }%
              \XTR@writetmp{%
                \string\begin{XTR\XTR@tempa}\the\verbatim@line
              }%
            }%
            \XTR@sttrue\let\XTR@tempb\verbatim@
          \else
            \edef\XTR@tempb{\noexpand\begin{XTR\XTR@tempa}}%
          \fi
          \XTR@tempb
        }%
        \XTR@namelet{endXTR\XTR@tempa}{end\XTR@tempa}%
        \@namedef{end\XTR@tempa\expandafter}\expandafter{\expandafter
          \def\expandafter\XTR@tempa\expandafter{\XTR@tempa}%
          \ifXTR@extract
            \@esphack
            \XTR@writeout{\string\end{\XTR@tempa}}%
            \XTR@writetmp{\string\end{XTR\XTR@tempa}}%
            \XTR@closetmp
            \AfterEndEnv{\input{\jobname.xtr}}%
          \else
            \edef\XTR@tempa{\noexpand\end{XTR\XTR@tempa}}%
            \expandafter\XTR@tempa
          \fi
        }%
      }%
    }%
  }%
  \XKV@ifundefined{XTR@cmdsargs}{}{%
    \let\XTR@sect\@sect
    \let\XTR@chapter\@chapter
    \def\XTR@tempb{chapter}%
    \XKV@for@o\XTR@cmdsargs\XTR@tempa{%
      \XKV@ifundefined\XTR@tempa{%
        \XTR@err{command `\@backslashchar\XTR@tempa' not defined;
          extraction canceled%
        }%
      }{%
        \@expandtwoargs\in@{,\XTR@tempa,}%
          {,chapter,section,subsection,subsubsection,}%
        \ifin@
          \ifx\XTR@tempa\XTR@tempb
            \def\@chapter[#1]#2{%
              \XTR@checkxtr{extract}{chapter}%
              \ifXTR@extract
                \XTR@writeout{}%
                \def\XTR@tempa{#1}%
                \def\XTR@tempb{#2}%
                \ifx\XTR@tempa\XTR@tempb
                  \@temptokena{{#2}}%
                \else
                  \@temptokena{[#1]{#2}}%
                \fi
                \XTR@writeout{\string\chapter\the\@temptokena}%
              \fi
              \XTR@chapter[#1]{#2}%
            }%
          \else
            \def\@sect#1#2#3#4#5#6[#7]#8{%
              \@expandtwoargs\in@{,#1,}{,\XTR@cmdsargs,}%
              \ifin@
                \XTR@checkxtr{extract}{#1}%
                \ifXTR@extract
                  \XTR@writeout{}%
                  \def\XTR@tempa{#7}%
                  \def\XTR@tempb{#8}%
                  \ifx\XTR@tempa\XTR@tempb
                    \@temptokena{{#8}}%
                  \else
                    \@temptokena{[#7]{#8}}%
                  \fi
                  \XTR@writeout{\expandafter
                    \string\csname#1\endcsname\the\@temptokena}%
                \fi
              \fi
              \XTR@sect{#1}{#2}{#3}{#4}{#5}{#6}[#7]{#8}%
            }%
          \fi
        \else
          \XTR@err{unsupported command `\XTR@tempa';
            try the `extract-cmdline option}%
        \fi
      }%
    }%
  }%
  \XKV@ifundefined{XTR@cmdsline}{}{%
    \XKV@for@o\XTR@cmdsline\XTR@tempa{%
      \XKV@ifundefined\XTR@tempa{%
        \XTR@err{command `\@backslashchar\XTR@tempa' not defined;
          extraction canceled}%
      }{%
        \@expandtwoargs\in@{,\XTR@tempa,}%
          {,chapter,section,subsection,subsubsection,}%
        \ifin@
          \XTR@err{%
            use the `extract-cmd' option for command `\XTR@tempa'%
          }%
        \else
          \XTR@namelet{XTR\XTR@tempa}{\XTR@tempa}%
          \@namedef{\XTR@tempa\expandafter}\expandafter{\expandafter
            \def\expandafter\XTR@tempa\expandafter{\XTR@tempa}%
            \XTR@checkxtr{extract}\XTR@tempa
              \begingroup
                \let\do\@makeother\dospecials\catcode`\^^M\active
                \XTR@extractcmdline
          }%
        \fi
      }%
    }%
    \begingroup
      \catcode`\~=\active\lccode`\~=`\^^M
    \lowercase{\endgroup
      \def\XTR@extractcmdline#1~{\verbatim@line{#1}\XTR@@xtractcmdline}%
    }%
    \def\XTR@@xtractcmdline{%
        \XTR@writeout{}%
        \XTR@writeout{\expandafter\string\csname\XTR@tempa
          \endcsname\the\verbatim@line
        }%
        \XTR@opentmp
        \XTR@writetmp{\expandafter\string\csname XTR\XTR@tempa
          \endcsname\the\verbatim@line
        }%
        \XTR@closetmp
      \endgroup
      \input{\jobname.xtr}%
    }%
  }%
}
\AtEndDocument{%
  \ifXTR@handles
    \XTR@writeout{}%
    \XTR@writeout{\string\end{document}}%
  \fi
  \immediate\closeout\XTR@out
}
\fi
\endinput
%%
%% End of file `extract.sty'.
