% \iffalse meta-comment
%
% Copyright (C) 2000, 2003, 2006 by
%    Heiko Oberdiek <oberdiek@uni-freiburg.de>
%
% 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 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% This Current Maintainer of this work is Heiko Oberdiek.
%
% This work consists of the main source file engord.dtx
% and the derived files
%    engord.sty, engord.pdf, engord.ins, engord.drv.
%
% Distribution:
%    CTAN:macros/latex/contrib/oberdiek/engord.dtx
%    CTAN:macros/latex/contrib/oberdiek/engord.pdf
%
% Unpacking:
%    (a) If engord.ins is present:
%           tex engord.ins
%    (b) Without engord.ins:
%           tex engord.dtx
%    (c) If you insist on using LaTeX
%           latex \let\install=y\input{engord.dtx}
%        (quote the arguments according to the demands of your shell)
%
% Documentation:
%    (a) If engord.drv is present:
%           latex engord.drv
%    (b) Without engord.drv:
%           latex engord.dtx; ...
%    The class ltxdoc loads the configuration file ltxdoc.cfg
%    if available. Here you can specify further options, e.g.
%    use A4 as paper format:
%       \PassOptionsToClass{a4paper}{article}
%
%    Programm calls to get the documentation (example):
%       pdflatex engord.dtx
%       makeindex -s gind.ist engord.idx
%       pdflatex engord.dtx
%       makeindex -s gind.ist engord.idx
%       pdflatex engord.dtx
%
% Installation:
%    TDS:tex/generic/oberdiek/engord.sty
%    TDS:doc/latex/oberdiek/engord.pdf
%    TDS:source/latex/oberdiek/engord.dtx
%
%<*ignore>
\begingroup
  \def\x{LaTeX2e}
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex
\Msg{************************************************************************}
\Msg{* Installation}
\Msg{* Package: engord 2006/02/20 v1.2 Provides the ifpdf switch (HO)}
\Msg{************************************************************************}

\keepsilent
\askforoverwritefalse

\preamble

This is a generated file.

Copyright (C) 2000, 2003, 2006 by
   Heiko Oberdiek <oberdiek@uni-freiburg.de>

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 2005/12/01 or later.

This work has the LPPL maintenance status "maintained".

This Current Maintainer of this work is Heiko Oberdiek.

This work consists of the main source file engord.dtx
and the derived files
   engord.sty, engord.pdf, engord.ins, engord.drv.

\endpreamble

\generate{%
  \file{engord.ins}{\from{engord.dtx}{install}}%
  \file{engord.drv}{\from{engord.dtx}{driver}}%
  \usedir{tex/generic/oberdiek}%
  \file{engord.sty}{\from{engord.dtx}{package}}%
}

\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* file into a directory searched by TeX:}
\Msg{*}
\Msg{*     engord.sty}
\Msg{*}
\Msg{* To produce the documentation run the file `engord.drv'}
\Msg{* through LaTeX.}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{engord.drv}%
  [2006/02/20 v1.2 Provides the ifpdf switch (HO)]
\documentclass{ltxdoc}
\usepackage{holtxdoc}
\begin{document}
  \DocInput{engord.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{274}
%
% \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         \~}
%
% \GetFileInfo{engord.drv}
%
% \title{The \xpackage{engord} package}
% \date{2006/02/20 v1.2}
% \author{Heiko Oberdiek\\\xemail{oberdiek@uni-freiburg.de}}
%
% \maketitle
%
% \begin{abstract}
% The package generates the suffix of English ordinal numbers.
% It can be used with plain and \LaTeX\ formats.
% \end{abstract}
%
% \tableofcontents
%
% \section{Usage}
%
% \begin{declcs}{engord}\,\M{\LaTeX\ counter name}
% \end{declcs}
% It prints the value of the \LaTeX\ counter as English
% ordinal number. It can be used in the same way as
% \cs{arabic}, \cs{roman}, or \cs{alph}. The command is
% not available in \plainTeX.
%
% \begin{declcs}{engordnumber}\,\M{any \TeX\ number}
% \end{declcs}
% It prints the number as English ordinal number.
%
% \begin{declcs}{engordletters}\,|{#1}|
% \end{declcs}
% This command formats the English ordinal letters
% after the number. It defaults to \cs{textsuperscript}.
%
% \begin{declcs}{engorderror}\,|{#1}|
% \end{declcs}
% It can be redefined, if an other error handling is
% wanted. The argument is a negative number or zero.
%
% \begin{declcs}{engordraisetrue}\\
%   \SpecialUsageIndex{\engordraisefalse}\cs{engordraisefalse}
% \end{declcs}
% These commands set the switch \cs{ifengordraise} that is
% asked by the default \cs{engordletters} before raising the
% ordinal letters.
%
% \subsection{Package options}
%
% \begin{description}
% \item[\xoption{normal}:] \cs{engordraisefalse}
% \item[\xoption{raise}:] \cs{engordraisetrue}
% \end{description}
% Default is \xoption{raise}.
%
% \subsection{Examples}
%
% \begin{itemize}
% \item
%   |\usepackage[normal]{engord}|\\
%   \begin{tabular}{@{}l@{ $\rightarrow$ }l@{}}
%     |\engordnumber{1}|   & 1st\\
%     |\engordnumber{12}|  & 12th\\
%     |\engordnumber{123}| & 123rd\\
%     |\engord{page}|      & 1st (if page has the value of one)\\
%     \multicolumn{2}{@{}l}{\cs{engordraisetrue}}\\
%     |\engordnumber{12}|  & 12\textsuperscript{th}
%   \end{tabular}
% \item
%     The default output of a counter can be redefined:
%     \begin{quote}
%       |\newcounter{mycounter}|\\
%       |\renewcommand{\theengcounter}{\engord{mycounter}}|
%     \end{quote}
% \item
%     Because the implementation of \cs{engord} and \cs{engordnumber}
%     is kept expandable, these commands can be used to make
%     command names with an appropriate definition of
%     \cs{engordletters}:
%     \begin{quote}
%       |\renewcommand*{\engordletters}[#1]{#1}|\\
%       |\@namedef{My\engordnumber{3}Command}{...}|
%     \end{quote}
%     This generates the command name `\cs{My4rdCommand}'.
%     Since version 1.2 the redefinition can be dropped if
%     the letters are not raised.
% \item
%     If the letters should not be raised, use \LaTeX\ package
%     option \xoption{normal} or use
%     \begin{quote}
%       \cs{engordraisefalse}
%     \end{quote}
%     Also \cs{engordletters} could be redefined for this purpose:
%     \begin{quote}
%       |\renewcommand*{\engordletters}[1]{#1}|
%     \end{quote}
% \end{itemize}
%
% \StopEventually{
% }
%
% \section{Implementation}
%
% \subsection{Reload check and identification}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%    Reload check, especially if the package is not used with \LaTeX.
%    \begin{macrocode}
\begingroup
  \expandafter\let\expandafter\x\csname ver@engord.sty\endcsname
  \ifcase 0%
    \ifx\x\relax % plain
    \else
      \ifx\x\empty % LaTeX
      \else
        1%
      \fi
    \fi
  \else
    \expandafter\ifx\csname PackageInfo\endcsname\relax
      \def\x#1#2{%
        \immediate\write-1{Package #1 Info: #2.}%
      }%
    \else
      \def\x#1#2{\PackageInfo{#1}{#2, stopped}}%
    \fi
    \x{engord}{The package is already loaded}%
    \endgroup
    \expandafter\endinput
  \fi
\endgroup
%    \end{macrocode}
%    Package identification:
%    \begin{macrocode}
\begingroup
  \expandafter\ifx\csname ProvidesPackage\endcsname\relax
    \def\x#1#2#3[#4]{\endgroup
      \immediate\write-1{Package: #3 #4}%
      \xdef#1{#4}%
    }%
  \else
    \def\x#1#2[#3]{\endgroup
      #2[{#3}]%
      \ifx#1\relax
        \xdef#1{#3}%
      \fi
    }%
  \fi
\expandafter\x\csname ver@engord.sty\endcsname
\ProvidesPackage{engord}%
  [2006/02/20 v1.2 Provides the ifpdf switch (HO)]
%    \end{macrocode}
%
% \subsection{Help commands for plain compatibility}
%
%    \begin{macro}{\EO@atcode}
%    Save catcode of |@|.
%    \begin{macrocode}
\expandafter\chardef\csname EO@atcode\endcsname\catcode`\@
\catcode`\@=11 %
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\EO@def}
%    Definitions, \cs{newcommand} does not exist in \plainTeX.
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname newcommand\endcsname\relax
  \def\EO@def{\def}%
\else
  \def\EO@def#1{%
    \newcommand*{#1}{}%
    \def#1%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EO@PackageWarning}
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname PackageWarning\endcsname\relax
  \def\EO@PackageWarning#1#2{%
    \immediate\write16{%
      Package #1 Warning: #2 on input line \the\inputlineno.%
    }%
  }%
\else
  \def\EO@PackageWarning{\PackageWarning}%
\fi
%    \end{macrocode}
%    \end{macro}
%
% \subsection{User macros}
%
%    \begin{macro}{\ifengordraise}
%    The switch \cs{ifengordraise}, whether the ordinal letters
%    are raised or not. Default is raised because of compatibility.
%    \begin{macrocode}
\newif\ifengordraise
\engordraisetrue
%    \end{macrocode}
%    \end{macro}
%    In \LaTeX\ this also can be controlled by option \xoption{normal}
%    or \xoption{raise}.
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname DeclareOption\endcsname\relax
\else
  \DeclareOption{normal}{\engordraisefalse}%
  \DeclareOption{raise}{\engordraisetrue}%
  \ProcessOptions*\relax
\fi
%    \end{macrocode}
%
%    \begin{macro}{\engordletters}
%    \cs{engordletters} is called with one argument, the english
%    ordinal letters, and contains the code to format them. It
%    defaults to \cs{textsuperscript} depending on \cs{ifengordraise}.
%    \begin{macrocode}
\expandafter\ifx\csname engordletters\endcsname\relax
  \EO@def\engordletters{%
    \ifengordraise
      \expandafter\engordtextsuperscript
    \fi
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\engordtextsuperscript}
%    For \plainTeX\ the definition is quite ugly, redefine
%    \cs{engordtextsuperscript} if you have a better one.
%    \begin{macrocode}
\expandafter\ifx\csname engordtextsuperscript\endcsname\relax
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname textsuperscript\endcsname\relax
    \def\engordtextsuperscript#1{%
      \relax
      \ifmmode
        ^{\rm#1}%
      \else
        $^{\rm#1}$%
      \fi
    }%
  \else
    \def\engordtextsuperscript{\textsuperscript}%
  \fi
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\engorderror}
%    \cs{engorderror} is called, if the number is zero
%    or negative.
%    \begin{macrocode}
\expandafter\ifx\csname engorderror\endcsname\relax
  \EO@def\engorderror#1{%
    #1\engordletters{!ERROR!}%
    \EO@PackageWarning{engord}{%
      `#1' is not an ordinal number%
    }%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\engord}
%    \cs{engord} expects a \LaTeX\ counter name as argument and
%    calls \cs{engordnumber}. It is defined only, if \LaTeX\ is used.
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname newcounter\endcsname\relax
\else
  \EO@def\engord#1{%
    \engordnumber{\value{#1}}%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\engordnumber}
%    \cs{engordnumber} is the user command to print a number as
%    english ordinal number. The argument can be any
%    \TeX\ number like explicit numbers, register values, \dots
%
%    In a safe way it converts the \TeX\ number argument
%    into a form that only consists of decimal digits.
%    \begin{macrocode}
\EO@def\engordnumber#1{%
  \expandafter\EO@number\expandafter{\number#1}%
}
%    \end{macrocode}
%    \end{macro}
%
% \subsection{Suffix generation}
%
%    \begin{macro}{\EO@number}
%    \cs{EO@number} expects a number with decimal digits as
%    argument and looks at the size of the number and
%    the count of the digits:
%    \begin{macrocode}
\def\EO@number#1{%
  \ifnum#1<1 % handle the error case
    \engorderror{#1}%
  \else
    \ifnum#1<21
      \EO@ord{#1}%
    \else
      \ifnum#1<100
        \EO@twodigits#1%
      \else
        \@ReturnAfterFi{%
          \EO@reverse#1\@nil{}\EO@afterreverse
        }%
      \fi
    \fi
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\@ReturnAfterFi}
%    An internal help macro to prevent a too deep \cs{if} nesting.
%    \begin{macrocode}
\long\def\@ReturnAfterFi#1\fi{\fi#1}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EO@ord}
%    \cs{EO@ord} prints the number with ord letters.\\
%    |#1|: decimal digits, |#1| $<$ 21
%    \begin{macrocode}
\def\EO@ord#1{%
  #1%
  \expandafter\engordletters
  \ifcase#1{th}\or
    {st}\or
    {nd}\or
    {rd}\else
    {th}%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EO@twodigits}
%    \cs{EO@twodigits} expects a number with two digits,\\
%    20 $<$ number $<$ 100
%    \begin{macrocode}
\def\EO@twodigits#1#2{%
  #1\EO@ord{#2}%
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EO@reverse}
%    \cs{EO@reverse} reverses the digits of the number.\\
%    |#1|: next digit\\
%    |#2|: rest of the digits\\
%    |#3|: already reversed digits\\
%    |#4|: next command to call with the reversed number
%    as argument
%    \begin{macrocode}
\def\EO@reverse#1#2\@nil#3#4{%
  \ifx\\#2\\%
    #4{#1#3}%
  \else
    \@ReturnAfterFi{%
      \EO@reverse#2\@nil{#1#3}{#4}%
    }%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EO@afterreverse}
%    \cs{EO@afterreverse} calls \cs{EO@reverseback} so that
%    \cs{EO@reverseback} can inspect the digits of the number.
%    \begin{macrocode}
\def\EO@afterreverse#1{%
  \EO@reverseback#1\@nil
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EO@reverseback}
%    \cs{EO@reverseback} reverses the reversion.\\
%    |#1|: the last digit of the number\\
%    |#2|: the second last digit of the number\\
%    |#3|: first digits of the number in reversed order,
%     it is not empty, because \cs{EO@reverseback} is
%     only called with numbers $>$ 100.
%    \begin{macrocode}
\def\EO@reverseback#1#2#3\@nil{%
  \EO@reverse#3\@nil{}\@firstofone
  \ifnum#2#1<21
    \EO@ord{#2#1}%
  \else
    #2\EO@ord{#1}%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
%    Restore catcode of |@|.
%    \begin{macrocode}
\catcode`\@=\EO@atcode
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
% \section{Installation}
%
% \paragraph{CTAN.} This package is available on
% CTAN\footnote{\url{ftp://ftp.ctan.org/tex-archive/}}:
% \begin{description}
% \item[\CTAN{macros/latex/contrib/oberdiek/engord.dtx}] The source file.
% \item[\CTAN{macros/latex/contrib/oberdiek/engord.pdf}] Documentation.
% \end{description}
%
% \paragraph{Unpacking.} The \xfile{.dtx} file is a self-extracting
% \docstrip\ archive. The files are extracted by running the
% \xfile{.dtx} through \plainTeX:
% \begin{quote}
%   \verb|tex engord.dtx|
% \end{quote}
%
% \paragraph{TDS.} Now the different files must be moved into
% the different directories in your installation TDS tree
% (also known as \xfile{texmf} tree):
% \begin{quote}
% \sbox0{^^A
% \begin{tabular}{@{}>{\ttfamily}l@{$\quad\rightarrow\quad$}>{\ttfamily}l@{}}
%   engord.sty & tex/generic/oberdiek/engord.sty\\
%   engord.pdf & doc/latex/oberdiek/engord.pdf\\
%   engord.dtx & source/latex/oberdiek/engord.dtx\\
% \end{tabular}^^A
% }%
% \ifdim\wd0>\linewidth
%   \mbox{}\nobreak\hskip0pt minus\leftmargin
%   \usebox0
%   \nobreak\hskip0pt minus\rightmargin
% \else
%   \usebox0
% \fi
% \end{quote}
% If you have a \xfile{docstrip.cfg} that configures and enables \docstrip's
% TDS installing feature, then some files can already be in the right
% place, see the documentation of \docstrip.
%
% \paragraph{Refresh file databases.} If your \TeX~distribution
% (\teTeX, \mikTeX, \dots) rely on file databases, you must refresh
% these. For example, \teTeX\ users run \verb|texhash| or
% \verb|mktexlsr|.
%
% \subsection{Some details for the interested}
%
% \paragraph{Attached source.}
%
% The PDF documentation on CTAN also includes the
% \xfile{.dtx} source file. It can be extracted by
% AcrobatReader 6 or higher. Another option is \textsf{pdftk},
% e.g. unpack the file into the current directory:
% \begin{quote}
%   \verb|pdftk engord.pdf unpack_files output .|
% \end{quote}
%
% \paragraph{Unpacking with \LaTeX.}
% The \xfile{.dtx} chooses its action depending on the format:
% \begin{description}
% \item[\plainTeX:] Run \docstrip\ and extract the files.
% \item[\LaTeX:] Generate the documentation.
% \end{description}
% If you insist on using \LaTeX\ for \docstrip\ (really,
% \docstrip\ does not need \LaTeX), then inform the autodetect routine
% about your intention:
% \begin{quote}
%   \verb|latex \let\install=y\input{engord.dtx}|
% \end{quote}
% Do not forget to quote the argument according to the demands
% of your shell.
%
% \paragraph{Generating the documentation.}
% You can use both the \xfile{.dtx} or the \xfile{.drv} to generate
% the documentation. The process can be configured by the
% configuration file \xfile{ltxdoc.cfg}. For instance, put this
% line into this file, if you want to have A4 as paper format:
% \begin{quote}
%   \verb|\PassOptionsToClass{a4paper}{article}|
% \end{quote}
% An example follows how to generate the
% documentation with pdf\LaTeX:
% \begin{quote}
%\begin{verbatim}
%pdflatex engord.dtx
%makeindex -s gind.ist engord.idx
%pdflatex engord.dtx
%makeindex -s gind.ist engord.idx
%pdflatex engord.dtx
%\end{verbatim}
% \end{quote}
%
% \begin{History}
%   \begin{Version}{2000/05/23 v1.0}
%   \item
%     First public release.
%   \end{Version}
%   \begin{Version}{2003/04/28 v1.1}
%   \item
%     Bug fix for 30, 40, 50, \dots, 100, 130, \dots
%   \item
%     \cs{ordletters} renamed to documented \cs{engordletters}.
%   \end{Version}
%   \begin{Version}{2006/02/20 v1.2}
%   \item
%     Support for \plainTeX.
%   \item
%     Switch \cs{ifengordraise} added.
%   \item
%     Package options \xoption{raise} and \xoption{normal} added.
%   \item
%     DTX framework.
%   \end{Version}
% \end{History}
%
% \PrintIndex
%
% \Finale
\endinput
