% \iffalse meta-comment
%
% This file is part of the babel-greek package.
% ---------------------------------------------
%
% It 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".
%
% The Current Maintainer of this work is Günter Milde <milde@users.sf.net>.
%
% \fi
%% \CheckSum{146}
%\iffalse
%
%% This is file `athnum.dtx'
%% (c) 1997-2007 Apostolos Syropoulos.
%% Changes in version 2 © 2022, 2023 Günter Milde
%% All rights reserved.
%\fi
%
% \iffalse
%    \begin{macrocode}
%<*driver>
\documentclass{ltxdoc}
\usepackage{hyperref}
\hypersetup{colorlinks=true,linkcolor=blue,urlcolor=blue,citecolor=black}
% auxiliary macro to keep the documentation independent of Greek text fonts
\def\PiIt#1{{%
    \newdimen\boxW \newdimen\boxH
    \settowidth{\boxW}{#1}%
    \settoheight{\boxH}{#1}%
    \addtolength{\boxW}{0.8pt}
    \vbox{%
    \hrule width\boxW\hbox{%
          \vrule height\boxH\mbox{#1}%
          \vrule height\boxH}}\kern.5pt}}
\GetFileInfo{athnum.drv}
\begin{document}
   \DocInput{athnum.dtx}
\end{document}
%</driver>
%    \end{macrocode}
% \fi
%%\title{Athenian (Attic) Numerals}
% \author{Apostolos Syropoulos}
% \date{2003/08/24}
%\maketitle
%
%\MakeShortVerb{\|}
%
%\section{Introduction}
%
% This \LaTeX\ package implements the macro
% \DescribeMacro{\athnum}
% |\athnum|.
% The macro transforms an Arabic numeral, i.e., the kind of numerals
% we all use (e.g., 1, 5, 789 etc), to the corresponding
% \href{https://en.wikipedia.org/wiki/Attic_numerals}{\emph{Athenian} numeral}.
% It can also be used to display counters.
% The package requires a Greek text font and
% the |greek-fontenc| package.\footnote{%
%   Up to version 1.1, `athnum' required that Babel was loaded with the
%   |greek| option. Version 2.0 sets up Greek font support autonomously.}
%
%\section{The Numbering System}
%
% The athenian numbering system, like the roman one, employs
% letters to denote important numbers. Multiple occurrence of a letter denote
% a multiple of the ``important'' number, e.g., the letter I denotes 1, so
% III denotes 3. Here are the basic digits used in the Athenian numbering
% system:
% \begin{itemize}
%  \item I denotes the number one (1)
%  \item $\Pi$ denotes the number five (5)
%  \item $\Delta$ denotes the number ten (10)
%  \item H denotes the number one hundred (100)
%  \item X denotes the number one thousand (1000)
%  \item M denotes the number ten thousands (10000)
%\end{itemize}
% Moreover,  the letters $\Delta$, H, X, and M under the letter $\Pi$,
% denote five times their original value, e.g., the symbol
% \PiIt{X}, denotes the number 5000, and the symbol
% \PiIt{$\Delta$}, denotes the number 50. It must be noted that
% the numbering system does not provide negative numerals or a symbol for
% zero.
%
% The Athenian numbering system is described, among others, in an article in
% Encyclopedia $\Delta o\mu\acute{\eta}$, Vol. 2, page 280, 7th edition,
% Athens, October 2, 1975.
%
% \section{The Code}
%
% Before we do anything further, we have to identify the package.
% \StopEventually
%
%    \begin{macrocode}
%<*package>
\NeedsTeXFormat{LaTeX2e}[1996/01/01]
\ProvidesPackage{athnum}[2023/03/17 v2.0.1 Athenian numerals]
%    \end{macrocode}
%
% \subsection{Font Setup}
%
% Typesetting Greek texts requires a font with Greek characters:
%
% For 8-bit LaTeX, this package uses fonts with the LGR font encoding.
% With the XeTeX or LuaTeX engines and Unicode fonts, the user must ensure
% that the selected font contains the required glyphs
% (only few fonts support the "GREEK ACROPHONIC ..." characters).
% LGR-encoded fonts can also be used alongside Unicode fonts with XeTeX/LuaTeX
% to enable the input of Greek letters via the Latin transliteration provided by
% this font encoding.
%
% \DescribeMacro\greekfontencoding
% We test for available font encodings and set |\greekfontencoding|
% and the internal macro |\athnum@greek@fontencdef| with the name of the
% font definition file that defines the required Greek LICR macros.
% The default is LGR for 8-bit TeX engines and TU for Xe/LuaTeX.
%    \begin{macrocode}
\ifdefined \UnicodeEncodingName % set by LaTeX for XeTeX/LuaTeX
  \providecommand*{\greekfontencoding}{\UnicodeEncodingName}
  \providecommand*{\athnum@greek@fontencdef}{tuenc-greek}
\else
  \providecommand*{\greekfontencoding}{LGR}
  \providecommand*{\athnum@greek@fontencdef}{lgrenc}
\fi
%    \end{macrocode}
% Ensure that the Greek LICR macros are available:
%    \begin{macrocode}
\@ifl@aded{def}{\athnum@greek@fontencdef}{}
{% else
  \InputIfFileExists{\athnum@greek@fontencdef .def}{}{% else
    \PackageError{athnum}{Font support for the Greek script missing.\\
               can't typeset Greek symbols.\\
               Install the "greek-fontenc" package.}
              {I can't find the \athnum@greek@fontencdef .def file
               for the Greek fonts (encoding \greekfontencoding).}
    \@@end
  }
}
%    \end{macrocode}
% \begin{macro}{\ensuregreek}
%   The TextCommand |\ensuregreek| sets it argument in |\greekfontencoding|
%   if the current font encoding does not provide a (typically empty) local
%   variant.
%    \begin{macrocode}
\ProvideTextCommandDefault{\ensuregreek}[1]{%
  \leavevmode{\fontencoding{\greekfontencoding}\selectfont #1}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Transform Arabic Numeral to Athenian Numeral}
%
%\begin{macro}{\@@athnum}
% Now, we turn our attention to the definition of the macro
% |\@@athnum|. This macro uses one integer variable (or counter in
% \TeX's jargon.)
%    \begin{macrocode}
\newcount\@ath@num
%    \end{macrocode}
% The macro |\@@athnum| is also defined as a robust command.
%    \begin{macrocode}
\DeclareRobustCommand*{\@@athnum}[1]{%
%    \end{macrocode}
% After assigning to variable |\@ath@num| the value of the macro's argument,
%we  make sure that the argument is in the expected range, i.e., it is greater
% than zero, and less or equal to $249999$.  In case it isn't, we simply
% produce a |\space|, warn the user about it and quit. Although, the
% |\athnum| macro is capable to produce an Athenian numeral for even greater
% intergers, the following argument by Claudio Beccari convised me to place
% this above upper limit:
% \begin{quote}
% According to psychological perception studies (that ancient Athenians
% and Romans perfectly knew without needing to study Freud and Jung)
% living beings (which includes at least all vertebrates, not only
% humans) can perceive up to four randomly set objects of the same kind
% without the need of counting, the latter activity being a specific
% acquired ability of human kind; the biquinary numbering notation
% used by the Athenians and the Romans exploits this natural
% characteristic of human beings.
% \end{quote}
%    \begin{macrocode}
        \@ath@num#1\relax
        \ifnum\@ath@num<\@ne%
          \space%
          \PackageWarning{athnum}{%
          Illegal value (\the\@ath@num) for athenian numeral}%
        \else\ifnum\@ath@num>249999%
          \space%
          \PackageWarning{athnum}{%
          Illegal value (\the\@ath@num) for athenian numeral}%
        \else
%    \end{macrocode}
% Having done all the necessary checks, we are now ready to do the actual
% computation. If the number is greater than $49999$, then it certainly
% has at least one \PiIt{M} ``digit''. We find all such digits by continuously
% subtracting $50000$ from |\@ath@num|, until |\@ath@num| becomes less than
% $50000$.
%    \begin{macrocode}
            \@whilenum\@ath@num>49999\do{%
               \textpentemuria\advance\@ath@num-50000}%
%    \end{macrocode}
% We now check for tens of thousands.
%    \begin{macrocode}
            \@whilenum\@ath@num>9999\do{%
               \textMu\advance\@ath@num-\@M}%
%    \end{macrocode}
% Since a number can have only one \PiIt{X} ``digit'' (equivalent to 5000), it
% is easy to check it out and produce the corresponding numeral in case it does
% have one.
%    \begin{macrocode}
            \ifnum\@ath@num>4999%
               \textpenteqilioi\advance\@ath@num-5000%
            \fi\relax
%    \end{macrocode}
% Next, we check for thousands, the same way we checked for tens of thousands.
%    \begin{macrocode}
            \@whilenum\@ath@num>999\do{%
               \textChi\advance\@ath@num-\@m}%
%    \end{macrocode}
% Like the five thousands, a numeral can have at most one \PiIt{H} ``digit''
% (equivalent to 500).
%    \begin{macrocode}
            \ifnum\@ath@num>499%
               \textpentehekaton\advance\@ath@num-500%
            \fi\relax
%    \end{macrocode}
% It is time to check hundreds, which follow the same pattern as thousands
%    \begin{macrocode}
            \@whilenum\@ath@num>99\do{%
               \textEta\advance\@ath@num-100}%
%    \end{macrocode}
% A numeral can have only one \PiIt{$\Delta$} ``digit'' (equivalent to 50).
%    \begin{macrocode}
            \ifnum\@ath@num>49%
               \textpentedeka\advance\@ath@num-50%
            \fi\relax
%    \end{macrocode}
% Let's check now decades.
%    \begin{macrocode}
            \@whilenum\@ath@num>9\do{%
               \textDelta\advance\@ath@num by-10}%
%    \end{macrocode}
% We check for five and, finally, for the digits 1, 2, 3, and 4.
%    \begin{macrocode}
            \@whilenum\@ath@num>4\do{%
               \textPi\advance\@ath@num-5}%
            \ifcase\@ath@num \or \textIota
	       \or \textIota\textIota
	       \or \textIota\textIota\textIota
	       \or \textIota\textIota\textIota\textIota\fi%
   \fi\fi}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\@athnum}
% The command |\@athnum| has one argument, which
% is a counter. It calls the command |\@@athnum| to process the value of
% the counter.
%    \begin{macrocode}
\def\@athnum#1{%
     \expandafter\@@athnum\expandafter{\the#1}}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\athnum}
% The command |\athnum| is a wrapper that declares a new counter
% in a local scope, assigns to it the argument of the command
% and calls the macro |\@athnum|.
% This way the command can process correctly either a number or a counter.
% It also ensures that the argument is set with a Greek text font.
%    \begin{macrocode}
\def\athnum#1{%
     \textrm{\ensuregreek{%
       \@ath@num#1\relax
       \@athnum{\@ath@num}}%
     }}
%</package>
%    \end{macrocode}
%\end{macro}
%
% \section{Changes}
% \begin{description}
%   \item [athnum-2.0 2022/12/08]
%     Do not require Babel.
%     Support Unicode fonts.
%     Drop the backwards compatibility definitions
%     for |\PiDelta|, |\PiEta|, |\PiChi|, and |\PiMu|.
%   \item [athnum-2.0.1 2023/03/17]
%     Fix behaviour in math mode.
% \end{description}
%
% \section*{Acknowledgment}
% I would like to thank Claudio Beccari for reading the documentation
% and for his very helpful suggestions. In addition, Antonis Tsolomitis
% spotted a bug in the first version, which is corrected in the present
% version.
% \section*{Dedication}
% I would like to dedicate this piece of work to my son
% \begin{center}Demetrios-Georgios.\end{center}
%
%
% \Finale
%
\endinput
