% \iffalse meta-comment
%
% extract package by Hendri Adriaens.
%
% Extract package files and examples and create documentation:
%    latex extract.dtx
%    latex extract.dtx
%    bibtex extract
%    makeindex -s gglo.ist -o extract.gls extract.glo
%    makeindex -s gind.ist -o extract.ind extract.idx
%    latex extract.dtx
%    latex extract.dtx
%
% To finish the installation you have to move the following
% file into a directory searched by LaTeX:
%    extract.sty
%
%% ---------------------------------------
%% 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.
%%
%<*batchfile>
\begingroup
\input docstrip
\keepsilent
\preamble
\endpreamble
\askforoverwritefalse
\generate{
  \file{extract.sty}{\from{extract.dtx}{extract}}
  \file{xtrex1.tex}{\from{extract.dtx}{xtrex1}}
  \file{xtrex2.tex}{\from{extract.dtx}{xtrex2}}
  \file{xtrex3.tex}{\from{extract.dtx}{xtrex3}}
  \file{xtrex4.tex}{\from{extract.dtx}{xtrex4}}
  \file{xtrex5.tex}{\from{extract.dtx}{xtrex5}}
  \file{xtrex6.tex}{\from{extract.dtx}{xtrex6}}
  \file{xtrex7.tex}{\from{extract.dtx}{xtrex7}}
  \file{xtrpream.ble}{\from{extract.dtx}{preamble}}
  \file{extract.bib}{\from{extract.dtx}{bib}}
}
\endgroup
%</batchfile>
%<*driver>
\documentclass[a4paper]{ltxdoc}
\input{xtrpream.ble}
\begin{document}
\DocInput{extract.dtx}
\let\Section\section\def\section*#1{\Section*{#1}\addcontentsline{toc}{section}{#1}}
\bibliographystyle{plain}
\bibliography{extract}
\section*{Acknowledgements}
Thanks go to David Carlisle for proposing the technique implemented
in this package to solve my initial problem.\footnote{See
section~\ref{sec:intro}.} Thanks also go to Michael Kohlhase and
Johannes Luber for reporting bugs and providing ideas for extensions
of the package.
\PrintChangesX\PrintIndexX
\end{document}
%</driver>
% \fi
%
% \changes{v1.0}{2004/12/19}{Initial release}
% \changes{v1.1}{2005/01/01}{Added conditional extraction using labels}
% \changes{v1.1}{2005/01/01}{Revised documentation}
% \changes{v1.2}{2005/01/06}{Fixed \cs{AfterEndEnv} hook for nested environments}
% \changes{v1.2}{2005/01/06}{Simplified optional argument check for \texttt{extract} environment}
% \changes{v1.2}{2005/01/06}{Increased efficiency of the package}
% \changes{v1.3}{2005/01/12}{Removed some input checks for more flexibility}
% \changes{v1.3}{2005/01/12}{Added \cs{extractline}}
% \changes{v1.3}{2005/01/12}{Added line extraction method for commands}
% \changes{v1.3}{2005/01/12}{Simplified package}
% \changes{v1.3}{2005/01/12}{Added \texttt{document-handles} option}
% \changes{v1.3}{2005/01/12}{Added examples and documentation}
% \changes{v1.3}{2005/01/12}{Updated license information}
% \changes{v1.4}{2005/02/06}{Adjusted options section for \pf{xkeyval} (\cs{XKV@tkey} not defined anymore)}
% \changes{v1.4}{2005/02/06}{Added \texttt{extractskip} environment}
% \changes{v1.4}{2005/02/06}{Allow for any filename}
% \changes{v1.4}{2005/02/06}{Changed temp extension to \texttt{.xtr}}
% \changes{v1.5}{2005/02/08}{Solved bug in options section}
% \changes{v1.5}{2005/02/08}{Avoid using counters for conditional extraction with numbers}
% \changes{v1.6}{2005/02/14}{Improved options section}
% \changes{v1.6}{2005/02/14}{Added optional header for extracted files}
% \changes{v1.7}{2005/03/31}{Solved small bug with `no-header' option}
% \changes{v1.7}{2005/03/31}{Revised documentation}
% \changes{v1.8}{2005/05/07}{Added \texttt{copydocumentclass} option}
% \changes{v1.8}{2005/05/07}{Changed name of option \texttt{no-header} to \texttt{header}}
% \changes{v1.8}{2005/05/07}{Changed name of option \texttt{document-handles} to \texttt{handles}}
% \changes{v1.8}{2005/05/07}{Revised documentation and examples}
%
% \GetFileInfo{extract.sty}
%
% \CheckSum{1013}
%
% \DoNotIndex{\@backslashchar,\@bsphack,\@checkend,\@currenvir,
% \@currenvline,\@doendpe,\@eha,\@ehc,\@empty,\@endpefalse,\@esphack,
% \@expandtwoargs,\@ifundefined,\@ignorefalse,\@latex@error,\do,
% \@makeother,\@namedef,\@ne,\@nil,\@nnil,\@temptokena,\@testopt,
% \active,\advance,\AtBeginDocument,\AtEndDocument,\begin,\begingroup,
% \catcode,\closeout,\csname,\CurrentOption,\DeclareOptionX,\def,
% \dospecials,\edef,\else,\end,\endcsname,\endgroup,\expandafter,\fi,
% \g@addto@macro,\global,\if@endpe,\if@ignore,\ifcat,\ifin@,\ifnum,\ifx,
% \ignorespaces,\immediate,\in@,\input,\jobname,\lccode,\let,\lowercase,
% \m@ne,\NeedsTeXFormat,\newcount,\newif,\newwrite,\noexpand,\on@line,
% \openout,\ProcessOptionsX,\ProvidesPackage,\relax,\RequirePackage,
% \reserved@a,\romannumeral,\string,\the,\write,\XTR@activefalse,
% \XTR@activetrue,\XTR@handlestrue,\XTR@resa,\XTR@tempa,\XTR@tempb,\z@,
% \PackageError,\gdef,\count@,\day,\divide,\month,\multiply,\space,
% \time,\xdef,\year,\@nameuse,\@tempcnta,\@tempcntb,\\,\!,\(,\),
% \define@boolkey,\XTR@sttrue,\XTR@stfalse,\documentclass,\filename@base,
% \filename@ext,\filename@parse,\KV@@sp@def,\XKV@afterelsefi,\XKV@afterfi,
% \XKV@classoptionslist,\XKV@documentclass,\XKV@for@n,\XKV@for@o,
% \XKV@ifstar,\XKV@ifundefined,\XKV@sp@deflist,\XKV@tkey,\chapter}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%\title{The \pf{extract} package
%\thanks{This package can be downloaded from the CTAN mirrors:
%\texttt{/macros/latex/contrib/extract}. See \texttt{extract.dtx} for
%information on installing \pf{extract} into your \LaTeX\
%distribution and for the license of this package.}}
%\author{Hendri Adriaens\\\url{http://stuwww.uvt.nl/~hendri}}
%\date{\fileversion\ (\filedate)}
%\maketitle
%
%\begin{abstract}\noindent
%This package can be used to (conditionally) extract specific
%commands and environments from a source file and write them to a
%target file. This can be done without significant changes to the
%source document, but labels inside the source can be used for more
%flexibility. The package also provides environments to write code
%directly to the target file or use code in both the source and the
%target file. These tools allow one to generate ready-to-run files
%from a source document, containing only the extracted material, in
%an otherwise ordinary \LaTeX\ run.
%\end{abstract}
%
%\tableofcontents
%
%\section{Introduction}\label{sec:intro}
%I created this package when I was working on some lecture notes with
%exercises in the text and wanted to generate an exercises book on
%the fly. David Carlisle put forward the idea to use a technique as
%is now implemented in this package. The package heavily uses the
%\pf{verbatim} package \cite{verbatim} by Rainer Sch\"opf and uses
%the \pf{xkeyval} package \cite{xkeyval} to provide a simple and easy
%interface\footnote{And some tools like \cs{XKV@ifundefined} and
%\cs{XKV@sp@deflist}. See section~\ref{sec:imple} for more
%information.}.
%
%There are other packages around that provide tools for conditionally
%typesetting material or writing material to an external file. Let me list a few.
%\begin{description}
%\item[\pf{askinclude} \cite{askinclude}, \pf{excludeonly} \cite{excludeonly}]\indent\\
%These packages enhance \LaTeX's |\include| and |\includeonly|
%system to select the files that should be included in typesetting.
%\item[\pf{comment} \cite{comment}, \pf{verbatim} \cite{verbatim}, \pf{xcomment} \cite{xcomment}]\indent\\
%The first two packages define the |comment| environment and the
%third the |xcomment| environment. These environments ignore their
%body. But if the command |\xcomment| is used and supplied with a
%list of environments, these environments will be typeset when they
%appear in the body of the |xcomment| environment. The \pf{comment}
%package provides the commands |\includecomment| and
%|\excludecomment| to do a similar job.
%\item[\pf{optional} \cite{optional}, \pf{version} \cite{version}, \pf{versions} \cite{versions}]\indent\\
%These packages define some commands with which you can
%control which material should be typeset.
%\item[\pf{pagesel} \cite{pagesel}, \pf{pdfpages} \cite{pdfpages}, \pf{selectp} \cite{selectp}]\indent\\
%These packages only typeset certain pages.
%\item[\pf{fancyvrb} \cite{fancyvrb}, \pf{listings} \cite{listings}]\indent\\
%These packages (among others) provide tools to write text to an
%external file.
%\end{description}
%
%The \pf{extract} package differs from all these packages since it
%extracts content and leaves the typeset version of the original
%document untouched. Furthermore, for simple extraction jobs, it is
%not necessary to make any changes to the document other than adding
%the |\usepackage| command. This allows for conditional extraction of
%commands and environments based on the number of the command or
%environment counted from the beginning of the document. More
%flexible conditional extraction can be achieved by adding labels to
%the source document. How all of this works will be explained in the
%sections to come.
%
%\section{Environment extraction}\label{sec:envxtr}
%The following provides an example of the user interface of the package.
%\begin{example}
% \usepackage[
%   active,
%   generate=file,
%   extract-env={figure,table}
% ]{extract}
%\end{example}
%\DescribeOptions{active,generate,extract-env}
%If the |active| option is not specified (or set to |false|), the
%package does nothing and no files are generated. If the option is
%specified, the package will redefine the environments |figure| and
%|table| so that they write their bodies (the content of the
%environment) to the file indicated with the |generate| option, here
%|file.tex|\footnote{If a file extension is lacking, \texttt{.tex}
%will be used.}, including the |\begin{figure}| and |\end{figure}|
%commands. Besides that, the environments will be executed as usual.
%Most environments are supported (known exception is the |document|
%environment). When the package encounters |\begin{document}| or
%|\end{document}| in the source file, by default\footnote{See
%section~\ref{sec:misc} for the \texttt{handles}
%option.}, it will also write these commands to the target file, such
%that, if a suitable preamble is added, the file is ready to be run
%by \LaTeX. See listing~\vref{list:env} for an example.\footnote{See
%section~\ref{sec:manual} for the \texttt{extract} environment,
%section~\ref{sec:misc} for the \texttt{copydocumentclass} option and
%section~\ref{sec:sande} how to obtain the example files.}
%\begin{Listing}\caption{Environment extraction.}\label{list:env}
%\begin{minipage}[t]{.49\linewidth}
%\hfill |xtrex1.tex|
%\begin{example}
% \documentclass[10pt]{article}
% \usepackage[
%   active,
%   generate=file,
%   copydocumentclass=false,
%   extract-env=equation
% ]{extract}
% \begin{extract}
% \documentclass[11pt]{article}
% \end{extract}
% \begin{document}
% Some text.
% \begin{equation}
% a^2+b^2=c^2
% \end{equation}
% Some text.
% \begin{equation}
% x^2+y^2=z^2
% \end{equation}
% Some text.
% \end{document}
%\end{example}
%\end{minipage}
%\hfill
%\begin{minipage}[t]{.49\linewidth}
%\hfill |file.tex|
%\begin{example}
% \documentclass[11pt]{article}
%
% \begin{document}
%
% \begin{equation}
% a^2+b^2=c^2
% \end{equation}
%
% \begin{equation}
% x^2+y^2=z^2
% \end{equation}
%
% \end{document}
%\end{example}
%\end{minipage}
%\end{Listing}
%The package will also write a header to the target file with some
%information about the source and time of generation of the target
%file. This header can be turned off with the |no-header| option.
%See also section~\ref{sec:misc}.
%
%\section{Command extraction}\label{sec:cmdxtr}
%\DescribeOptions{extract-cmd,extract-cmdline}
%This package can also extract commands. It supplies two methods to
%do this. See the example below.
%\begin{example}
% \usepackage[
%   active,
%   generate=file,
%   extract-cmd=section,
%   extract-cmdline=label
% ]{extract}
%\end{example}
%The first method (accessed with the |extract-cmd| option) is based
%on the particular syntax of the command and hence only supports
%particular commands. It will read it arguments and write them,
%together with the original command, to the target file. Besides
%that, the command will be executed as usual. Currently, the commands
%|\chapter|, |\section|, |\subsection|, and |\subsubsection| in the
%standard \LaTeX\ classes (and classes or packages derived from
%those) are supported. An optional argument to these commands, like
%|\chapter[short]{long}| is supported. However, the starred version
%|\chapter*{title}| will not be written to the file due to technical
%limitations. Sections~\ref{sec:how} and~\ref{sec:imple} will explain
%this in more detail.
%
%The second method (accessed with the |extract-cmdline| option) will
%redefine commands to write themselves and all the text following on
%the same line to the target file and will also execute the entire
%line as with an ordinary \LaTeX\ run. This allows to redefine any
%command that is not supported by the first method, but should be
%applied with care. If the command is used internally in a class or
%style file, your document might fail to run. In particular, one
%should not redefine one of the commands supported by the first
%method with this method. See listing~\vref{list:cmd} for a working
%example of both methods.\footnote{See section~\ref{sec:manual} for
%the \texttt{extract*} environment.}
%\begin{Listing}\caption{Command extraction.}\label{list:cmd}
%\begin{minipage}[t]{.49\linewidth}
%\hfill |xtrex2.tex|
%\begin{example}
% \documentclass{article}
% \usepackage[
%   active,
%   generate=file,
%   extract-env=exercise,
%   extract-cmd=section,
%   extract-cmdline=label
% ]{extract}
% \begin{extract*}
% \newtheorem{exercise}{Exercise}
% \end{extract*}
% \begin{document}
% \section{Theory}
% \label{sec:1}
% Some text.
% \section{Exercises}
% \begin{exercise}
% Use the results from section
% \ref{sec:1} to show that\dots
% \end{exercise}
% Some text.
% \end{document}
%\end{example}
%\end{minipage}
%\hfill
%\begin{minipage}[t]{.49\linewidth}
%\hfill |file.tex|
%\begin{example}
% \documentclass{article}
% \newtheorem{exercise}{Exercise}
%
% \begin{document}
%
% \section{Theory}
%
% \label{sec:1}
%
% \section{Exercises}
%
% \begin{exercise}
% Use the results from section
% \ref{sec:1} to show that\dots
% \end{exercise}
%
% \end{document}
%\end{example}
%\end{minipage}
%\end{Listing}
%
%\section{Conditional extraction}\label{sec:conditional}
%\subsection{Extraction with numbers}
%\DescribeOptions{-nrs}
%It is also possible to conditionally extract environments and
%commands. After the options |extract-env|, |extract-cmd| and
%|exstract-cmdline| are used, for each environment or command
%specified there, there will be a new option with the name of that
%environment or command and the |-nrs| postfix. This option can take
%a comma separated list in which you can specify which environments
%or macros (counting from the |\usepackage{extract}|
%command\footnote{Notice that starred commands like \cs{chapter*},
%which won't be redefined by the \texttt{extract-cmd} option, will
%also not be counted.}) should be extracted. Table~\vref{tab:syntax} lists
%the syntax that can be used in the comma separated list. The term
%`item' is used for `command or environment'.
%\begin{table}[htb]\centering
%\begin{tabular}{cp{8.5cm}}
%Syntax&Meaning\\\hline
%$\meta{x}$&Item $\meta{x}$ will be extracted.\\
%-$\meta{x}$&All items up to and including item $\meta{x}$ will be extracted.\\
%$\meta{x}$-&All items from and including item $\meta{x}$ will be extracted.\\
%$\meta{x}$-$\meta{y}$&All items in between $\meta{x}$ and $\meta{y}$, including
%$\meta{x}$ and $\meta{y}$ will be extracted.
%\end{tabular}
%\caption{Syntax for conditional extraction with numbers.}\label{tab:syntax}
%\end{table}
%See an example in the listing below.
%\begin{example}
% \documentclass{book}
% \usepackage[
%   active,
%   generate=file,
%   extract-env={figure,table},
%   figure-nrs={-2,4-},
%   extract-cmd=chapter,
%   chapter-nrs={3-5,7}
% ]{extract}
% \begin{document}
% ...
% \end{document}
%\end{example}
%This example, when completed with content, will extract all |table|
%environments, |figure| environments 1, 2, and all figures from (and
%including) figure 4. It will also extract chapter commands 3 to 5 and 7.
%
%\subsection{Extraction with labels}
%\DescribeOptions{-labels}
%Conditional extraction is also possible with labels. The advantage of
%using labels is that output does not change (in comparison to using
%numbers) when commands or environments are added. The drawback is
%that one needs to modify the source document and add labels in the
%text.
%
%\DescribeMacro{\extractionlabel}
%Labels are declared with the following command.
%\begin{command}
% `\cs{extractionlabel}\marg{name}'
%\end{command}
%A label should be declared just before the command or environment
%that you want to extract. For instance
%\begin{example}
% \extractionlabel{exer-a}
% \begin{exercise}
% ...
% \end{exercise}
%\end{example}
%You can reuse the same label multiple times and you can specify
%which items should be extracted by the options with a |-labels|
%prefix. This works in the same way as with numbers.
%\begin{example}
% \documentclass{book}
% \usepackage[
%   active,
%   generate=file,
%   extract-env=exercise,
%   exercise-labels={exer-a,exer-c}
% ]{extract}
% \begin{document}
% ...
% \end{document}
%\end{example}
%This example will only extract exercises that have been preceded by
%the declaration |\extractionlabel{exer-a}| or
%|\extractionlabel{exer-c}|.
%
%When using both conditional extraction with numbers and with labels,
%the command or environment at hand will be extracted when at least
%on of the conditions is true. Find an example in listing~\vref{list:cond}.
%\begin{Listing}\caption{Conditional extraction.}\label{list:cond}
%\begin{minipage}[t]{.49\linewidth}
%\hfill |xtrex3.tex|
%\begin{example}
% \documentclass{article}
% \usepackage[
%   active,
%   generate=file,
%   extract-env=figure,
%   figure-nrs={1,3},
%   figure-labels={fig-a,fig-b}
% ]{extract}
% \begin{document}
% Some text.
% \begin{figure}
% Figure 1.
% \end{figure}
% Some text.
% \extractionlabel{fig-a}
% \begin{figure}
% Figure 2.
% \end{figure}
% Some text.
% \extractionlabel{fig-b}
% \begin{figure}
% Figure 3.
% \end{figure}
% Some text.
% \extractionlabel{fig-c}
% \begin{figure}
% Figure 4.
% \end{figure}
% \end{document}
%\end{example}
%\end{minipage}
%\hfill
%\begin{minipage}[t]{.49\linewidth}
%\hfill |file.tex|
%\begin{example}
% \documentclass{article}
%
% \begin{document}
%
% \begin{figure}
% Figure 1.
% \end{figure}
%
% \begin{figure}
% Figure 2.
% \end{figure}
%
% \begin{figure}
% Figure 3.
% \end{figure}
%
% \end{document}
%\end{example}
%\end{minipage}
%\end{Listing}
%
%\section{Extraction tools}\label{sec:tools}
%\subsection{Manual extraction}\label{sec:manual}
%\DescribeEnv{extract}
%The package provides the environment |extract|.
%\begin{command}
% `\cs{begin\char`\{extract\char`\}}'
% `\meta{body}'
% `\cs{end\char`\{extract\char`\}}'
%\end{command}
%This environment writes its body only to the target file. This can
%be used to generate a preamble in the target file so that you can
%run the generated file through \LaTeX\ immediately after creation.
%See the example in listing~\vref{list:env} and the example below.
%
%\DescribeEnv{extract*}
%There also exists a starred version of the |extract| environment.
%\begin{command}
% `\cs{begin\char`\{extract*\char`\}}'
% `\meta{body}'
% `\cs{end\char`\{extract*\char`\}}'
%\end{command}
%This environment will not only write the body to the target file,
%but will also execute the code in the source document. This can be
%used to create a common preamble which holds packages and commands
%that will be used in both the source and the target file. See
%listing~\vref{list:cmd} and listing~\vref{list:xtrenv} for examples.
%Notice that the `answer' will not appear in |xtrex4.dvi|.
%\begin{Listing}\caption{Extract environments.}\label{list:xtrenv}
%\begin{minipage}[t]{.49\linewidth}
%\hfill |xtrex4.tex|
%\begin{example}
% \documentclass{article}
% \usepackage[
%   active,
%   generate=file,
%   extract-env=equation*
% ]{extract}
% \begin{extract*}
% \usepackage{amsmath}
% \end{extract*}
% \begin{document}
% Some text.
% \begin{equation*}
% x^2+y^2=z^2
% \end{equation*}
% \begin{extract}
% $x=3$, $y=4$ and $z=5$
% satisfy this equation.
% \end{extract}
% \end{document}
%\end{example}
%\end{minipage}
%\hfill
%\begin{minipage}[t]{.49\linewidth}
%\hfill |file.tex|
%\begin{example}
% \documentclass{article}
% \usepackage{amsmath}
%
% \begin{document}
%
% \begin{equation*}
% x^2+y^2=z^2
% \end{equation*}
% $x=3$, $y=4$ and $z=5$
% satisfy this equation.
%
% \end{document}
%\end{example}
%\end{minipage}
%\end{Listing}
%
%\DescribeMacros{\extractline,\extractline*}
%The package also provides a command that extracts the current line.
%\begin{command}
% `\cs{extractline}'
% `\cs{extractline*}'
%\end{command}
%The starred version also executes the code at that line. See the
%example below.
%\begin{example}
% \extractline This line should be extracted.
% \extractline*This line should be extracted and executed.
%\end{example}
%Notice that a space is following the command |\extractline| and that no space is
%following the command |\extractline*|. In the first line, \LaTeX\ will eat this
%space, but it won't do that in the second line. If we add a space
%there in between the |*| and |This|, this space will be executed and
%extracted as well.
%
%\DescribeMacros{\extractline,\extractline*}
%\DescribeEnvs{extract,extract*}
%The |extract| and |extract*| environments and the macros
%|\extractionlabel| and |\extractionlabel*| have an optional argument
%for specifying the label directly.
%\begin{command}
% `\cs{begin\char`\{extract\char`\}}\oarg{name}'
% `\cs{begin\char`\{extract*\char`\}}\oarg{name}'
% `\cs{extractline}\oarg{name}'
% `\cs{extractline*}\oarg{name}'
%\end{command}
%\newpage
%\DescribeOptions{extract-nrs,line-nrs,extract-labels,line-labels}
%The options |extract-nrs| and |line-nrs| can be used to
%control conditional extraction of these environments and commands
%using numbers. Moreover, one can do conditional extractions with
%labels for these commands and environments as well. Use the options
%|extract-labels| and |line-labels| for that purpose. See also
%section~\ref{sec:conditional} for information about conditional extraction.
%
%See an example in listing~\vref{list:optlab}.
%This example will demonstrate that only certain lines and environments
%are extracted. Note that, when running |xtrex5.tex| with \LaTeX,
%the output, |xtrex5.dvi|, will contain the following.
%\begin{example}
% line 2
% line 4
% line 5
%\end{example}
%\begin{Listing}\caption{Optional labels.}\label{list:optlab}
%\begin{minipage}[t]{.49\linewidth}
%\hfill |xtrex5.tex|
%\begin{example}
% \documentclass{article}
% \usepackage[
%   active,
%   generate=file,
%   copydocumentclass=false,
%   extract-labels=type-a,
%   line-labels={type-a,type-c},
%   line-nrs=3
% ]{extract}
% \begin{extract}[type-a]
% \documentclass{article}
% \end{extract}
% \begin{extract}[type-b]
% \documentclass{book}
% \end{extract}
% \begin{document}
% \parindent0pt
% \extractline[type-a]line 1\\
% \extractline*line 2\\
% \extractline line 3\\
% \extractline*[type-a]line 4\\
% \extractline*[type-c]line 5\\
% \extractline line 6\\
% \end{document}
%\end{example}
%\end{minipage}
%\hfill
%\begin{minipage}[t]{.49\linewidth}
%\hfill |file.tex|
%\begin{example}
% \documentclass{article}
%
% \begin{document}
% line 1\\
% line 3\\
% line 4\\
% line 5\\
%
% \end{document}
%\end{example}
%\end{minipage}
%\end{Listing}
%
%\subsection{Skipping extraction}
%\DescribeEnv{extractskip}
%The package also provides the |extractskip| environment.
%\begin{command}
% `\cs{begin\char`\{extractskip\char`\}}\oarg{name}'
% `\meta{body}'
% `\cs{end\char`\{extractskip\char`\}}'
%\end{command}
%The body of this environment will not be written to the target file,
%but will be executed. This environment can be used to skip material
%in an environment which extracts its entire body. This could be the
%|extract| environment, but also an environment that has been
%redefined to be extracted using the |extract-env| option. The argument
%\meta{name} is optional and contains the label.
%
%\DescribeOptions{extractskip-nrs,extractskip-labels}
%This environment can also operate conditionally as has been
%described in section~\ref{sec:conditional} using the options
%|extractskip-nrs| and |extractskip-labels|. Listings~\vref{list:skip1}
%and~\vref{list:skip2} will demonstrate this environment.
%\begin{Listing}\caption{Skipping extraction.}\label{list:skip1}
%\begin{minipage}[t]{.49\linewidth}
%\hfill |xtrex6.tex|
%\begin{example}
% \documentclass{article}
% \usepackage[
%   active,
%   generate=file,
%   extract-env=figure
% ]{extract}
% \begin{document}
% \begin{figure}[!h]
% \begin{extractskip}
% \fbox{figure 1}
% \end{extractskip}
% \fbox{figure 2}
% \end{figure}
% \begin{extract*}
% \begin{itemize}
% \item 1
% \item 2
% a\begin{extractskip}b
% \item 3
% c\end{extractskip}d
% \item 4
% \begin{extractskip}
% \item 5
% \end{extractskip}
% \end{itemize}
% \end{extract*}
% \end{document}
%\end{example}
%\end{minipage}
%\hfill
%\begin{minipage}[t]{.49\linewidth}
%\hfill |file.tex|
%\begin{example}
% \documentclass{article}
%
% \begin{document}
%
% \begin{figure}[!h]
% \fbox{figure 2}
% \end{figure}
% \begin{itemize}
% \item 1
% \item 2
% a
% d
% \item 4
% \end{itemize}
%
% \end{document}
%\end{example}
%\end{minipage}
%\end{Listing}
%\begin{Listing}\caption{Skipping extraction conditionally.}\label{list:skip2}
%\begin{minipage}[t]{.49\linewidth}
%\hfill |xtrex7.tex|
%\begin{example}
% \documentclass{article}
% \usepackage[
%   active,
%   generate=file,
%   extractskip-labels=skipb
% ]{extract}
% \begin{document}
% \begin{extract*}
% \begin{itemize}
% \begin{extractskip}[skipa]
% \item 1
% \end{extractskip}
% \begin{extractskip}[skipb]
% \item 2
% \end{extractskip}
% \begin{extractskip}[skipc]
% \item 3
% \end{extractskip}
% \end{itemize}
% \end{extract*}
% \end{document}
%\end{example}
%\end{minipage}
%\hfill
%\begin{minipage}[t]{.49\linewidth}
%\hfill |file.tex|
%\begin{example}
% \documentclass{article}
%
% \begin{document}
% \begin{itemize}
% \item 1
% \item 3
% \end{itemize}
%
% \end{document}
%\end{example}
%\end{minipage}
%\end{Listing}
%
%\subsection{Miscellaneous options}\label{sec:misc}
%\DescribeOption{header} When setting the |header| option to
%|false|, no header will be written to the target file. If the option
%is not specified or set to |true|, the package will write a header
%to the target file including information on when the target file was
%generated and which source file was used.
%
%\DescribeOption{handles}
%This option controls whether the package will write
%|\begin{document}| and |\end{document}| to the target file when it
%encounters these commands in the source document. By default, this
%option is set to |true|. When the option is set to |false|, the
%generated file can be |\input|ed or |\include|d by another file
%immediately after production.
%
%\DescribeOption{copydocumentclass}
%This option control whether the target file should get the same
%|\documentclass| command as the source document (including class
%options). If set to |false|, you should specify another document class
%for the target file, for instance using the |extract| environment.
%See listing~\vref{list:env} for an example.
%
%\section{How it works, limitations}\label{sec:how}
%The package works as follows. When an environment is asked to be
%extracted, the package will first make a backup of the environment
%with the |XTR| prefix, for instance, |XTRequation*|. After that, the
%package will redefine the environment to read the lines of its body
%verbatim (without executing), parse the lines to locate
%|extractskip| environments and write all lines to a temporary file
%and the lines that are not in an |extractskip| environment to a
%temporary file. After the environment is finished, the temporary
%file, containing for instance
%\begin{example}
% \begin{XTRequation*}
% x^2+y^2=z^2
% \end{XTRequation*}
%\end{example}
%will be inserted in the source document using |\input|. This works,
%at least in theory, with most environments. Notice that this method
%does require a change to the |\begin| and |\end| macros provided by
%the \LaTeX\ kernel \cite{LaTeXbase}. I had to add a hook to these
%commands to be able to collect code to be executed after the current
%environment is ended. In particular, this package will use those
%hooks to |\input| the original code after finishing the current
%group. See section~\ref{sec:imple} for more details.
%
%Commands require a different approach. As a command can have a very
%specific argument structure, redefining commands safely, without
%distorting its original behavior, is not possible in general. I have
%chosen to support the most basic document structure commands as
%provided by standard \LaTeX\ classes, like \pf{book} and
%\pf{article}. Extraction of commands from other classes, like
%provided by the \pf{koma-script} bundle, might work, but there is no
%guarantee.
%
%To be more precise: when requested, the macros |\@chapter| and
%|\@sect| will be redefined. These commands are at the basis of all
%chapter and section commands. When \pf{extract} redefines one of
%these commands, it first makes a backup, like |\XTR@chapter|. After
%that, it will redefine the original command to read its argument(s),
%export them to the target file and execute the backup with the
%proper arguments.
%
%An alternative to this, rather restricted method is the method
%accessed by the |extract-cmdline| option which redefines the
%commands listed there to read the entire line of text, write that to
%the target file and afterwards execute it with a backup copy of the
%original command. This methods too has its drawbacks though as you
%can't redefine commands that are used internally in other macros.
%
%More details on the package code can be found in
%section~\ref{sec:imple}.
%
%\section{Source and examples}\label{sec:sande}
%To generate this documentation, find the source of this
%package, |extract.dtx| in your local \LaTeX\ installation or on CTAN
%and perform the following steps.
%\begin{example}
% latex extract.dtx
% latex extract.dtx
% bibtex extract
% makeindex -s gglo.ist -o extract.gls extract.glo
% makeindex -s gind.ist -o extract.ind extract.idx
% latex extract.dtx
% latex extract.dtx
%\end{example}
%
%If you only want to produce the package and example files from the
%source, then the first step is sufficient. This step will generate
%the package file |extract.sty| and the example files |xtrex1.tex|,
%|xtrex2.tex|, |xtrex3.tex|, |xtrex4.tex|, |xtrex5.tex|, |xtrex6.tex|
%and |xtrex7.tex|.
%
%\StopEventually{}
%
% \section{Implementation}\label{sec:imple}
% Initializations.
%    \begin{macrocode}
%<*extract>
\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
%    \end{macrocode}
% \begin{macro}{\XTR@err}
% \marg{text}\\
% Error macro.
%    \begin{macrocode}
\def\XTR@err#1{\PackageError{extract}{#1}\@ehc}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@namelet}
% \marg{cmd1}\marg{cmd2}\\
% Version of |\let| for two command sequence names.
%    \begin{macrocode}
\def\XTR@namelet#1#2{%
  \expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname
}
%    \end{macrocode}
% \end{macro}
% Options section, powered by \pf{xkeyval}.
% \DescribeOption{active}
% Control extraction with one switch.
%    \begin{macrocode}
\define@boolkey[XTR]{extract.sty}[XTR@]{active}[true]{}
%    \end{macrocode}
% \DescribeOption{header}
% Do not create a header in the target file.
%    \begin{macrocode}
\define@boolkey[XTR]{extract.sty}[XTR@]{header}[true]{}
%    \end{macrocode}
% \DescribeOption{handles}
% Extract |\begin{document}| and |\end{document}| or not.
%    \begin{macrocode}
\define@boolkey[XTR]{extract.sty}[XTR@]{handles}[true]{}
%    \end{macrocode}
% \DescribeOption{copydocumentclass}
% Copy the |\documentclass| command to the target file.
%    \begin{macrocode}
\define@boolkey[XTR]{extract.sty}[XTR@]{copydocumentclass}[true]{}
%    \end{macrocode}
% \DescribeOption{generate}
% Entry point for the target file name.
%    \begin{macrocode}
\DeclareOptionX[XTR]{generate}{\lowercase{\def\XTR@file{#1}}}
%    \end{macrocode}
% \DescribeOption{extract-env}
% Environments that should be extracted.
%    \begin{macrocode}
\DeclareOptionX[XTR]{extract-env}{%
  \def\XTR@envs{#1}%
  \XKV@for@n{#1}\XTR@tempa\XTR@tempb
}
%    \end{macrocode}
% \DescribeOption{extract-cmd}
% Commands that should be extracted with the `arguments method'.
%    \begin{macrocode}
\DeclareOptionX[XTR]{extract-cmd}{%
  \def\XTR@cmdsargs{#1}%
  \XKV@for@n{#1}\XTR@tempa\XTR@tempb
}
%    \end{macrocode}
% \DescribeOption{extract-cmdline}
% Commands that should be extracted with the `line method'.
%    \begin{macrocode}
\DeclareOptionX[XTR]{extract-cmdline}{%
  \def\XTR@cmdsline{#1}%
  \XKV@for@n{#1}\XTR@tempa\XTR@tempb
}
\def\XTR@tempb{%
%    \end{macrocode}
% \DescribeOptions{-nrs,-labels}
% For each environment or command provide new package options that
% save the argument to a list cleared from redundant spaces. The
% |-nrs| options also create a `counter' for counting the commands or
% environments. The lists and counters will be used for conditional
% extraction. Note that |\XTR@tkey| contains the key name inside
% the option macro (due to the use of |\ProcessOptionsXi|).
%    \begin{macrocode}
  \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}%
  }%
}
%    \end{macrocode}
% \DescribeOptions{line-nrs,line-labels}
% Generate options for |\extractline| commands.
%    \begin{macrocode}
\def\XTR@tempa{line}\XTR@tempb
%    \end{macrocode}
% \DescribeOptions{extract-nrs,extract-labels}
% Generate options for |extract| environments.
%    \begin{macrocode}
\def\XTR@tempa{extract}\XTR@tempb
%    \end{macrocode}
% \DescribeOptions{extractskip-nrs,extractskip-labels}
% Generate options for |extractskip| environments.
%    \begin{macrocode}
\def\XTR@tempa{extractskip}\XTR@tempb
%    \end{macrocode}
% Generate an error for unknown options.
%    \begin{macrocode}
\DeclareOptionX*{\XTR@err{Unknown option `\CurrentOption'}}
%    \end{macrocode}
% Initialize options.
%    \begin{macrocode}
\ExecuteOptionsX[XTR]{header=true,handles=true,copydocumentclass=true}
%    \end{macrocode}
% Process options.
%    \begin{macrocode}
\ProcessOptionsX[XTR]
%    \end{macrocode}
% \begin{macro}{\XTR@opentmp}
% \begin{macro}{\XTR@writetmp}
% \begin{macro}{\XTR@closetmp}
% \begin{macro}{\XTR@writeout}
% Shortcut macros for much used command sequences.
%    \begin{macrocode}
\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}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% Perform some checks on the input. Notice the use of |\XKV@ifundefined|
% which is equal to |\@ifundefined| if no $\varepsilon$-\TeX\ engine
% is available and which uses |\ifcsname| when it is. In the latter
% case, testing whether commands are defined does not create an
% entry in \TeX's hash table.
%    \begin{macrocode}
\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
%    \end{macrocode}
% \begin{macro}{\@envdepth}
% Counter for depth of environments.
%    \begin{macrocode}
\newcount\@envdepth\@envdepth\z@
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\begin}
% \marg{environment}\\
% Modify the macro |\begin| to allow adding code to a level specific
% hook which can be executed after |\endgroup| in |\end|. See for
% more info on this macro the \LaTeX\ source~\cite{LaTeXbase}.
%    \begin{macrocode}
\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
%    \end{macrocode}
% Advance depth level.
%    \begin{macrocode}
  \global\advance\@envdepth\@ne
%    \end{macrocode}
% Initialize the hook for this level.
%    \begin{macrocode}
  \global\@namedef{@afterendenvhook@\romannumeral\@envdepth}{}%
  \reserved@a}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\end}
% \marg{environment}\\
% Modify |\end| to execute the code collected in the hook.
%    \begin{macrocode}
\def\end#1{%
  \csname end#1\endcsname\@checkend{#1}%
  \expandafter\endgroup\if@endpe\@doendpe\fi
%    \end{macrocode}
% Copy current hook code to a temporary macro.
%    \begin{macrocode}
  \expandafter\let\expandafter\reserved@a
    \csname @afterendenvhook@\romannumeral\@envdepth\endcsname
%    \end{macrocode}
% Decrease the depth.
%    \begin{macrocode}
  \global\advance\@envdepth\m@ne
%    \end{macrocode}
% Execute the hook of the current environment. This is done after
% decreasing the depth as to avoid level mixing problems when the
% hook contains another environment. This environment has to be
% executed at the same level as the environment in which the hook was
% defined since it is executed after the group and does not below
% anymore to the environment in which the hook was defined.
%    \begin{macrocode}
  \reserved@a\relax
  \if@ignore\@ignorefalse\ignorespaces\fi}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\AfterEndEnv}
% Adds code to the macros |\@afterendenvhook@i|, |ii|, etc. which will
% be executed after the group of the current environment.
%    \begin{macrocode}
\def\AfterEndEnv{%
  \expandafter\g@addto@macro
    \csname @afterendenvhook@\romannumeral\@envdepth\endcsname
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@checkxtr}
% \marg{type}\marg{item}\\
% Checks whether a certain environment or command should be extracted or skipped.
% \meta{type} is the type of check: for extraction or for skipping
% content. \meta{item} is the name of a command or an environment.
%    \begin{macrocode}
\def\XTR@checkxtr#1#2{%
  \@nameuse{XTR@#1false}%
  \XTR@namelet{XTR@maketrue}{XTR@#1true}%
%    \end{macrocode}
% First check whether, for this macro or environment, some method of
% conditional extraction is used. If not, just extract.
%    \begin{macrocode}
  \XKV@ifundefined{XTR@#2-nrs}{%
    \XKV@ifundefined{XTR@#2-labels}\XTR@maketrue{}%
  }{%
%    \end{macrocode}
% Advance the `counter'.
%    \begin{macrocode}
    \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}{}{%
%    \end{macrocode}
% If the current label is in the list for extraction, extract it.
%    \begin{macrocode}
      \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}{}{%
%    \end{macrocode}
% If the current command or environment number is in the list, extract it.
%    \begin{macrocode}
      \expandafter\XTR@ch@ckxtr\csname XTR@#2-nrs\expandafter
        \endcsname\csname XTR@#2-nrs@cnt\endcsname
    }%
  \fi
%    \end{macrocode}
% Redefine |\XTR@currentlabel| to avoid extracting all following
% environments of this type.
%    \begin{macrocode}
  \global\let\XTR@currentlabel\relax
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@ch@ckxtr}
% \meta{list}\meta{counter}\\
% Parse the \meta{list} of numbers and compare each item with the
%\meta{counter} holding the number of the current item.
%    \begin{macrocode}
\def\XTR@ch@ckxtr#1#2{%
  \XKV@for@o#1\XTR@resa{\expandafter\XTR@ch@ck@tr\XTR@resa--\@nil#2}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@ch@ck@tr}
% \meta{x}|-|\meta{y}|-|\meta{z}|\@nil|\meta{counter}\\
% Parse an element of the list. Basically, decide whether we have |x|,
% |x-y|, |x-| or |-y| and act accordingly.
%    \begin{macrocode}
\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
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\extractionlabel}
% \changes{v1.3}{2005/01/12}{Was \cs{extractlabel}}
% \begin{macro}{\XTR@currentlabel}
% |\extractionlabel| saves its argument (after removing redundant spaces).
% This label will be used for conditional extraction. |\XTR@currentlabel|
% is initialized.
%    \begin{macrocode}
\def\extractionlabel{\KV@@sp@def\XTR@currentlabel}
\let\XTR@currentlabel\relax
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\extract}
% \begin{macro}{\extract*}
% Define environments that write verbatim to the target file. The
% starred version also executes the code by writing it to a temp file
% and inputting it |\AfterEndEnv|, just as with redefining existing
% environments. When the package is inactive, |extract| is equivalent
% to the |comment| environment and |extract*| takes its body out of the
% group and executes it hence acting as if |\begin{extract*}| and
% |\end{extract*}| were never typed.
%    \begin{macrocode}
\def\extract{\XTR@stfalse\XTR@extract}
\@namedef{extract*}{\XTR@sttrue\XTR@extract}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\XTR@extract}
% Prepare verbatim reading and check for an optional argument.
%    \begin{macrocode}
\def\XTR@extract{%
  \@bsphack
  \let\do\@makeother\dospecials\catcode`\^^M\active
  \@testopt\XTR@@xtract\@nil
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@@xtract}
% \oarg{label}\\
% Process the optional label, define line processing and start reading
% verbatim. Do not extract when the package is not active. Use a
% temporary file to extract the body to in case this needs to be
% executed in the source document (|extract*| environment).
%    \begin{macrocode}
\def\XTR@@xtract[#1]{%
%    \end{macrocode}
% Check state.
%    \begin{macrocode}
  \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
}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\begingroup
  \lccode`\!=`\\ \lccode`\(=`\{ \lccode`\)=`\}
\lowercase{\endgroup
%    \end{macrocode}
% \begin{macro}{\XTR@processline@begin}
% This macro starts the reparsing of a line read by \pf{verbatim}. It
% is possible to have |\begin{extractskip}| and |\end{extractskip}|
% on the same line.
%    \begin{macrocode}
\def\XTR@processline@begin{%
%    \end{macrocode}
% Initialize |\@temptokena| (used for temp file) and |\verbatim@line|
% (used for output file). Save the original content of |\verbatim@line|
% for later use.
%    \begin{macrocode}
  \@temptokena{}%
  \edef\XTR@orig@line{\the\verbatim@line}%
  \verbatim@line{}%
  \expandafter\XTR@testbegin\XTR@orig@line!begin(extractskip)\@nil
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@testbegin}
% \meta{text1}|\begin{extractskip}|\meta{text2}|\@nil|\\
% Checks whether |\begin{extractskip}| occurs.
%    \begin{macrocode}
\def\XTR@testbegin#1!begin(extractskip)#2\@nil{%
  \@temptokena\expandafter{\the\@temptokena#1}%
  \verbatim@line\expandafter{\the\verbatim@line#1}%
  \def\XTR@tempa{#2}%
%    \end{macrocode}
% If \meta{text2} empty, there is no |\begin{extractskip}|. Just write the
% content to file.
%    \begin{macrocode}
  \ifx\XTR@tempa\@empty\XTR@processline@write\else\XKV@afterfi
%    \end{macrocode}
% Check the label.
%    \begin{macrocode}
    \XTR@skiplabel#2[]\@nil
%    \end{macrocode}
% Should we skip the |extractskip| environment or not?
%    \begin{macrocode}
    \XTR@checkxtr{skip}{extractskip}%
%    \end{macrocode}
% Switch to scanning for |\end{extractskip}| in the next line.
%    \begin{macrocode}
    \let\verbatim@processline\XTR@processline@end
%    \end{macrocode}
% Remove some stuff that we added and continue scanning for |\end{extractskip}|
% on the current line.
%    \begin{macrocode}
    \ifx\XTR@tempa\@nnil\XKV@afterelsefi
      \XTR@t@stbegin#2\@nil
    \else\XKV@afterfi
      \expandafter\XTR@t@stbegin\XTR@tempa\@nil
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@skiplabel}
% \meta{text1}\oarg{label}\meta{text2}|\@nil|\\
% This macro checks whether a label is present and sets |\XTR@currentlabel|
% if necessary.
%    \begin{macrocode}
\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
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@sk@plabel}
% \meta{text}|[]\@nil|\\
% Remove extra brackets from input.
%    \begin{macrocode}
\def\XTR@sk@plabel#1[]\@nil{\def\XTR@tempa{#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@t@stbegin}
% \meta{text}|\begin{extractskip}\@nil|\\
% Remove the extra |\begin{extractskip}| and start scanning for
% |\end{extractskip}| in the current line.
%    \begin{macrocode}
\def\XTR@t@stbegin#1!begin(extractskip)\@nil{%
  \XTR@testend#1!end(extractskip)\@nil
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@processline@end}
% Starts scanning for |\end{extractskip}| in case this was not on one
% line together with |\begin{extractskip}|. Comparable to |\XTR@processline@begin|.
%    \begin{macrocode}
\def\XTR@processline@end{%
  \@temptokena{}%
  \edef\XTR@orig@line{\the\verbatim@line}%
  \verbatim@line{}%
  \expandafter\XTR@testend\XTR@orig@line!end(extractskip)\@nil
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@testend}
% \meta{text1}|\end{extractskip}|\meta{text2}|\@nil|\\
% Check whether |\end{extractskip}| occurs in the line.
%    \begin{macrocode}
\def\XTR@testend#1!end(extractskip)#2\@nil{%
  \@temptokena\expandafter{\the\@temptokena#1}%
%    \end{macrocode}
% Skip material conditionally on labels or numbers.
%    \begin{macrocode}
  \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
%    \end{macrocode}
% Switch to scanning for |\begin{extractskip}| in the next line.
%    \begin{macrocode}
    \let\verbatim@processline\XTR@processline@begin
%    \end{macrocode}
% Continue scanning for |\begin{extractskip}| in the current line.
%    \begin{macrocode}
    \XTR@t@stend#2\@nil
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@t@stend}
% \meta{text}|\end{extractskip}\@nil|\\
% Remove the redundant |\end{extractskip}| and continue scanning for
% for the string |\begin{extractskip}| in this line.
%    \begin{macrocode}
\def\XTR@t@stend#1!end(extractskip)\@nil{%
  \XTR@testbegin#1!begin(extractskip)\@nil
}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@processline@write}
% \changes{v1.8}{2005/05/07}{Solved bug of not copying empty lines to the target file}
% Writes the material to the appropriate file. If one of the tokens
% has become empty, it might be because the line was empty originally
% or because the parsing and removal of |\begin{extractskip}| and
% |\end{extractskip}| made it empty. In the latter case, do not write
% the empty line. In the former case, do write it.
%    \begin{macrocode}
\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
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\endextract}
% \begin{macro}{\endextract*}
% Stop reading verbatim and if necessary execute the body of the
% environment after the |extract*| environment.
%    \begin{macrocode}
\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
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\extractskip}
% \begin{macro}{\endextractskip}
% The |extractskip| environment when it is not used inside an environment
% that is redefined to be extracted. Hence this environment makes itself
% disappear, just as |extract*|, but doesn't write to the output file.
% The trick with |XTR@activefalse| will remain local.
%    \begin{macrocode}
\@namedef{extractskip}{\XTR@activefalse\@nameuse{extract*}}
\XTR@namelet{endextractskip}{endextract*}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\extractline}
% This macro extracts all text after the macro and at the same line.
% First we check for an optional star.
%    \begin{macrocode}
\def\extractline{%
  \XKV@ifstar{\XTR@sttrue\XTR@extractline}%
    {\XTR@stfalse\XTR@extractline}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@extractline}
% Start the group and reset all catcodes for verbatim reading.
%    \begin{macrocode}
\def\XTR@extractline{%
  \begingroup
    \let\do\@makeother\dospecials\catcode`\^^M\active
%    \end{macrocode}
% Test for an optional argument. Note that, due to reset catcodes,
% macros won't work in the optional argument, but that is not a real
% restriction, while it saves some tokens and memory. If we want to
% allow for macro arguments, we need an extra macro for the check.
%    \begin{macrocode}
    \@testopt\XTR@@xtractline\@nil
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@@xtractline}
% \oarg{label}\meta{text}\meta{eol}\\
% The workhorse that reads input until the end of the line. Use the
% |\lowercase| trick for the definition.
%    \begin{macrocode}
\begingroup
  \catcode`\~=\active\lccode`\~=`\^^M
\lowercase{\endgroup
  \def\XTR@@xtractline[#1]#2~{%
%    \end{macrocode}
% Check state.
%    \begin{macrocode}
      \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
%    \end{macrocode}
% If we need to execute the line, the catcodes are wrong, so write it
% to the temporary file and insert it again when the catcodes are
% reset by |\endgroup|.
%    \begin{macrocode}
      \ifXTR@st\XTR@opentmp\XTR@writetmp{#2}\XTR@closetmp\fi
    \endgroup
%    \end{macrocode}
% Insert original content.
%    \begin{macrocode}
    \ifXTR@st
      \input{\jobname.xtr}%
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
% Only define the following macros when the package is active. This
% branch also performs redefinitions of the macros and environments
% that should be extracted.
%    \begin{macrocode}
\ifXTR@active
%    \end{macrocode}
% Start writing the target file.
%    \begin{macrocode}
\immediate\openout\XTR@out\XTR@file\relax
%    \end{macrocode}
% Write header to the target file.
%    \begin{macrocode}
\ifXTR@header
%    \end{macrocode}
% Compute the time.
%    \begin{macrocode}
  \@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
%    \end{macrocode}
% Save the \% character.
%    \begin{macrocode}
    \catcode`\%=12
    \gdef\XTR@tempa{%%\space}
  \endgroup
%    \end{macrocode}
% Write all information to the target file.
%    \begin{macrocode}
  \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
%    \end{macrocode}
% If requested, reconstruct the |\documentclass| command using
% information from \pf{xkeyval}.
%    \begin{macrocode}
\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
%    \end{macrocode}
% Perform redefinitions at the beginning of the document.
%    \begin{macrocode}
\AtBeginDocument{%
  \ifXTR@handles
    \XTR@writeout{}%
    \XTR@writeout{\string\begin{document}}%
  \fi
%    \end{macrocode}
% Redefine environments.
%    \begin{macrocode}
  \XKV@ifundefined{XTR@envs}{}{%
    \XKV@for@o\XTR@envs\XTR@tempa{%
%    \end{macrocode}
% Check whether the environment is defined.
%    \begin{macrocode}
      \XKV@ifundefined\XTR@tempa{%
        \XTR@err{%
          environment `\XTR@tempa' not defined; extraction canceled%
        }%
      }{%
%    \end{macrocode}
% Backup the beginning of the environment.
%    \begin{macrocode}
        \XTR@namelet{XTR\XTR@tempa}{\XTR@tempa}%
%    \end{macrocode}
% Redefine the beginning of the environment. This uses \pf{verbatim}
% internally.
%    \begin{macrocode}
        \@namedef{\XTR@tempa\expandafter}\expandafter{\expandafter
          \def\expandafter\XTR@tempa\expandafter{\XTR@tempa}%
%    \end{macrocode}
% Check whether the current environment should be extracted. Note
% that |\XTR@tempa| contains the current environment name.
%    \begin{macrocode}
          \XTR@checkxtr{extract}\XTR@tempa
          \ifXTR@extract
%    \end{macrocode}
% If extraction is required, write to the target file and to a temporary
% file for inclusion afterwards.
%    \begin{macrocode}
            \XTR@writeout{}\XTR@opentmp
            \@bsphack
            \let\do\@makeother\dospecials\catcode`\^^M\active
%    \end{macrocode}
% \begin{macro}{\verbatim@processline}
% \changes{v1.1}{2005/01/01}{Made to write content on same line as
% environment heading if so in source file}
% Process macro for \pf{verbatim}.
%    \begin{macrocode}
            \def\verbatim@processline{%
%    \end{macrocode}
% |\verbatim@processline| is redefined here since the first line is
% treated specially, see below.
%    \begin{macrocode}
              \let\verbatim@processline\XTR@processline@begin
%    \end{macrocode}
% Write the content to the files.
%    \begin{macrocode}
              \XTR@writeout{%
                \string\begin{\XTR@tempa}\the\verbatim@line
              }%
              \XTR@writetmp{%
                \string\begin{XTR\XTR@tempa}\the\verbatim@line
              }%
            }%
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
            \XTR@sttrue\let\XTR@tempb\verbatim@
          \else
%    \end{macrocode}
% Else, execute the backup of the current environment.
%    \begin{macrocode}
            \edef\XTR@tempb{\noexpand\begin{XTR\XTR@tempa}}%
          \fi
          \XTR@tempb
        }%
%    \end{macrocode}
% Backup the end of the environment.
%    \begin{macrocode}
        \XTR@namelet{endXTR\XTR@tempa}{end\XTR@tempa}%
%    \end{macrocode}
% Redefine the end of the environment.
%    \begin{macrocode}
        \@namedef{end\XTR@tempa\expandafter}\expandafter{\expandafter
          \def\expandafter\XTR@tempa\expandafter{\XTR@tempa}%
          \ifXTR@extract
            \@esphack
%    \end{macrocode}
% Finalize writing and add the |\input| to the hook at the end of the
% current environment.
%    \begin{macrocode}
            \XTR@writeout{\string\end{\XTR@tempa}}%
            \XTR@writetmp{\string\end{XTR\XTR@tempa}}%
            \XTR@closetmp
            \AfterEndEnv{\input{\jobname.xtr}}%
          \else
%    \end{macrocode}
% If not extracting, execute the backup of the end of the environment.
%    \begin{macrocode}
            \edef\XTR@tempa{\noexpand\end{XTR\XTR@tempa}}%
            \expandafter\XTR@tempa
          \fi
        }%
      }%
    }%
  }%
%    \end{macrocode}
% Redefine commands using the arguments.
%    \begin{macrocode}
  \XKV@ifundefined{XTR@cmdsargs}{}{%
%    \end{macrocode}
% Once backup the current definitions.
%    \begin{macrocode}
    \let\XTR@sect\@sect
    \let\XTR@chapter\@chapter
    \def\XTR@tempb{chapter}%
%    \end{macrocode}
% Redefine a list of macros to write themselves to the target file.
% Chapters and section are treated differently since they are
% constructed differently. |\chapter*| will not extract itself since
% this gives technical difficulties due to the fact that this macro
% is reused at several places inside other macros, taking none-character
% input in its argument.
%    \begin{macrocode}
    \XKV@for@o\XTR@cmdsargs\XTR@tempa{%
      \XKV@ifundefined\XTR@tempa{%
        \XTR@err{command `\@backslashchar\XTR@tempa' not defined;
          extraction canceled%
        }%
      }{%
%    \end{macrocode}
% Check whether allowed or not.
%    \begin{macrocode}
        \@expandtwoargs\in@{,\XTR@tempa,}%
          {,chapter,section,subsection,subsubsection,}%
        \ifin@
          \ifx\XTR@tempa\XTR@tempb
            \def\@chapter[#1]#2{%
%    \end{macrocode}
% Check whether to extract this chapter or not.
%    \begin{macrocode}
              \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
%    \end{macrocode}
% Write to file.
%    \begin{macrocode}
                \XTR@writeout{\string\chapter\the\@temptokena}%
              \fi
%    \end{macrocode}
% Typeset the chapter.
%    \begin{macrocode}
              \XTR@chapter[#1]{#2}%
            }%
          \else
%    \end{macrocode}
% We do a similar thing for sections created with |\@sect|.
%    \begin{macrocode}
            \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}{}{%
%    \end{macrocode}
% Redefine a list of commands to write themselves and the text on the
% same line to the target file. This works similar to |\extractline|.
%    \begin{macrocode}
    \XKV@for@o\XTR@cmdsline\XTR@tempa{%
      \XKV@ifundefined\XTR@tempa{%
%    \end{macrocode}
% Check whether the command is defined.
%    \begin{macrocode}
        \XTR@err{command `\@backslashchar\XTR@tempa' not defined;
          extraction canceled}%
      }{%
%    \end{macrocode}
% Check whether allowed or not.
%    \begin{macrocode}
        \@expandtwoargs\in@{,\XTR@tempa,}%
          {,chapter,section,subsection,subsubsection,}%
        \ifin@
          \XTR@err{%
            use the `extract-cmd' option for command `\XTR@tempa'%
          }%
        \else
%    \end{macrocode}
% Backup the command.
%    \begin{macrocode}
          \XTR@namelet{XTR\XTR@tempa}{\XTR@tempa}%
%    \end{macrocode}
% Redefine the command. Note that, inside the definition of the command,
% |\XTR@tempa| contains the command name.
%    \begin{macrocode}
          \@namedef{\XTR@tempa\expandafter}\expandafter{\expandafter
            \def\expandafter\XTR@tempa\expandafter{\XTR@tempa}%
%    \end{macrocode}
% Check whether this command should be extracted.
%    \begin{macrocode}
            \XTR@checkxtr{extract}\XTR@tempa
              \begingroup
                \let\do\@makeother\dospecials\catcode`\^^M\active
                \XTR@extractcmdline
          }%
        \fi
      }%
    }%
    \begingroup
      \catcode`\~=\active\lccode`\~=`\^^M
%    \end{macrocode}
% \begin{macro}{\XTR@extractcmdline}
% \changes{v1.4}{2005/02/06}{Changed temp macro to token register}
% \meta{text}\meta{eol}\\
% Workhorse for the command line extraction method. This macros reads
% until the next end of line and saves the content in |\XTR@tempb|.
%    \begin{macrocode}
    \lowercase{\endgroup
      \def\XTR@extractcmdline#1~{\verbatim@line{#1}\XTR@@xtractcmdline}%
    }%
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\XTR@@xtractcmdline}
% Finalize the operation with the content of the current line. We write
% it to a target file and to a temporary file for execution in the
% current document. Note that |\XTR@tempa| still contains the current
% command name.
%    \begin{macrocode}
    \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}%
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
% Finalize writing the target file.
%    \begin{macrocode}
\AtEndDocument{%
  \ifXTR@handles
    \XTR@writeout{}%
    \XTR@writeout{\string\end{document}}%
  \fi
  \immediate\closeout\XTR@out
}
\fi
%</extract>
%    \end{macrocode}
% \Finale
% \endinput
% \iffalse
%<*xtrex1>
\documentclass[10pt]{article}
\usepackage[
  active,
  generate=file,
  copydocumentclass=false,
  extract-env=equation
]{extract}
\begin{extract}
\documentclass[11pt]{article}
\end{extract}
\begin{document}
Some text.
\begin{equation}
a^2+b^2=c^2
\end{equation}
Some text.
\begin{equation}
x^2+y^2=z^2
\end{equation}
Some text.
\end{document}
%</xtrex1>
%
%<*xtrex2>
\documentclass{article}
\usepackage[
  active,
  generate=file,
  extract-env=exercise,
  extract-cmd=section,
  extract-cmdline=label
]{extract}
\begin{extract*}
\newtheorem{exercise}{Exercise}
\end{extract*}
\begin{document}
\section{Theory}
\label{sec:1}
Some text.
\section{Exercises}
\begin{exercise}
Use the results from section
\ref{sec:1} to show that\dots
\end{exercise}
Some text.
\end{document}
%</xtrex2>
%
%<*xtrex3>
\documentclass{article}
\usepackage[
  active,
  generate=file,
  extract-env=figure,
  figure-nrs={1,3},
  figure-labels={fig-a,fig-b}
]{extract}
\begin{document}
Some text.
\begin{figure}
Figure 1.
\end{figure}
Some text.
\extractionlabel{fig-a}
\begin{figure}
Figure 2.
\end{figure}
Some text.
\extractionlabel{fig-b}
\begin{figure}
Figure 3.
\end{figure}
Some text.
\extractionlabel{fig-c}
\begin{figure}
Figure 4.
\end{figure}
\end{document}
%</xtrex3>
%
%<*xtrex4>
\documentclass{article}
\usepackage[
  active,
  generate=file,
  extract-env=equation*
]{extract}
\begin{extract*}
\usepackage{amsmath}
\end{extract*}
\begin{document}
Some text.
\begin{equation*}
x^2+y^2=z^2
\end{equation*}
\begin{extract}
$x=3$, $y=4$ and $z=5$
satisfy this equation.
\end{extract}
\end{document}
%</xtrex4>
%
%<*xtrex5>
\documentclass{article}
\usepackage[
  active,
  generate=file,
  copydocumentclass=false,
  extract-labels=type-a,
  line-labels={type-a,type-c},
  line-nrs=3
]{extract}
\begin{extract}[type-a]
\documentclass{article}
\end{extract}
\begin{extract}[type-b]
\documentclass{book}
\end{extract}
\begin{document}
\parindent0pt
\extractline[type-a]line 1\\
\extractline*line 2\\
\extractline line 3\\
\extractline*[type-a]line 4\\
\extractline*[type-c]line 5\\
\extractline line 6\\
\end{document}
%</xtrex5>
%
%<*xtrex6>
\documentclass{article}
\usepackage[
  active,
  generate=file,
  extract-env=figure
]{extract}
\begin{document}
\begin{figure}[!h]
\begin{extractskip}
\fbox{figure 1}
\end{extractskip}
\fbox{figure 2}
\end{figure}
\begin{extract*}
\begin{itemize}
\item 1
\item 2
a\begin{extractskip}b
\item 3
c\end{extractskip}d
\item 4
\begin{extractskip}
\item 5
\end{extractskip}
\end{itemize}
\end{extract*}
\end{document}
%</xtrex6>
%
%<*xtrex7>
\documentclass{article}
\usepackage[
  active,
  generate=file,
  extractskip-labels=skipb
]{extract}
\begin{document}
\begin{extract*}
\begin{itemize}
\begin{extractskip}[skipa]
\item 1
\end{extractskip}
\begin{extractskip}[skipb]
\item 2
\end{extractskip}
\begin{extractskip}[skipc]
\item 3
\end{extractskip}
\end{itemize}
\end{extract*}
\end{document}
%</xtrex7>
%
%<*preamble>
\usepackage{url}
\usepackage{extract}
\usepackage{xcolor}
\usepackage{fourier}
\usepackage{varioref}
\def\reftextafter{on page~\thevpagerefnum}
\def\reftextbefore{on page~\thevpagerefnum}
\def\reftextfaceafter{on page~\thevpagerefnum}
\def\reftextfacebefore{on page~\thevpagerefnum}
\usepackage{listings}
\lstnewenvironment{command}{%
  \lstset{columns=flexible,frame=single,backgroundcolor=\color{blue!20},%
    xleftmargin=\fboxsep,xrightmargin=\fboxsep,escapeinside=`',gobble=1}}{}
\lstnewenvironment{example}{%
  \lstset{basicstyle=\footnotesize\ttfamily,columns=flexible,frame=single,%
    backgroundcolor=\color{yellow!20},xleftmargin=\fboxsep,%
    xrightmargin=\fboxsep,gobble=1}}{}
\usepackage{float}
\newfloat{Listing}{htb}{loe}
\makeatletter
\def\tableofcontents{%
  \begin{multicols}{2}%
  [\section*{Contents}%
  \setlength{\columnseprule}{.4pt}%
  \setlength{\columnsep}{18pt}]%
  \@starttoc{toc}%
  \end{multicols}%
}
\def\changes@#1#2#3{%
  \protected@edef\@tempa{%
    \noexpand\glossary{\textbf{#1}\hfill\emph{(#2)}%
      \levelchar
      \ifx\saved@macroname\@empty
        \space\actualchar\generalname
      \else
        \expandafter\@gobble\saved@macroname
        \actualchar\string\verb\quotechar*%
        \verbatimchar\saved@macroname\verbatimchar
      \fi
      :\levelchar #3}%
    }%
  \@tempa\endgroup\@esphack
}
\def\DescribeMacros{\leavevmode\@bsphack
  \begingroup\MakePrivateLetters\Describe@Macros}
\def\Describe@Macros#1{\endgroup\strut
  \marginpar{\raggedleft
  \def\@tempa{#1}\count@\z@
  \@for\@tempa:=\@tempa\do{%
    \ifnum\count@>\z@\\\fi\advance\count@\@ne
    \MacroFont\expandafter\string\@tempa
    \expandafter\SpecialUsageIndex\expandafter{\@tempa}%
  }}%
  \@esphack\ignorespaces
}
\def\DescribeOption#1{\leavevmode\@bsphack
              \marginpar{\raggedleft\PrintDescribeOption{#1}}%
              \SpecialOptionIndex{#1}\@esphack\ignorespaces}
\def\PrintDescribeOption#1{\strut\emph{option}\\\MacroFont #1\ }
\def\SpecialOptionIndex#1{\@bsphack
    \index{#1\actualchar{\protect\ttfamily#1}
           (option)\encapchar usage}%
    \index{options:\levelchar#1\actualchar{\protect\ttfamily#1}\encapchar
           usage}\@esphack}
\def\DescribeOptions#1{\leavevmode\@bsphack
  \marginpar{\raggedleft\strut\emph{options}%
  \@for\@tempa:=#1\do{%
    \\\strut\MacroFont\@tempa\SpecialOptionIndex\@tempa
  }}\@esphack\ignorespaces}
\def\DescribeEnv#1{\leavevmode\@bsphack
              \marginpar{\raggedleft\PrintDescribeEnv{#1}}%
              \SpecialEnvIndex{#1}\@esphack\ignorespaces}
\def\PrintDescribeEnv#1{\strut\emph{environment}\\\MacroFont #1\ }
\def\SpecialEnvIndex#1{\@bsphack
    \index{#1\actualchar{\protect\ttfamily#1}
           (environment)\encapchar usage}%
    \index{environments:\levelchar#1\actualchar{\protect\ttfamily#1}\encapchar
           usage}\@esphack}
\def\SpecialMainEnvIndex#1{\@bsphack\special@index{%
                                      #1\actualchar
                                      {\string\ttfamily\space#1}
                                      \encapchar main}%
    \special@index{environments:\levelchar#1\actualchar{%
                   \string\ttfamily\space#1}\encapchar
           main}\@esphack}
\def\DescribeEnvs#1{\leavevmode\@bsphack
  \marginpar{\raggedleft\strut\emph{environments}%
  \@for\@tempa:=#1\do{%
    \\\strut\MacroFont\@tempa\SpecialEnvIndex\@tempa
  }}\@esphack\ignorespaces}
\renewenvironment{theglossary}{%
  \glossary@prologue
  \GlossaryParms \let\item\@idxitem \ignorespaces
  }{}%
\makeatother
\def\PrintChangesX{%
  \begingroup
    \let\efill\relax
    \PrintChanges
  \endgroup
}
\def\PrintIndexX{%
  \begingroup
    \setcounter{IndexColumns}{2}
    \setlength{\columnsep}{18pt}%
    \setlength{\columnseprule}{.4pt}%
    \PrintIndex
  \endgroup
}
\def\pf#1{\textsf{#1}}
\def\descriptionlabel{\hspace\labelsep\normalfont}
\EnableCrossrefs
\RecordChanges
\GlossaryPrologue{\section*{Version history}}
\CodelineIndex
%</preamble>
%
%<*bib>
@MISC{LaTeXbase,
  author       = {Johannes Braams and David Carlisle and Alan Jeffrey and Leslie Lamport
                    and Frank Mittelbach and Chris Rowley and Rainer Sch\"opf},
  title        = {The {\LaTeXe} Sources},
  howpublished = {\url{CTAN:/macros/latex/base}},
  year         = 2003
}

@MISC{xkeyval,
  author       = {Hendri Adriaens},
  title        = {\pf{xkeyval} package},
  howpublished = {\url{CTAN:/macros/latex/contrib/xkeyval}}
}

@MISC{verbatim,
  author       = {Rainer Sch\"opf},
  title        = {\pf{verbatim} package, v1.5q},
  howpublished = {\url{CTAN:/macros/latex/required/tools}},
  year         = {2003/08/22}
}

@MISC{xcomment,
  author       = {Timothy Van Zandt},
  title        = {\pf{xcomment} package, v1.2},
  howpublished = {\url{CTAN:/macros/latex/contrib/seminar}},
  year         = {1993/02/12}
}

@MISC{askinclude,
  author       = {{Pablo A. Straub}},
  title        = {\pf{askinclude} package, v1.2e},
  howpublished = {\url{CTAN:/macros/latex/contrib/misc}},
  year         = {1994/11/11}
}

@MISC{excludeonly,
  author       = {Dan Luecking},
  title        = {\pf{excludeonly} package, v1.0},
  howpublished = {\url{CTAN:/macros/latex/contrib/misc}},
  year         = {2003/03/14}
}

@MISC{comment,
  author       = {Victor Eijkhout},
  title        = {\pf{comment} package, v3.6},
  howpublished = {\url{CTAN:/macros/latex/contrib/comment}},
  year         = {1999/10}
}

@MISC{optional,
  author       = {Donald Arseneau},
  title        = {\pf{optional} package, v2.2},
  howpublished = {\url{CTAN:/macros/latex/contrib/misc}},
  year         = {2001/09}
}

@MISC{version,
  author       = {Stephen Bellantoni},
  title        = {\pf{version} package},
  howpublished = {\url{CTAN:/macros/latex/contrib/misc}},
  year         = {1990}
}

@MISC{versions,
  author       = {Uwe L\"uck},
  title        = {\pf{versions} package, v0.51},
  howpublished = {\url{CTAN:/macros/latex/contrib/versions}},
  year         = {2003/10/15}
}

@MISC{pagesel,
  author       = {Heiko Oberdiek},
  title        = {\pf{pagesel} package, v1.1},
  howpublished = {\url{CTAN:/macros/latex/contrib/oberdiek}},
  year         = {1999/04/13}
}

@MISC{pdfpages,
  author       = {Andreas Matthias},
  title        = {\pf{pdfpages} package, v0.3e},
  howpublished = {\url{CTAN:/macros/latex/contrib/pdfpages}},
  year         = {2004/01/31}
}

@MISC{selectp,
  author       = {Donald Arseneau},
  title        = {\pf{selectp} package, v0.9},
  howpublished = {\url{CTAN:/macros/latex/contrib/misc}},
  year         = {1992/09/25}
}

@MISC{fancyvrb,
  author       = {Timothy Van Zandt},
  title        = {\pf{fancyvrb} package, v2.6},
  howpublished = {\url{CTAN:/macros/latex/contrib/fancyvrb}},
  year         = {1998/07/17}
}

@MISC{listings,
  author       = {Carsten Heinz},
  title        = {\pf{listings} package, v1.3},
  howpublished = {\url{CTAN:/macros/latex/contrib/listings}},
  year         = {2004/09/07}
}
%</bib>
%
% \fi
