% \iffalse meta-comment
%
% etaremune package by Hendri Adriaens.
%
% Extract package files and examples and create documentation:
%    latex etaremune.dtx
%    latex etaremune.dtx
%    bibtex etaremune
%    makeindex -s gglo.ist -o etaremune.gls etaremune.glo
%    makeindex -s gind.ist -o etaremune.ind etaremune.idx
%    latex etaremune.dtx
%    latex etaremune.dtx
%
% To finish the installation you have to move the following
% file into a directory searched by LaTeX:
%    etaremune.sty
%
%% ----------------------------------
%% Copyright (C) 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 etaremune.dtx and the
%% derived file etaremune.sty.
%%
%% The following files constitute the etaremune package and must be
%% distributed as a whole: readme, etaremune.dtx, etaremune.pdf and
%% etaremune.sty.
%%
% \fi
%
% \iffalse
%<*batchfile>
\begingroup
\input docstrip
\keepsilent
\preamble
\endpreamble
\askforoverwritefalse
\generate{
  \file{etaremune.sty}{\from{etaremune.dtx}{etaremune}}
  \file{empream.ble}{\from{etaremune.dtx}{preamble}}
  \file{etaremune.bib}{\from{etaremune.dtx}{bib}}
}
\endgroup
%</batchfile>
%<*driver>
\documentclass[a4paper]{ltxdoc}
\input{empream.ble}
\begin{document}
\DocInput{etaremune.dtx}
\let\Section\section\def\section*#1{\Section*{#1}\addcontentsline{toc}{section}{#1}}
\bibliographystyle{plain}
\bibliography{etaremune}
\PrintChangesX\PrintIndexX
\end{document}
%</driver>
% \fi
%
% \changes{v1.0}{2005/03/12}{Initial release}
% \changes{v1.1}{2005/04/15}{Simplified}
% \changes{v1.1}{2005/04/15}{Added check on negative labels}
% \changes{v1.2}{2005/06/01}{Added global and local style control for lists}
%
% \GetFileInfo{etaremune.sty}
%
% \CheckSum{141}
%
%\DoNotIndex{\@auxout,\@currentlabel,\@enumctr,\@enumdepth,\@ifundefined,
%\@latex@warning@no@line,\@ne,\@spaces,\@toodeep,\addtocounter,\advance,
%\AtEndDocument,\csname,\def,\edef,\else,\endcsname,\endlist,\expandafter,
%\fi,\gdef,\hss,\ifnum,\immediate,\let,\list,\llap,\makelabel,\NeedsTeXFormat,
%\newcounter,\newenvironment,\ProvidesPackage,\relax,\romannumeral,\setcounter,
%\space,\stepcounter,\string,\the,\thr@@,\write,\xdef,\z@,\m@ne,
%\define@key,\define@cmdkey,\ProcessOptionsX,\presetkeys,\DeclareOptionX,
%\@tempa,\@tempb,\CurrentOption,\noexpand,\PackageWarning,\RequirePackage,
%\setkeys,\setlength,\setrmkeys,\XKV@for@n,\c@EM@etaremunectr,\c@EM@itemctr}
%
% \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{etaremune} package
%\thanks{This package can be downloaded from CTAN mirrors:
%\texttt{/macros/latex/contrib/etaremune}. See \texttt{etaremune.dtx} for
%information on installing \pf{etaremune} 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 implements the |etaremune| environment which is similar
%to the |enumerate| environment, except that labels are decreasing
%instead of increasing. This package provides an alternative to the
%\pf{revnum} package which uses a lot of counters. The style of lists
%can be controlled locally and globally.
%\end{abstract}
%
%\begin{multicols}{2}
%[\section*{Contents}
%\setlength{\columnseprule}{.4pt}
%\setlength{\columnsep}{18pt}]
%\tableofcontents
%\end{multicols}
%
%\section{Introduction}
%This package implements the |etaremune| environment which labels
%items with decreasing numbers, starting from the number of items
%in the environment. Since this number is not known at the start of
%the environment, we use a two step procedure. On the first \LaTeX\
%run, we determine the number of items in each |etaremune|
%environment and number the items in a default way and on the second
%run, we number all items correctly.
%
%The \pf{revnum} package \cite{revnum} already implements this
%environment. It provides the |revnumerate| environment which labels
%the items with decreasing numbers. This package has as main drawback
%that it consumes numerous counters. It uses 4 counters plus 1 for
%every |revnumerate| environment in the document. In big documents,
%this can cause problems. The package at hand uses only 2 counters,
%irrespective of the number of |etaremune| environments used in the
%document.
%
%I created the first version of the main code of this package to solve
%a question on the
%\TeX-NL mailing list. I figured that it might be a good idea to put
%my code in a small package on CTAN as an alternative for \pf{revnum}.
%
%\section{The \texttt{etaremune} environment}
%\DescribeEnv{etaremune}
%The |etaremune| environment works just as the |enumerate| environment
%and has the same control possibilities using macros like |\labelenumi|,
%|\theenumi| and |\@listi|. See for a detailed description of customizing
%|enumerate| and |etaremune| environments a \LaTeX\ manual, for instance
%\cite{companion}, pages 128--131 and 144--151.
%
%The |etaremune| environment is different from the |enumerate| environment
%in labeling the items. This environment labels items with decreasing
%numbers.
%\begin{command}
% `\cs{begin\char`\{etaremune\char`\}}\oarg{options}'
% `\meta{items}'
% `\cs{end\char`\{etaremune\char`\}}'
%\end{command}
%
%As \LaTeX\ cannot know at the beginning of the environment how many
%items it will have to typeset, this environment computes the
%starting point of the list in two \LaTeX\ runs, using the auxiliary
%file. On the first run, the number of items is counted and the items
%will be numbered with increasing numbers (as in the |enumerate|
%environment) and on the second run, the item labels will be set
%correctly. See an example below.
%
%\vspace*{.2cm}\noindent
%\begin{minipage}{.33\linewidth}
%\begin{etaremune}
%\item Not important.
%\item Quite relevant.
%\item Paramount.
%\end{etaremune}
%\end{minipage}
%\begin{minipage}{.666\linewidth}
%\begin{example}
% \begin{etaremune}
%  \item Not important.
%  \item Quite relevant.
%  \item Paramount.
% \end{etaremune}
%\end{example}
%\end{minipage}
%\vspace*{.2cm}
%
%The |etaremune| environment will avoid creating labels with negative
%numbers as this can cause problems when using alphabets for
%labels.\footnote{Positions -1, -2,\dots\ in an alphabet are not
%defined by the \LaTeX\ macro \cs{@alph} and friends.} So when
%adding items to an existing list, these will be numbered with 0 on
%the next run. Another \LaTeX\ run will settle the item labels again.
%
%The optional argument \meta{options} can be used to control the
%style of the environment (see below) and the starting point (if one
%wants to replace the starting point computed by \pf{etaremune}).
%
%\vspace*{.2cm}\noindent
%\begin{minipage}{.33\linewidth}
%\begin{etaremune}[start=5]
%\item First.
%\item Second.
%\item Third.
%\end{etaremune}
%\end{minipage}
%\begin{minipage}{.666\linewidth}
%\begin{example}
% \begin{etaremune}[start=5]
%  \item First.
%  \item Second.
%  \item Third.
% \end{etaremune}
%\end{example}
%\end{minipage}
%\vspace*{.2cm}
%
%The |etaremune| environment can be nested up to four levels deep,
%like the standard |enumerate| environment. The example below also
%shows an example of customizing item labels and referring to items.
%Notice that it takes three \LaTeX\ runs for references to items in
%the |etaremune| environment to settle.
%
%\vspace*{.2cm}\noindent
%\begin{minipage}{.33\linewidth}
%\renewcommand{\labelenumi}{\theenumi)}
%\renewcommand{\theenumii}{\roman{enumii}}
%\begin{enumerate}
%\item First.
%\begin{etaremune}
%\item third.\label{notice}
%\item second.
%\item first.
%\end{etaremune}
%\item Second.
%\item Third.
%\end{enumerate}
%Notice item~\ref{notice}.
%\end{minipage}
%\begin{minipage}{.666\linewidth}
%\begin{example}
% \renewcommand{\labelenumi}{\theenumi)}
% \renewcommand{\theenumii}{\roman{enumii}}
% \begin{enumerate}
%  \item First.
%   \begin{etaremune}
%    \item third.\label{notice}
%    \item second.
%    \item first.
%   \end{etaremune}
%  \item Second.
%  \item Third.
% \end{enumerate}
% Notice item~\ref{notice}.
%\end{example}
%\end{minipage}
%\vspace*{.2cm}
%
%\DescribeOptions{topsep,partopsep,itemsep,parsep,leftmargin,rightmargin,%
%  listparindent,itemindent,labelwidth,labelsep}
%The \pf{etaremune} package allows for specifying some style
%parameters to control the markup of lists. These are the vertical
%lengths |\topsep|, |\partopsep|, |\itemsep| and |\parsep| and the
%horizontal lengths |\leftmargin|, |\rightmargin|, |\listparindent|,
%|\itemindent|, |\labelwidth| and |\labelsep|.\footnote{See for
%instance \cite{companion}, pages 144--151, for more information.}
%When set through the optional argument \meta{options}, these lengths
%are set locally (so holding only for the environment at hand).
%
%\vspace*{.2cm}\noindent
%\begin{minipage}{.33\linewidth}
%\begin{etaremune}[itemsep=0pt,parsep=0pt]
%\item third.
%\item second.
%\begin{etaremune}
%\item second.
%\item first.
%\end{etaremune}
%\item first.
%\end{etaremune}
%\end{minipage}
%\begin{minipage}{.666\linewidth}
%\begin{example}
% \begin{etaremune}[itemsep=0pt,parsep=0pt]
%  \item third.
%  \item second.
%   \begin{etaremune}
%    \item second.
%    \item first.
%   \end{etaremune}
%  \item first.
% \end{etaremune}
%\end{example}
%\end{minipage}
%\vspace*{.2cm}
%
%One can also change the style of all |etaremune| environments
%throughout the document by specifying these options in the
%|\usepackage| command.
%
%\vspace*{.2cm}\noindent
%\begin{minipage}{.33\linewidth}
%\begin{etaremune}[leftmargin=0pt,labelsep=20pt]
%\item third.
%\item second.
%\begin{etaremune}[leftmargin=0pt,labelsep=20pt]
%\item second.
%\item first.
%\end{etaremune}
%\item first.
%\end{etaremune}
%\end{minipage}
%\begin{minipage}{.666\linewidth}
%\begin{example}
% \usepackage[leftmargin=0pt,labelsep=20pt]{etaremune}
% ...
% \begin{etaremune}
% \item third.
% \item second.
%  \begin{etaremune}
%   \item second.
%   \item first.
%  \end{etaremune}
% \item first.
% \end{etaremune}
%\end{example}
%\end{minipage}
%\vspace*{.2cm}
%
%\StopEventually{}
%
% \section{Implementation}
%    \begin{macrocode}
%<*etaremune>
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{etaremune}[2005/06/01 v1.2 Reversed enumerate (HA)]
%    \end{macrocode}
% Build global and local options. We need some delicate work with
% presets here as \LaTeX\ resets dimens locally in each environment.
% So setting lengths globally couldn't be done without presets.
%    \begin{macrocode}
\RequirePackage{xkeyval}
\def\@tempa#1#2{%
  \DeclareOptionX{#1}{\presetkeys[EM]{template}{#1=##1}{}}%
  \define@key[EM]{template}{#1}{\setlength#2{##1}}%
}
\XKV@for@n{topsep,partopsep,itemsep,parsep,leftmargin,rightmargin,%
  listparindent,itemindent,labelwidth,labelsep}\@tempb{%
  \edef\@tempb{%
    {\@tempb}\expandafter\noexpand\csname\@tempb\endcsname
  }%
  \expandafter\@tempa\@tempb
}
\DeclareOptionX*{%
  \PackageWarning{etaremune}{Unknown option `\CurrentOption'}%
}
\ProcessOptionsX
\define@cmdkey[EM]{etaremune}[EM@]{start}{}
\presetkeys[EM]{etaremune}{start=-1}{}
%    \end{macrocode}
% Two counters needed in the package.
%    \begin{macrocode}
\newcounter{EM@itemctr}
\newcounter{EM@etaremunectr}
%    \end{macrocode}
% \begin{environment}{etaremune}
% The main code.
%    \begin{macrocode}
\newenvironment{etaremune}[1][]{%
%    \end{macrocode}
% There is a (usual) maximum to nesting.
%    \begin{macrocode}
  \ifnum\@enumdepth>\thr@@\@toodeep\else
    \setkeys*[EM]{etaremune}{#1}%
    \advance\@enumdepth\@ne
%    \end{macrocode}
% Count the environments. Each environment gets a unique identification
% to be used to define macros containing the number of items and
% to set starting points on the second run.
%    \begin{macrocode}
    \stepcounter{EM@etaremunectr}%
%    \end{macrocode}
% Backup the number of items that we have seen so far in a higher
% level. We reinitialize the items counter when we get back from
% the nested environment. Unfortunately, locally counting items is
% not possible as \LaTeX\ uses a box to typeset the item label.
% Otherwise, this macros would not be necessary.
%    \begin{macrocode}
    \edef\EM@currnum{\the\c@EM@itemctr}%
%    \end{macrocode}
% This is a backup of the current level list number. This is used to
% be able to identify the current list at the end of the list taking
% into account possibly nested lists in this list.
%    \begin{macrocode}
    \edef\EM@currlist{\romannumeral\c@EM@etaremunectr}%
%    \end{macrocode}
% Define the current level list counter.
%    \begin{macrocode}
    \edef\@enumctr{enum\romannumeral\the\@enumdepth}%
%    \end{macrocode}
% Start counting items.
%    \begin{macrocode}
    \setcounter{EM@itemctr}\z@
%    \end{macrocode}
% Initialize the step size to count backwards.
%    \begin{macrocode}
    \let\EM@step\m@ne
    \ifnum\EM@start<\z@
%    \end{macrocode}
% If we have no optional input, check whether the macro for the
% current list has been defined.
%    \begin{macrocode}
      \@ifundefined{etaremune@\EM@currlist}{%
%    \end{macrocode}
% If the macro is not defined, we count forwards (like |enumerate|).
% Define the macro to produce a warning. This warning will
% be issued |\AtEndDocument| to notify the user that the document
% should be rerun.
%    \begin{macrocode}
        \let\EM@step\@ne
        \setcounter\@enumctr\m@ne
        \gdef\EM@rerun{%
          \@latex@warning@no@line{Etaremune labels have changed.^^J
          \@spaces\@spaces\@spaces\space\space Rerun to get them right}%
        }%
      }{%
%    \end{macrocode}
% If the macro is defined, it contains the number of items of the
% current list at this depth. Use that to initiate the label counter.
%    \begin{macrocode}
        \setcounter\@enumctr{\csname etaremune@\EM@currlist\endcsname}%
      }%
%    \end{macrocode}
% If there was input, use that to set the starting point.
%    \begin{macrocode}
    \else\setcounter\@enumctr\EM@start\fi
    \stepcounter\@enumctr
%    \end{macrocode}
% Start the list.
%    \begin{macrocode}
    \list{%
%    \end{macrocode}
% At every item, step the label counter, check whether it is smaller
% than 0, increase the items
% counter by 1 and define the current label for referring to items.
%    \begin{macrocode}
      \addtocounter\@enumctr\EM@step
      \ifnum\csname c@\@enumctr\endcsname<\z@\setcounter\@enumctr\z@\fi
      \stepcounter{EM@itemctr}%
      \xdef\@currentlabel{%
        \csname p@\@enumctr\endcsname\csname the\@enumctr\endcsname
      }%
%    \end{macrocode}
% Typesets the item label.
%    \begin{macrocode}
      \csname label\@enumctr\endcsname
%    \end{macrocode}
% Define how to align the label. This is the same as for the |enumerate|
% environment.
%    \begin{macrocode}
    }{\def\makelabel##1{\hss\llap{##1}}\setrmkeys[EM]{template}}%
  \fi
}{%
%    \end{macrocode}
% The end of the environment. End the list.
%    \begin{macrocode}
  \endlist
%    \end{macrocode}
% Write the number of items of the current list to the auxiliary file.
%    \begin{macrocode}
  \immediate\write\@auxout{\string\gdef\expandafter\string
    \csname etaremune@\EM@currlist\endcsname{\the\c@EM@itemctr}%
  }%
%    \end{macrocode}
% Restore the items counter to continue counting the number of items
% in the environment at this level. This is needed when lists were
% nested.
%    \begin{macrocode}
  \setcounter{EM@itemctr}\EM@currnum
}
%    \end{macrocode}
% \end{environment}
% Issue a warning when item labels haven't settled yet.
%    \begin{macrocode}
\AtEndDocument{\EM@rerun}
\let\EM@rerun\relax
%</etaremune>
%    \end{macrocode}
% \Finale
% \endinput
% \iffalse
%<*preamble>
\usepackage{url}
\usepackage{footmisc}
\usepackage{filecontents}
\usepackage{fourier}
\usepackage{etaremune}
\usepackage{xkeyval}
\usepackage{xcolor}
\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}}{}
\makeatletter
\def\tableofcontents{\@starttoc{toc}}
\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\eTeX{$\m@th\varepsilon$-\TeX}
\renewenvironment{theglossary}{%
  \section*{Version history}%
  \GlossaryParms \let\item\@idxitem \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\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}
\makeatother
\def\PrintChangesX{%
  \begingroup
    \let\efill\relax
    \PrintChanges
  \endgroup
}
\def\PrintIndexX{%
  \begingroup
    \setcounter{IndexColumns}{2}
    \setlength{\columnsep}{18pt}%
    \setlength{\columnseprule}{.4pt}%
    \PrintIndex
  \endgroup
}
\def\larg#1{{\ttfamily\char`\<}\meta{#1}{\ttfamily\char`\>}}
\def\pf#1{\textsf{#1}}
\EnableCrossrefs
\RecordChanges
\CodelineIndex
%</preamble>
%
%<*bib>
@BOOK{companion,
  author       = {Frank Mittelbach and Michel Goossens and Johannes Braams and David Carlisle and Chris Rowley},
  title        = {The {\LaTeX} {C}ompanion, {S}econd {E}dition},
  year         = {2004},
  publisher    = {{Addison-Wesley}}
}

@MISC{revnum,
  author       = {J\"orn Wilms},
  title        = {\pf{revnum} package, v1.0, 1997/05/10},
  howpublished = {\url{CTAN:/macros/latex/contrib/revnum}}
}
%</bib>
%
% \fi
