%% \CheckSum{242}
% \iffalse
% File: ordinalpt.dtx
% Copyright (C) 2007 Miguel V. S. Frasson (mvsfrasson@gmail.com)
%
% This package may be distributed under the terms of the LaTeX
% Project Public License, as described in lppl.txt in the base
% LaTeX distribution, either version 1.2 or (at your option)
% any later version.
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{doc}
\usepackage[T1]{fontenc}
\usepackage{amstext,makeidx}
\makeindex
\newcommand{\ordm}{\textordmasculine}
\newcommand{\ordf}{\textordfeminine}
\begin{document}
  \DocInput{ordinalpt.dtx}
\end{document}
%</driver>
% \fi
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \StopEventually
%
% \changes{1.0}{2007/01/10}{initial version}
% \changes{2.0}{2007/01/27}{reimplemented to make macros freely expandable}
% \changes{3.0}{2007/02/08}{add options for ordinals of 11, 12 and 13;
%                           changed default behavior for these numbers}
%
% \MakeShortVerb+
%
% \title{\textsf{ordinalpt}: counters as ordinal numbers in Portuguese}
% \author{Miguel Vin\'\i cius Santini Frasson}
% \date{2007--02--08 version 3.0}
%
% \maketitle
%
% \tableofcontents
%
% \section{Introduction}
%
%    The package \textsf{ordinalpt} provides a counter style like
%    +\arabic+, +\alph+, etc., but that produces as
%    output strings like ``primeiro'' (first in Portuguese),
%    ``segundo'' (second), ``terceiro'' (third), \ldots, up to
%    ``mil\'esimo noningent\'esimo nonag\'esimo nono''
%    (1999$^{\text{th}}$).  There are counter commands to output the
%    text in UPPERCASE, Capitalized or in lowercase, with masculine or
%    feminine gender.
%
% \section{User commands}
%
% \newcommand{\countername}{\texttt{\textit{counter}}}
%
% \DescribeMacro{\ordptmasc}
% \DescribeMacro{\Ordptmasc}
% \DescribeMacro{\ORDPTMASC}
% \DescribeMacro{\ordptfem}
% \DescribeMacro{\Ordptfem}
% \DescribeMacro{\ORDPTFEM}
%    Six commands to output all incarnations of ordinal numbers in
%    portuguese --- masculine or feminine, in ``lowercase'',
%    ``Capitalized'' or in ``UPPERCASE''---:\\
%    +  \ordptmasc{+\countername+}+ (lowercase masculine),\\
%    +  \Ordptmasc{+\countername+}+ (capitalized masculine),\\
%    +  \ORDPTMASC{+\countername+}+ (uppercase masculine),\\
%    +  \ordptfem{+\countername+}+ (lowercase feminine),\\
%    +  \Ordptfem{+\countername+}+ (capitalized feminine) and\\
%    +  \ORDPTFEM{+\countername+}+ (uppercase feminine).
%
%    Example:
%
%\begin{verbatim}
%\renewcommand{\thepage}{--- \Ordptfem{page} p\'agina ---}
%\end{verbatim}
% will produce in the place for page numbers:
%
%\centerline{--- Primeira p\'agina ---}
%\centerline{--- Segunda p\'agina ---}
%
% \section{User options}
%
% \DescribeEnv{11-12-curtos}
% \DescribeEnv{11-13-curtos}
% \DescribeEnv{11-13-longos}
%   For the ordinals of 11, 12 and 13 there are two correct forms each:\par
%   \begin{tabular}{cll}
%     Number & Shorter & Longer\\\hline
%     11 & und\'ecimo & d\'ecimo primeiro\\
%     12 & duod\'ecimo & d\'ecimo segundo\\
%     13 & tred\'ecimo & d\'ecimo terceiro
%   \end{tabular}\par\noindent
%   It is usual to find the shorter forms for 11\ordm\ and 12\ordm\ in
%   texts, including some grammars.  The shorter form for 13\ordm\ is
%   a bit harder to find.  It is kind of strange to see shorter for
%   11\ordm\ and longer for 12\ordm\ and the other way around, so it
%   should not happen.  If 13\ordm\ is presented in shorter form,
%   then definitely the shorter forms should be used for 11\ordm\ and
%   12\ordm.  Therefore we provide three package options
%   `\texttt{11-12-curtos}', `\texttt{11-13-curtos}' and
%   `\texttt{11-13-longos}' with the following results: \par
%   \begin{tabular}{llll}
%     Option & 11\ordm & 12\ordm & 13\ordm\\\hline 
%     \texttt{11-12-curtos} (default) & und\'ecimo & duod\'ecimo & d\'ecimo terceiro\\
%     \texttt{11-13-curtos} & und\'ecimo & duod\'ecimo & tred\'ecimo\\
%     \texttt{11-13-longos} & d\'ecimo primeiro & d\'ecimo segundo & d\'ecimo terceiro
%   \end{tabular}\par\noindent
%   \textit{Nota bene}: These options affect all ordinals that
%   finish in 11, 12 and 13.  For instance, with option
%   \texttt{11-12-curtos}, the ordinal for 112 becomes ``cent\'esimo
%   duod\'ecimo''. 
%
% \DescribeEnv{tricentesimo}
% \DescribeEnv{trecentesimo}
%   The options `\texttt{tricentesimo}' (default; closer to latin
%   \textit{tricentesimu}) and `\texttt{trecentesimo}' are provided
%   to choose between ``tricent\'esimo'' and ``trecent\'esimo'', since
%   both forms are correct for the ordinal of 300.\par
%   \begin{tabular}{ll}
%     Option & 300\ordm\\\hline
%     \texttt{tricentesimo} (default) & tricent\'esimo\\
%     \texttt{trecentesimo} & trecent\'esimo\\
%   \end{tabular}
%
% \DescribeEnv{sexcentesimo}
% \DescribeEnv{seiscentesimo}
%    The options `\texttt{sexcentesimo}' (default; closer to the latin
%    \textit{sexcentesimu}) and `\texttt{seiscentesimo}' are provided
%    to choose between ``sexcent\'esimo'' and ``seiscent\'esimo'',
%    since both forms are correct for the ordinal of 600.\par
%   \begin{tabular}{ll}
%     Option & 600\ordm\\\hline
%     \texttt{sexcentesimo} (default) & sexcent\'esimo\\
%     \texttt{seiscentesimo} & seiscent\'esimo\\
%   \end{tabular}
%
% \DescribeEnv{noningentesimo}
% \DescribeEnv{nongentesimo}
%    The options `\texttt{noningentesimo}' (default; from latin
%    \textit{noningentesimu}) and `\texttt{nongentesimo} (from latin
%    \textit{nongentesimu})' are provided to choose between
%    ``noningent\'esimo'' and ``nongent\'esimo'', since both forms are
%    correct for the ordinal of 900.\par
%   \begin{tabular}{ll}
%     Option & 900\ordm\\\hline
%     \texttt{noningentesimo} (default) & noningent\'esimo\\
%     \texttt{nongentesimo} & nongent\'esimo\\
%   \end{tabular}
%
% \section{Code}
%
% Identidication of the package.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ordinalpt}[2007/02/08 v2.1 Ordinal numbers in Portuguese]
%    \end{macrocode}
%
% \DescribeEnv{11-12-curtos}
% \DescribeEnv{11-13-curtos}
% \DescribeEnv{11-13-longos}
% Declaring options `\texttt{11-12-curtos}' (default) and
% `\texttt{11-13-curtos}' and 
% `\texttt{11-13-longos}', for the various situations for ordinals of
% 11, 12 and 13, as discussed before.
%    \begin{macrocode}
\newif\if@ordpt@twelveshort
\newif\if@ordpt@thirteenshort
\@ordpt@twelveshorttrue
\@ordpt@thirteenshortfalse
\DeclareOption{11-12-curtos}{%
  \@ordpt@twelveshorttrue
  \@ordpt@thirteenshortfalse}
\DeclareOption{11-13-curtos}{%
  \@ordpt@twelveshorttrue
  \@ordpt@thirteenshorttrue}
\DeclareOption{11-13-longos}{%
  \@ordpt@twelveshortfalse
  \@ordpt@thirteenshortfalse}
\ExecuteOptions{11-12-curtos}
%    \end{macrocode}
%
% \DescribeEnv{tricentesimo}
% \DescribeEnv{trecentesimo}
% Declaring options `\texttt{tricentesimo}' (default) and
% `\texttt{trecentesimo}', since both forms ``tricent\'esimo'' and
% ``trecent\'esimo'' are correct for the ordinal of 300.
%    \begin{macrocode}
\DeclareOption{tricentesimo}{\def\@ordpt@tricent{i}}
\DeclareOption{trecentesimo}{\def\@ordpt@tricent{e}}
\ExecuteOptions{tricentesimo}
%    \end{macrocode}
%
% \DescribeEnv{sexcentesimo}
% \DescribeEnv{seiscentesimo}
% Declaring options `\texttt{sexcentesimo}' (default; closer to the
% latin \textit{sexcentesimu}) and
% `\texttt{seiscentesimo}', since both forms ``sexcent\'esimo'' and
% ``seiscent\'esimo'' are correct for the ordinal of 600.
%    \begin{macrocode}
\DeclareOption{sexcentesimo}{\def\@ordpt@sexcent{x}}
\DeclareOption{seiscentesimo}{\def\@ordpt@sexcent{is}}
\ExecuteOptions{sexcentesimo}
%    \end{macrocode}
%
% \DescribeEnv{noningentesimo}
% \DescribeEnv{nongentesimo}
% Declaring options `\texttt{noningentesimo}' (default; from latin
% \textit{noningentesimu}) and `\texttt{nongentesimo} (from latin
% \textit{nongentesimu})', since both forms ``noningent\'esimo'' and
% ``nongent\'esimo'' are correct for the ordinal of 900.
%    \begin{macrocode}
\DeclareOption{noningentesimo}{\def\@ordpt@noningent{in}}
\DeclareOption{nongentesimo}{\def\@ordpt@noningent{}}
\ExecuteOptions{noningentesimo}
%    \end{macrocode}
%
%  Processing options.
%    \begin{macrocode}
\ProcessOptions
%    \end{macrocode}
%
% \DescribeMacro{\@ordpt@lowercase}
% \DescribeMacro{\@ordpt@capitalize}
% \DescribeMacro{\@ordpt@uppercase}
% Commands that deal with letter case.
%    \begin{macrocode}
\newcommand{\@ordpt@lowercase}[2]{#1#2}
\newcommand{\@ordpt@capitalize}[2]{\uppercase{#1}#2}
\newcommand{\@ordpt@uppercase}[2]{\uppercase{#1#2}}
%    \end{macrocode}
%
% \DescribeMacro{\@ordpt@printunits}
% \DescribeMacro{\@ordpt@printtens}
% \DescribeMacro{\@ordpt@printhundreds}
% \DescribeMacro{\@ordpt@printthousands}
%   These commands take as first and second parameters the command
%   that deals with lettercase (commands above) and a letter ``o'' or
%   ``a'' for the masculine or feminine genders.  The other parameters
%   are the digits necessary to print the number: one for numbers less
%   than 10, two for numbers between 10 and 99, etc.  This commands
%   also have to take care of spaces that separate the words.
%    \begin{macrocode}
\newcommand{\@ordpt@printunits}[3]{%
  \ifcase#3 \or
    #1{p}{rimeir#2}\or
    #1{s}{egund#2}\or
    #1{t}{erceir#2}\or
    #1{q}{uart#2}\or
    #1{q}{uint#2}\or
    #1{s}{ext#2}\or
    #1{s}{\'etim#2}\or
    #1{o}{itav#2}\or
    #1{n}{on#2}%
  \fi}
\newcommand{\@ordpt@printtens}[4]{%
  \ifx#31%
    \ifx#41%
      \if@ordpt@twelveshort
        #1{u}{nd\'ecim#2}%
      \else
        #1{d}{\'ecim#2}\space#1{p}{rimeir#2}%
      \fi
    \else
      \ifx#42%
        \if@ordpt@twelveshort
          #1{d}{uod\'ecim#2}%
        \else
          #1{d}{\'ecim#2}\space#1{s}{egund#2}%%
        \fi
      \else
        \ifx#43%
          \if@ordpt@thirteenshort
            #1{t}{red\'ecim#2}%
          \else
            #1{d}{\'ecim#2}\space#1{t}{erceir#2}%%
          \fi
        \else
          #1{d}{\'ecim#2}%
          \ifx#40%
          \else
            \space\@ordpt@printunits#1#2#4%
          \fi
        \fi
      \fi
    \fi
  \else
    \ifcase#3 \or\or
      #1{v}{ig\'esim#2}\or
      #1{t}{rig\'esim#2}\or
      #1{q}{uadrag\'esim#2}\or
      #1{q}{uinquag\'esim#2}\or
      #1{s}{exag\'esim#2}\or
      #1{s}{eptuag\'esim#2}\or
      #1{o}{ctog\'esim#2}\or
      #1{n}{onag\'esim#2}%
    \fi
    \ifx#40%
    \else
      \space\@ordpt@printunits#1#2#4%
    \fi
  \fi}
\newcommand{\@ordpt@printhundreds}[5]{%
  \ifcase#3 \or
    #1{c}{ent\'esim#2}\or
    #1{d}{ucent\'esim#2}\or
    #1{t}{r\@ordpt@tricent cent\'esim#2}\or
    #1{q}{uadringent\'esim#2}\or
    #1{q}{uingent\'esim#2}\or
    #1{s}{e\@ordpt@sexcent cent\'esim#2}\or
    #1{s}{eptingent\'esim#2}\or
    #1{o}{ctingent\'esim#2}\or
    #1{n}{on\@ordpt@noningent gent\'esim#2}%
  \fi
  \ifx#40%
    \ifx#50%
    \else
      \space\@ordpt@printunits#1#2#5%
    \fi
  \else
    \space\@ordpt@printtens#1#2#4#5%
  \fi}
\newcommand{\@ordpt@printthousands}[6]{%
  \ifcase#3 \or
    #1{m}{il\'esim#2}%
  \fi
  \ifx#40%
    \ifx#50%
      \ifx#60%
      \else
        \space\@ordpt@printunits#1#2#6%
      \fi
    \else
      \space\@ordpt@printtens#1#2#5#6%
    \fi
  \else
    \space\@ordpt@printhundreds#1#2#4#5#6
  \fi}
%    \end{macrocode}
%
% \DescribeMacro{\@ordpt@print}
%    The command +\@ordpt@print+ is defined with special arguments
%    (not possible to be defined with +\newcommand+).  The arguments
%    are expected as follows:\\
% +  #1+ lettercase command\\
% +  #2+ gender letter o or a\\
% +  #3+ digit\\
% +  #4+ digit or ? (only units)\\
% +  #5+ digit or ? (up to tens)\\
% +  #6+ digit or ? (up to hundreds)\\
% +  #7+ ? (up to thousands) or nothing
%    \begin{macrocode}
\newcommand{\@ordpt@print}{}
\def\@ordpt@print#1#2#3#4#5#6#7!{%
  \ifx#4?%
    \@ordpt@printunits#1#2#3%
  \else
    \ifx#5?%
      \@ordpt@printtens#1#2#3#4%
    \else
      \ifx#6?%
        \@ordpt@printhundreds#1#2#3#4#5%
      \else
        \@ordpt@printthousands#1#2#3#4#5#6%
      \fi
    \fi
  \fi}
%    \end{macrocode}
%
% \DescribeMacro{\@ordpt@ordinal}
%   In this command the value of the counter (third argument) is
%   expanded, via +\expandafter+, to a list of digits, and four ``?''
%   and a ``!'' are added and expected to be goobled by
%   +\@ordpt@print+.  We need the ``?'' because of the +\ifx+ on
%   detecting how many digits the number has.  This command checks if
%   the value of the counter is between 1 and 1999 (including both).
%   After the expansion of the +\the+, the command +\@ordpt@print+ is
%   used.
%    \begin{macrocode}
\newcommand{\@ordpt@ordinal}[3]{%
  \ifnum\csname c@#3\endcsname<1
    \@ctrerr
  \else
    \ifnum\csname c@#3\endcsname>1999
      \@ctrerr
    \else
      \expandafter\@ordpt@print
      \expandafter#1\expandafter#2\the\csname c@#3\endcsname????!%
    \fi
  \fi}
%    \end{macrocode}
%
% \DescribeMacro{\ordptmasc}
% \DescribeMacro{\Ordptmasc}
% \DescribeMacro{\ORDPTMASC}
% \DescribeMacro{\ordptfem}
% \DescribeMacro{\Ordptfem}
% \DescribeMacro{\ORDPTFEM}
%    The user commands.  The diference between them is that the
%    pass to +\@ordpt@ordinal+ the letter case commands as first
%    argument and the final gender vowel as second, and the counter
%    name as third argument.
%    \begin{macrocode}
\newcommand{\ordptmasc}[1]{\@ordpt@ordinal\@ordpt@lowercase o{#1}}
\newcommand{\Ordptmasc}[1]{\@ordpt@ordinal\@ordpt@capitalize o{#1}}
\newcommand{\ORDPTMASC}[1]{\@ordpt@ordinal\@ordpt@uppercase o{#1}}
\newcommand{\ordptfem}[1]{\@ordpt@ordinal\@ordpt@lowercase a{#1}}
\newcommand{\Ordptfem}[1]{\@ordpt@ordinal\@ordpt@capitalize a{#1}}
\newcommand{\ORDPTFEM}[1]{\@ordpt@ordinal\@ordpt@uppercase a{#1}}
%    \end{macrocode}
% \Finale \addcontentsline{toc}{section}{Index}\printindex %\PrintChanges
%
\endinput
%
%% \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         \~}