% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \iffalse meta-comment
%
% DocStrip file for ionumbers package
%
% Copyright 2007-2009 Christian Schneider <software(at)chschneider(dot)eu>
%
% idea and parts of this package based on: ziffer.sty v2.1
% Copyright Martin Vaeth <vaeth@mathematik.uni-wuerzburg.de>
%
% ion@addto@macro: identical to l@addto@macro from koma-script
%                  bundle v2.9t
% Copyright 1994-2004 Markus Kohm and Frank Neukam
%
% Thanks to Martin Vaeth and Markus Kohm for permitting to use their
% code in this package. Please do not bother Martin, Markus, or Frank
% with questions or bugs concering this package!
%
% This file is part of ionumbers.
%
% ionumbers is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation, not any later version.
%
% ionumbers is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with ionumbers.  If not, see <http://www.gnu.org/licenses/>.
%
% WARNING: THIS IS ALPHA SOFTWARE AND MAY CONTAIN SERIOUS BUGS!
%
% \fi
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{ionumbers}
%<package>  [2009/07/18 v0.2.4-alpha restyle numbers in math mode]
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{ionumbers}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%
% modified description environment
\newcommand{\listmark}{$\bullet$}
\newcommand{\listlengths}{%
  \setlength{\parsep}{0pt}%
  \setlength{\itemsep}{0pt}%
  \setlength{\leftmargin}{10.5em}%
  \setlength{\rightmargin}{0pt}%
  \setlength{\labelsep}{1em}%
  \setlength{\labelwidth}{9.5em}}
\newenvironment{mydescription}{%
  \begin{list}{\listmark}{\listlengths}%
}{%
  \end{list}%
}
%
\begin{document}
  \DocInput{ionumbers.dtx}
\end{document}
%</driver>
% \fi
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \changes{v0.2.0-alpha}{2008/10/27}{initial .dtx version}
% \changes{v0.2.1-alpha}{2008/11/02}{replaced website by e-mail address in all
%   fields containing contact information}
% \changes{v0.2.2-alpha}{2008/12/21}{fixed problem with single digit numbers
%   without \{\}-grouping; conflict with \textsf{amsmath}/\textsf{amsopn}
%   documented and handled with warning messages; thanks to Robert N\"{u}rnberg
%   for reporting these two problems}
% \changes{v0.2.3-alpha}{2008/12/21}{saved one \texttt{\textbackslash if} and
%   made sign/digit macros a bit clearer}
% \changes{v0.2.4-alpha}{2009/07/18}{fixed bug with curly braces in
%   \texttt{\textbackslash ion@problem@package} macro; thanks to Lars for
%   reporting this problem}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% %\CheckSum{0}
% \CheckSum{1192}
%
% \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{ionumbers.sty}
%
% \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
% \DoNotIndex{\@ne}
% \DoNotIndex{\advance,\begingroup,\catcode,\closein}
% \DoNotIndex{\closeout,\day,\def,\edef,\else,\empty,\endgroup}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \title{The \textsf{ionumbers} package\thanks{This document corresponds
%   to \textsf{ionumbers}~\fileversion, dated~\filedate. Copyright
%   2007-2009 Christian Schneider \texttt{<software(at)chschneider(dot)eu>}.}}
% \author{Christian Schneider\\ \texttt{<software(at)chschneider(dot)eu>}}
%
% \maketitle
%
% \begin{center}
%   \large\textbf{\textsf{Warning: This is alpha software and may
%   contain serious bugs! Use with caution and on your own risk! Check output!}}
% \end{center}
%
% \tableofcontents
% \clearpage
%
% \begin{abstract}
%   \textsf{ionumbers} stands for `input/output numbers'.
%
%   This package restyles numbers in math mode. If a number in the input file
%   is written, e.g., as |$3,231.44$| as commonly used in English texts, this
%   package is able to restyle it to be output as `$3\,231{,}44$' as commonly
%   used in German texts (and vice versa). This may be very useful, if you have
%   a large table and want to include it in texts with different output
%   conventions without the need of changing the table.
%
%   Furthermore this package can automatically group digits left to the decimal
%   separator (\emph{thousands}) and right to the decimal separator
%   (\emph{thousandths}) in triplets without the need of specifing commas
%   (English) or points (German) as separators. E.g., the input |$1234.567890$|
%   can be output as `$1\,234.\,567\,890$'.
%
%   Finally, an |e| starts the exponent of the number. For example, |$21e6$|
%   may be output as `$26\times10\,^{6}$'.
% \end{abstract}
%
% \section{Details of number handling}
%
% \subsection{General rules}
%
% Every input \emph{in math mode} consisting of the following characters is
% treated by this package: |.,+-0123456789| These characters get macro
% definitions. A number is any combination of these characters without
% anything---not even white spaces---in between them. There are two
% exceptions/special cases:
%
% \begin{enumerate}
%   \item The separator characters |.| and |,| are not treated as part of the
%     number at its end. This avoids problems with lists like
%     |1, 2, 3, \ldots|, where the commas are not part of in the numbers. Note,
%     however, that the commas are treated as part of the numbers in the first
%     two appearances in |1,2,3,\ldots|, as the commas are immediately followed
%     by a digit. Please input lists with white spaces after the separators as
%     shown in the first case.
%   \item The sign characters |+| and |-| will only be considered as part of
%     the number, if they appear at the begining of a number.
% \end{enumerate}
%
% The lower case letter |e| plays a special role. An |e| following immediatly a
% number (without any white space or other input in between) can be configured
% as begining of the exponential part. The letter |e| will be eaten from the
% input in this case and substituted by some configurable output. (It is
% virtually impossible to handle it the same way as the other and assigning a
% macro definition to |e|.)  In this case the number following an |e| in the
% same group will be grouped with curly braces |{}|. This is important to
% understand, e.\,g., the spacing in cases when that number begins with a sign.
%
% \subsection{Caveats}
%
% Please be aware that the first decimal separator of a number marks the
% begining of the thousandths part of a number; every part of a number
% appearing left to the first decimal separator is the thousands part. That is
% why, the input |$1.234.567$| with (only) the package option
% |autothousandths=true| (|.| is the decimal separator; option will be
% explained later) will lead to `$1.234.\,567$' in the output. Note the small
% space after the second point as a result of |234.567| being treated as
% thousandths part. The thousandths separator---by default a small space---will
% be output between the third and fourth digit of the thousandths part; the
% additional point from the input will not be omitted. The input is
% syntactically incorrect (there must not be two decimal separators in one
% number) and the output is \emph{not} a bug.
%
% A number following an |e| treated as exponential part of a number may be
% typeset as superscript (depending on the configuration). In the case of an
% exponential part there \emph{may} be arbitrary input between |e| and the
% number in the exponent. Especially, the input |$1e \Pi 2| with package option
% |exponent=timestento| (will be explained later) leads to a superscript
% |2| in the output. In some cases, e.\,g., |$1e \sqrt 2$| or |$1e^2$| with
% |e| configured as begining of the exponential part, even an error occurs.
% Again, the input is syntactically incorrect and there is no easy way to
% circumvent |e| from being treated as begining of the exponential part in these
% cases (at least, I did not find it).
%
% In some rare cases, e.\,g., |$\sqrt ,$| or |$a^.$|, the usage of point and
% comma without curly braces |{}| around them will lead to an error. In these
% cases please add curly braces |{}| around the point or comma. (The
% \textsf{ziffer} package has the same problem.)
%
% If you use, e.\,g., indexes consisting of four or more digits together with
% automatic grouping of thousands, the grouping will also apply to the
% indexes. So |a_{1234}| might be output as $a_{1,234}$. Possibilities to
% prevent undesired automatic grouping are on the one hand input as
% |a_{1 2 3 4}| (with a space after at least every third digit) or on the other
% hand selectively switching of automatic grouping (see below).
%
% \section{Conflicts with other packages}
%
% This package potentially conflicts with any other package that defines a
% macro for any of the following characters: |.,+-0123456789|
%
% There are tests for these cases and warning or error messages may be output.
% Please load \textsf{ionumbers} as \emph{last package} to be able to detect as
% many conflicts as possible. As there is no way to detect conflicts in any
% case, please report any package known to conflict with \textsf{ionumbers} to
% the author.
%
% Packages known to conflict with \textsf{ionumbers} are:
% \begin{mydescription}
%   \item[\textsf{ziffer}] this package can be replaced by \textsf{ionumbers}
%     except for \textsf{ziffer}'s special handling of |--| enabled by
%     |\ZifferStrichAn|
%   \item[\textsf{dcolumn}] workaround: disable \textsf{ionumbers} for tabulars
%     (e.\,g., put them inside |\ionumbersoff|\marg{\ldots})
%   \item[\textsf{amsmath}/\textsf{amsopn}] load \textsf{ionumbers} as last
%     package and disable \textsf{ionumbers} for |\operatorname|\marg{\ldots}
%     (e.\,g., put it inside\\
%     |\ionumbersoff|\marg{\ldots})
% \end{mydescription}
%
% \section{Usage}
%
% Package options are used to globally configure a default behaviour of
% \textsf{ionumbers} for the whole document. These options usually consist
% of a \meta{key}|=|\meta{value} pair. Local changes from this global
% configuration for arbitrary parts of the document can be applied with special
% commands.
%
% \subsection{Package options concerning the separators in the input}
% \label{sec:in}
%
% The following options configure the meaning of separators in the \LaTeX{}
% input file:
% \begin{mydescription}
%   \item[\texttt{comma}=\meta{value}] comma `|,|' will be treated as
%     \meta{value}
%   \item[\texttt{point}=\meta{value}] point `|.|' will be treated as
%     \meta{value}
% \end{mydescription}
%
% The following \meta{value}s can be chosen for both of them:
% \begin{mydescription}
%   \item[\texttt{ignore}] the separator will be ignored (no output)
%   \item[\texttt{decimal}] decimal separator (separating the thousands from
%     the thousandths part of a number)
%   \item[\texttt{thousands}] thousands separator (used for grouping of
%     thousands part)
%   \item[\texttt{default}] default behaviour of \textsf{ionumbers}
%     (|decimal| for |point|; |thousands| for |comma|)
% \end{mydescription}
%
% The separator for exponents is always the lowercase letter |e|. A
% thousandths separator does not exist in input files; such a separator will
% only be output, if automatic grouping of the thousandths part is enabled (see
% below).
%
% \subsection{Package options concerning the separators in the output}
% \label{sec:out}
%
% The previously described options assign a \emph{meaning} to separators in
% the input file. The \emph{output} of the \emph{meanings} is configured via
% the following options:
% \begin{mydescription}
%   \item[\texttt{thousands}=\meta{value}] thousands separator will be output as
%     \meta{value}
%   \item[\texttt{decimal}=\meta{value}] decimal separator will be output as
%     \meta{value}
%   \item[\texttt{thousandths}=\meta{value}] thousandths separator will be
%     \meta{value}
%   \item[\texttt{exponent}=\meta{value}] exponent separator will be output as
%     \meta{value}
% \end{mydescription}
%
% The list of valid \meta{value}s for |thousands|, |decimal|, and |thousandths|
% is:
% \begin{mydescription}
%   \item[\texttt{none}] will be ignored (no output)
%   \item[\texttt{point}] normal point; this is the default point without
%     \textsf{ionumbers}
%   \item[\texttt{comma}] normal comma
%   \item[\texttt{punctpoint}] punctuation point (point followed by small space)
%   \item[\texttt{punctcomma}] punctuation comma (point followed by small
%     space); this is the default comma without \textsf{ionumbers}
%   \item[\texttt{apostrophe}] apostrophe (actually |$^\prime$|; \emph{not} for
%     |decimal|)
%   \item[\texttt{phantom}] space with width of a point (|$\phantom{.}$|;
%     \emph{not} for |decimal|)
%   \item[\texttt{space}] small space (|$\,$|; \emph{not} for |decimal|)
%   \item[\texttt{default}] default behaviour of \textsf{ionumbers}
%     (|punctcomma| for |thousands|; |point| for |decimal|; |space| for
%    |thousandths|)
% \end{mydescription}
%
% If a number is handled as exponent, it will be put into curly braces |{}|
% for correct output of, e.\,g., signs without spacing around them (mathord).
% In the following list of valid \meta{value}s for |exponent| a number
% immediatly following an |e| will be handled as exponent, unless specified
% otherwise:
% \begin{mydescription}
%   \item[\texttt{none}] will be ignored (not output; following number
%     \emph{not} handled as exponent)
%   \item[\texttt{original}] a simple character `e' (following number
%     \emph{not} handled as exponent)
%   \item[\texttt{ite/itE}] italic lower/upper case letter `e'
%   \item[\texttt{rme/rmE}] roman lower/upper case letter `e'
%   \item[\texttt{timestento}] |$\times10\,$| with following number output as
%     superscript
%   \item[\texttt{cdottento}] |$\cdot10\,$| with following number output as
%     superscript
%   \item[\texttt{wedge}] |$^\wedge$|
%   \item[\texttt{default}] default behaviour of \textsf{ionumbers} (|original|)
% \end{mydescription}
%
% \subsection{Package options concerning automatic grouping}
% \label{sec:auto}
%
% Automatic grouping is a feature that automatically adds the thousands and
% thousandths separator, respectively. The separator will be added after each
% triplet of digits. Automatic grouping can be enable or disabled with the
% following options:
% \begin{mydescription}
%   \item[\texttt{autothousands}=\meta{value}] automatic grouping of thousands
%     (digits left to decimal separator)
%   \item[\texttt{autothousandths}=\meta{value}] automatic grouping of
%     thousandths (digits right to decimal separator)
% \end{mydescription}
%
% The available \meta{value}s are |true| and |false| (default).
%
% Notes on automatic grouping:
% \begin{enumerate}
%   \item Grouping of thousandths requires |autothousandths=true| in any case,
%     as there is no thousandths separator for explicitly specifing separations
%     in the input.
%   \item Automatic grouping of thousands will be skipped in a number, if it
%     contains a thousands separator in the input.
% \end{enumerate}
%
% \subsection{Local style changes}
%
% \DescribeMacro{\ionumbersstyle}
% The command |\ionumbersstyle|\marg{option list} changes the global style
% definitions as specified as package options for the rest of the group.
% The \meta{option list} may contain any of the package options described in
% sections~\ref{sec:in}--\ref{sec:auto}. An additional \meta{value} for all
% \meta{key}s is available inside |\ionumbersstyle| to switch back to the
% configuration specified as package options: |reset|.
%
% \DescribeMacro{\ionumbersresetstyle}
% The command |\ionumbersresetstyle| resets all \meta{value}s to the
% configuration specified as package options. Actually, it is only a shorthand
% for\\
% |\ionumbersstyle{comma=reset,point=reset,decimal=reset,...}|.
%
% \subsection{User-defined values for output separators}
% \label{sec:defs}
%
% A user may specify further output separators. Any user-defined \meta{value}s
% for |thousands|, |decimal|, |thousandths|, and |exponent| can be used like
% the built-in options in section~\ref{sec:out}.
%
% \DescribeMacro{\newionumbersthousands}
% \DescribeMacro{\newionumbersdecimal}
% \DescribeMacro{\newionumbersthousandths}
% \DescribeMacro{\newionumbersexponent}
% The command |\newionumbersthousands|\marg{value}\marg{definition} has two
% mandatory arguments. The first one is the name of the newly defined
% \meta{value} for the |thousands| \meta{key} and the second one its
% definition. The commands |\newionumbersdecimal|, |\newionumbersthousandths|,
% and |\newionumbersexponent| work the same way for the |decimal|,
% |thousandths|, and |exponent| \meta{key}, respectively. There is a starred
% version of |\newionumbersexponent| (called |\newionumbersexponent*|) that
% typesets the following number as superscript.
%
% \DescribeMacro{\renewionumbersthousands}
% \DescribeMacro{\renewionumbersdecimal}
% \DescribeMacro{\renewionumbersthousandths}
% \DescribeMacro{\renewionumbersexponent}
% To redefine an existing \meta{key} definition there are |\renew...| versions
% of the previously described commands.
%
% Notes on definitions:
% \begin{enumerate}
%   \item All \meta{definition}s are set inside |\ionumbersoff| (see
%     section~\ref{sec:feat}). This means that numbers appearing in the
%     \meta{definition}s are not treated by this package.
%   \item The value |curr| has an internal meaning and should \emph{not} be
%     defined/redefined by the user.
% \end{enumerate}
%
% \subsection{Enabling and disabling features}
% \label{sec:feat}
%
% \DescribeMacro{\ionumbers}
% The command |\ionumbers| makes comma, point, signs, and digits active in math
% mode. This is equivalent to enabling the features of this package. This
% command applies to the end of the current group.
%
% \DescribeMacro{\endionumbers}
% To disable the features by making comma, point, signs, and digits inactive
% again the command |\endionumbers| can be used. This command applies to the
% end of the current group.
%
% \DescribeMacro{\ionumbersoff}
% The command |\ionumbersoff|\marg{stuff} disables the features only for
% \meta{stuff}.
%
% \section{License}
%
% \textsf{ionumbers} is free software: you can redistribute it and/or
% modify it under the terms of the GNU General Public License version~3 as
% published by the Free Software Foundation, not any later version.
%
% \textsf{ionumbers} is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details.
%
% You should have received a copy of the GNU General Public License along with
% ionumbers.  If not, see \texttt{<http://www.gnu.org/licenses/>}.
%
% \section{Acknowledgements}
%
% The idea and parts of this package are based on \textsf{ziffer.sty}~v2.1 by
% Martin V\"ath \texttt{<vaeth@mathematik.uni-wuerzburg.de>}.
%
% Furthermore the |\l@addto@macro| (with changed name) from
% \textsf{koma-script bundle}~v2.9t by Markus Kohm and Frank Neukam is
% used in this package.
%
% Thanks to Martin V\"ath and Markus Kohm for permitting to use their code in
% this package.
%
% \section{Bugs, problems, and suggestions}
%
% Please report bugs and problems or send suggestions for this package to
% Christian Schneider. Check for updates before reporting bugs at the website
% mentioned above. Do \emph{not} bother Martin V\"ath, Markus Kohm, or Frank
% Neukam with bugs, problems or suggestions concerning this package!
%
% \StopEventually{\PrintChanges\PrintIndex}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \section{Implementation}
%
% The implementation is briefly described in this section. First of all, we
% need the \textsf{keyval} package for \meta{key}|=|\meta{value} options:
%    \begin{macrocode}
\RequirePackage{keyval}
%    \end{macrocode}
%
% ---------------------------------------------------------------------
%
% \subsection{Default/global configuration}
%
% In principle the definitions of all available \meta{key'}|=|\meta{value'}
% pairs is contained in the internal macros |\ion@|\meta{key'}|@|\meta{value'}.
% Setting a package option \meta{key}|=|\meta{value} defines
% |\ion@|\meta{key}|@reset| to be |\ion@|\meta{key}|@|\meta{value}.
%
% The following |if|s will be required to remember, if automatic grouping is
% enabled.
%    \begin{macrocode}
\newif\ifion@autothousands
\newif\ifion@autothousandths
%    \end{macrocode}
%
% These shorthands are used to define the \meta{key}s for package options
% and set their \meta{value}s using \textsf{keyval}, respectively.
%    \begin{macrocode}
\newcommand*\ion@defpackopts{\define@key{ion@packopts}}
\newcommand*\ion@setpackopts{\setkeys{ion@packopts}}
%    \end{macrocode}
%
% Next the \meta{key}s are defined.
%    \begin{macrocode}
\ion@defpackopts{comma}{%
  \def\ion@comma@reset{\csname ion@comma@#1\endcsname}%
  \def\ion@aftercomma@reset{\csname ion@aftercomma@#1\endcsname}}
\ion@defpackopts{point}{%
  \def\ion@point@reset{\csname ion@point@#1\endcsname}%
  \def\ion@afterpoint@reset{\csname ion@afterpoint@#1\endcsname}}
\ion@defpackopts{decimal}{\def\ion@decimal@reset{%
  \csname ion@decimal@#1\endcsname}}
\ion@defpackopts{thousands}{\def\ion@thousands@reset{%
  \csname ion@thousands@#1\endcsname}}
\ion@defpackopts{thousandths}{\def\ion@thousandths@reset{%
  \csname ion@thousandths@#1\endcsname}}
\ion@defpackopts{exponent}{\def\ion@exponent@reset{%
  \csname ion@exponent@#1\endcsname}}
\ion@defpackopts{autothousands}[true]{\def\ion@autothousandsreset{%
  \csname ion@autothousands#1\endcsname}\ion@autothousandsreset}
\ion@defpackopts{autothousandths}[true]{\def\ion@autothousandthsreset{%
  \csname ion@autothousandths#1\endcsname}\ion@autothousandthsreset}
%    \end{macrocode}
%
% Finally, the default \meta{value}s are set and---if specified by the user as
% package option---overwritten with the user's configuration.
%    \begin{macrocode}
\ion@setpackopts{comma=default,point=default,thousands=default,%
  decimal=default,thousandths=default,exponent=default,autothousands=false,%
  autothousandths=false}
\DeclareOption*{\expandafter\ion@setpackopts\expandafter{\CurrentOption}}
\ProcessOptions\relax
%    \end{macrocode}
%
% ---------------------------------------------------------------------
%
% \subsection{Local style changes}
%
% The currently active configuration of a \meta{key} is stored in the macro
% |\ion@|\meta{key}|@curr|. The |\ion@|\meta{key}|@curr| macros for all
% \meta{key}s are defined using the mechanism for local configuration changes.
%
% The local options are defined and set---analogous to the package option
% case---with two shorthands using \textsf{keyval}. The latter is
% publically available to the user.
%    \begin{macrocode}
\newcommand*\ion@deflocopts{\define@key{ion@locopts}}
%    \end{macrocode}
% \begin{macro}{\ionumberstyle}
%    \begin{macrocode}
\newcommand*\ionumbersstyle[1]{\setkeys{ion@locopts}{#1}}
%    \end{macrocode}
% \end{macro}
%
% Now the \meta{key}s for the local options are defined (just as in the case of
% the package options):
%    \begin{macrocode}
\ion@deflocopts{comma}{%
  \def\ion@comma@curr{\csname ion@comma@#1\endcsname}%
  \def\ion@aftercomma@curr{\csname ion@aftercomma@#1\endcsname}}
\ion@deflocopts{point}{%
  \def\ion@point@curr{\csname ion@point@#1\endcsname}%
  \def\ion@afterpoint@curr{\csname ion@afterpoint@#1\endcsname}}
\ion@deflocopts{decimal}{\def\ion@decimal@curr{%
  \csname ion@decimal@#1\endcsname}}
\ion@deflocopts{thousands}{\def\ion@thousands@curr{%
  \csname ion@thousands@#1\endcsname}}
\ion@deflocopts{thousandths}{\def\ion@thousandths@curr{%
  \csname ion@thousandths@#1\endcsname}}
\ion@deflocopts{exponent}{\def\ion@exponent@curr{%
  \csname ion@exponent@#1\endcsname}}
\ion@deflocopts{autothousands}[true]{\csname ion@autothousands#1\endcsname}
\ion@deflocopts{autothousandths}[true]{\csname ion@autothousandths#1\endcsname}
%    \end{macrocode}
%
% Finally, the command for resetting all \meta{key}s is defined.
% \begin{macro}{\ionumbersresetstyle}
%    \begin{macrocode}
\newcommand*\ionumbersresetstyle{%
  \ionumbersstyle{comma=reset,point=reset,thousands=reset,%
    decimal=reset,thousandths=reset,exponent=reset,autothousands=reset,%
    autothousandths=reset}}
%    \end{macrocode}
% \end{macro}
% This command is issued at the end of the package to make the configuration
% of the package options active (and have no undefined |\ion@|\meta{key}|@curr|
% macros).
%    \begin{macrocode}
\AtEndOfPackage{\ionumbersresetstyle}
%    \end{macrocode}
%
% ---------------------------------------------------------------------
%
% \subsection{User-defined values for output separators}
%
% The commands for user-defined \meta{value}s for output separators just
% (re)define the internal macro |\ion@|\meta{key}|@|\meta{value} storing
% the definition for the \meta{key}|=|\meta{value} pair.
%
% \begin{macro}{\newionumbersthousands}
%    \begin{macrocode}
\newcommand*\newionumbersthousands[2]{\expandafter\newcommand%
  \expandafter*\csname ion@thousands@#1\endcsname{\ionumbersoff{#2}}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\newionumbersdecimal}
%    \begin{macrocode}
\newcommand*\newionumbersdecimal[2]{\expandafter\newcommand%
  \expandafter*\csname ion@decimal@#1\endcsname{\ionumbersoff{#2}}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\newionumbersthousandths}
%    \begin{macrocode}
\newcommand*\newionumbersthousandths[2]{\expandafter\newcommand%
  \expandafter*\csname ion@thousandths@#1\endcsname{\ionumbersoff{#2}}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\newionumbersexponent}
%    \begin{macrocode}
\newcommand*\newionumbersexponent{%
  \@ifstar{\newionumbersexponent@@}{\newionumbersexponent@}}
\newcommand*\newionumbersexponent@[2]{\expandafter\newcommand%
  \expandafter*\csname ion@exponent@#1\endcsname{\ionumbersoff{#2}}}
\newcommand*\newionumbersexponent@@[2]{\expandafter\newcommand%
  \expandafter*\csname ion@exponent@#1\endcsname{\ionumbersoff{#2}%
  \ion@exponent@superscripttrue}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\renewionumbersthousands}
%    \begin{macrocode}
\newcommand*\renewionumbersthousands[2]{\expandafter\renewcommand%
  \expandafter*\csname ion@thousands@#1\endcsname{\ionumbersoff{#2}}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\renewionumbersdecimal}
%    \begin{macrocode}
\newcommand*\renewionumbersdecimal[2]{\expandafter\renewcommand%
  \expandafter*\csname ion@decimal@#1\endcsname{\ionumbersoff{#2}}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\renewionumbersthousandths}
%    \begin{macrocode}
\newcommand*\renewionumbersthousandths[2]{\expandafter\renewcommand%
  \expandafter*\csname ion@thousandths@#1\endcsname{\ionumbersoff{#2}}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\renewionumbersexponent}
%    \begin{macrocode}
\newcommand*\renewionumbersexponent{%
  \@ifstar{\renewionumbersexponent@@}{\renewionumbersexponent@}}
\newcommand*\renewionumbersexponent@[2]{\expandafter\renewcommand%
  \expandafter*\csname ion@exponent@#1\endcsname{\ionumbersoff{#2}%
  \ion@currnum@exponent}}
\newcommand*\renewionumbersexponent@@[2]{\expandafter\renewcommand%
  \expandafter*\csname ion@exponent@#1\endcsname{\ionumbersoff{#2}%
  \ion@currnum@exponent\ion@exponent@superscripttrue}}
%    \end{macrocode}
% \end{macro}
%
% ---------------------------------------------------------------------
%
% \subsection{Internal macros holding definitions for \meta{key}=\meta{value}
% pairs}
%
% First of all, macros with the original character definitions are defined.
%    \begin{macrocode}
\mathchardef\ion@point@original="013A
\mathchardef\ion@comma@original="613B
\mathchardef\ion@plus@original="202B
\mathchardef\ion@minus@original="2200
\mathchardef\ion@zero@original="7030
\mathchardef\ion@one@original="7031
\mathchardef\ion@two@original="7032
\mathchardef\ion@three@original="7033
\mathchardef\ion@four@original="7034
\mathchardef\ion@five@original="7035
\mathchardef\ion@six@original="7036
\mathchardef\ion@seven@original="7037
\mathchardef\ion@eight@original="7038
\mathchardef\ion@nine@original="7039
\mathchardef\ion@e@original="7165
%    \end{macrocode}
%
% Here the |\ion@|\meta{key}|@|\meta{value} macros are defined, begining with
% the definitions for the comma as input separator.
%    \begin{macrocode}
\def\ion@comma@ignore{}
\def\ion@comma@decimal{\ion@decimal@curr}
\def\ion@comma@thousands{\ion@thousands@curr}
\def\ion@comma@default{\ion@comma@thousands}
%    \end{macrocode}
% The macros |\ion@comma@|\meta{value} contain the output for a comma appearing
% in the input. Actually, a second set of |\ion@|aftercomma|@|\meta{value}
% macros is required containing commands to be issued whenever a comma appears.
% If comma is the decimal separator, the appearance of comma in the input will
% mean that input of the thousands part is complete and the thousandths
% thousandths part starts (|\ion@beforedecimalfalse| must be issued). If comma
% is the thousands separator, the automatic grouping of thousands will be
% switched of for that number (|\ion@noexplicitthousandsfalse| must be issued).
%    \begin{macrocode}
\def\ion@aftercomma@ignore{}
\def\ion@aftercomma@decimal{\ion@beforedecimalfalse}
\def\ion@aftercomma@thousands{\ion@noexplicitthousandsfalse}
\def\ion@aftercomma@default{\ion@aftercomma@thousands}
%    \end{macrocode}
%
% An analogous set of macros is defined for the point as input separator.
%    \begin{macrocode}
\def\ion@point@ignore{}
\def\ion@point@decimal{\ion@decimal@curr}
\def\ion@point@thousands{\ion@thousands@curr}
\def\ion@point@default{\ion@point@decimal}
%    \end{macrocode}
% For the same reasons as mentioned before a set of
% |\ion@|afterpoint|@|\meta{value} macros is required.
%    \begin{macrocode}
\def\ion@afterpoint@ignore{}
\def\ion@afterpoint@decimal{\ion@beforedecimalfalse}
\def\ion@afterpoint@thousands{\ion@noexplicitthousandsfalse}
\def\ion@afterpoint@default{\ion@afterpoint@decimal}
%    \end{macrocode}
%
% Next the definitions for the decimal output separator, \ldots
%    \begin{macrocode}
\mathchardef\ion@decimal@point="013A
\mathchardef\ion@decimal@comma="013B
\mathchardef\ion@decimal@punctpoint="613A
\mathchardef\ion@decimal@punctcomma="613B
\def\ion@decimal@default{\ion@decimal@point}
%    \end{macrocode}
%
% \ldots{} the thousands output separator, \ldots
%    \begin{macrocode}
\def\ion@thousands@none{}
\mathchardef\ion@thousands@comma="013B
\mathchardef\ion@thousands@point="013A
\mathchardef\ion@thousands@punctcomma="613B
\mathchardef\ion@thousands@punctpoint="613A
\def\ion@thousands@apostrophe{^\prime}
\def\ion@thousands@phantom{\phantom{\ion@point@original}}
\def\ion@thousands@space{\,}
\def\ion@thousands@default{\ion@thousands@punctcomma}
%    \end{macrocode}
%
% \ldots{} the thousandths output separator, \ldots
%    \begin{macrocode}
\def\ion@thousandths@none{}
\mathchardef\ion@thousandths@comma="013B
\mathchardef\ion@thousandths@point="013A
\mathchardef\ion@thousandths@punctcomma="613B
\mathchardef\ion@thousandths@punctpoint="613A
\def\ion@thousandths@apostrophe{^\prime}
\def\ion@thousandths@phantom{\phantom{\ion@point@original}}
\def\ion@thousandths@space{\,}
\def\ion@thousandths@default{\ion@thousandths@space}
%    \end{macrocode}
%
% \ldots{} and the exponent output separator are given.
%    \begin{macrocode}
\def\ion@exponent@none{}
\def\ion@exponent@original{\ion@e@original}
\def\ion@exponent@ite{\mathchar"7165\ion@currnum@exponenttrue}
\def\ion@exponent@itE{\mathchar"7145\ion@currnum@exponenttrue}
\def\ion@exponent@rme{\mathchar"7065\ion@currnum@exponenttrue}
\def\ion@exponent@rmE{\mathchar"7045\ion@currnum@exponenttrue}
\def\ion@exponent@timestento{\times10\,\ion@currnum@exponenttrue%
  \ion@exponent@superscripttrue}
\def\ion@exponent@cdottento{\cdot10\,\ion@currnum@exponenttrue%
  \ion@exponent@superscripttrue}
\def\ion@exponent@wedge{^\wedge\ion@currnum@exponenttrue}
\def\ion@exponent@default{\ion@exponent@original}
%    \end{macrocode}
%
% ---------------------------------------------------------------------
%
% \subsection{Enabling and disabling features}
%
% The following helper macros make different subsets of |.,+-0123456789|
% active.
%    \begin{macrocode}
\def\ion@separators@active{\catcode`\,=\active\catcode`\.=\active\relax}
\def\ion@signs@active{\catcode`\+=\active\catcode`\-=\active\relax}
\def\ion@digits@active{\catcode`\,=\active\catcode`\.=\active%
  \catcode`\0=\active\catcode`\1=\active\catcode`\2=\active%
  \catcode`\3=\active\catcode`\4=\active\catcode`\5=\active%
  \catcode`\6=\active\catcode`\7=\active\catcode`\8=\active%
  \catcode`\9=\active\relax}
%    \end{macrocode}
%
% An analogous set of macros makes subsets of these characters active/inactive
% in math mode.
%    \begin{macrocode}
\def\ion@separators@math@active{\mathcode`,="8000\mathcode`.="8000\relax}
\def\ion@separators@math@inactive{\mathcode`,="613B\mathcode`.="013A\relax}
\def\ion@signs@math@active{\mathcode`+="8000\mathcode`-="8000\relax}
\def\ion@signs@math@inactive{\mathcode`+="202B\mathcode`-="2200\relax}
\def\ion@digits@math@active{\mathcode`0="8000\mathcode`1="8000\mathcode`2="8000%
  \mathcode`3="8000\mathcode`4="8000\mathcode`5="8000\mathcode`6="8000%
  \mathcode`7="8000\mathcode`8="8000\mathcode`9="8000\relax}
\def\ion@digits@math@inactive{\mathcode`0="7030\mathcode`1="7031%
  \mathcode`2="7032\mathcode`3="7033\mathcode`4="7034\mathcode`5="7035%
  \mathcode`6="7036\mathcode`7="7037\mathcode`8="7038\mathcode`9="7039\relax}
%    \end{macrocode}
%
% Next the user interface for making |.,+-0123456789| active/inactive follows.
% \begin{macro}{\ionumbers}
%    \begin{macrocode}
\def\ionumbers{\ion@separators@math@active\ion@signs@math@active%
  \ion@digits@math@active}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\endionumbers}
%    \begin{macrocode}
\def\endionumbers{\ion@separators@math@inactive\ion@signs@math@inactive%
  \ion@digits@math@inactive}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ionumbersoff}
%    \begin{macrocode}
\newcommand\ionumbersoff[1]{\begingroup\endionumbers#1\ionumbers\endgroup}
%    \end{macrocode}
% \end{macro}
%
% Of course, at the begining of the document the charactars shall be active
% by default.
%    \begin{macrocode}
\AtBeginDocument{\ionumbers}
%    \end{macrocode}
%
% ---------------------------------------------------------------------
%
% \subsection{Definitions of active characters}
%
% The macro definitions for the characters |.,+-0123456789| are hold in the
% following macros. Number processing works by looking at the next character
% and performing one or more from the following actions:
% \begin{itemize}
%   \item the currently configured output for the character will be added to
%     the end of |\ion@currnum| by |\ion@currnum@append|;
%     |\ion@currnum| stores the currently processed number
%   \item only for comma/point: the corresponding |after...| macro will be
%     issued
%   \item the currently processed number will be output via
%     |\ion@currnum@output|
%   \item the |e| will be eaten and replaced by its configured output
% \end{itemize}
% The conditions in the macro definitions should be self-explanatory for each
% character. The extra |\ion@startnumber| is required to avoid problems with
% input like |$a_0$| or |$\sqrt 2$|, where curly braces around |0| and |2| have
% been omitted.
%    \begin{macrocode}
\def\ion@comma{%
  \ion@ifnextdigit{%
    \ion@currnum@append*{\ion@comma@curr}\ion@aftercomma@curr%
  }{%
    \ion@ifnextseparator{%
      \ion@currnum@append*{\ion@comma@curr}\ion@aftercomma@curr%
        \@warning{Too many separators}%
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@append*{\ion@comma@curr}\ion@aftercomma@curr%
          \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output\ion@comma@original%
      }%
    }%
  }%
}
\def\ion@point{%
  \ion@ifnextdigit{%
    \ion@currnum@append*{\ion@point@curr}\ion@afterpoint@curr%
  }{%
    \ion@ifnextseparator{%
      \ion@currnum@append*{\ion@point@curr}\ion@afterpoint@curr%
        \@warning{Too many separators}%
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@append*{\ion@point@curr}\ion@afterpoint@curr%
          \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output\ion@point@original%
      }%
    }%
  }%
}
\def\ion@plus{%
  \ion@iffirstchar{%
    \ion@plus@original%
  }{%
    \ion@currnum@append*{\ion@plus@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextsign{%
        \@warning{Too many signs}%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@minus{%
  \ion@iffirstchar{%
    \ion@minus@original%
  }{%
    \ion@currnum@append*{\ion@minus@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextsign{%
        \@warning{Too many signs}%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@zero{%
  \ion@iffirstchar{%
    \ion@zero@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@zero@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@one{%
  \ion@iffirstchar{%
    \ion@one@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@one@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@two{%
  \ion@iffirstchar{%
    \ion@two@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@two@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@three{%
  \ion@iffirstchar{%
    \ion@three@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@three@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@four{%
  \ion@iffirstchar{%
    \ion@four@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@four@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@five{%
  \ion@iffirstchar{%
    \ion@five@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@five@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@six{%
  \ion@iffirstchar{%
    \ion@six@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@six@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@seven{%
  \ion@iffirstchar{%
    \ion@seven@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@seven@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@eight{%
  \ion@iffirstchar{%
    \ion@eight@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@eight@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
\def\ion@nine{%
  \ion@iffirstchar{%
    \ion@nine@original\ion@currnum@append{}%
  }{%
    \ion@currnum@append{\ion@nine@original}%
  }%
  \ion@ifnextdigit{%
    %% nothing
  }{%
    \ion@ifnextseparator{%
      %% nothing
    }{%
      \ion@ifnextchar e{%
        \ion@currnum@output\ion@exponent@curr\@gobble%
      }{%
        \ion@currnum@output%
      }%
    }%
  }%
}
%    \end{macrocode}
%
% The macro |\ion@define@charmacros| is used to assign the above macros to the
% (active) characters |.,+-0123456789|. It will be executed later in the
% conflict test section.
%    \begin{macrocode}
\begingroup
  \ion@separators@active\ion@signs@active\ion@digits@active
  \gdef\ion@define@charmacros{%
    \global\let,=\ion@comma%
    \global\let.=\ion@point%
    \global\let+=\ion@plus%
    \global\let-=\ion@minus%
    \global\let0=\ion@zero%
    \global\let1=\ion@one%
    \global\let2=\ion@two%
    \global\let3=\ion@three%
    \global\let4=\ion@four%
    \global\let5=\ion@five%
    \global\let6=\ion@six%
    \global\let7=\ion@seven%
    \global\let8=\ion@eight%
    \global\let9=\ion@nine%
  }
\endgroup
%    \end{macrocode}
%
% If one of |+-0123456789| is the first character of a number and this number
% not part of an exponent, then argument `1' will be used; otherwise argument
% `2' will be used. This macro is required to handle single characters not
% grouped in curly braces |{}| in expressions like |$a^0$| or |$\sqrt 2$|
% correctly.
%    \begin{macrocode}
\def\ion@iffirstchar#1#2{%
  \ifion@currnum@exponent%
    #2%
  \else%
    \ifion@currnum@firstchar%
      #1%
    \else
      #2%
    \fi%
  \fi%
  \ion@currnum@firstcharfalse%
}
%    \end{macrocode}
%
% Now the macros for the conditions in the above definitions follow. There are
% tests for a digit |0123456789|, \ldots
%    \begin{macrocode}
\long\def\ion@ifnextdigit#1#2{%
  \def\reserved@a{#1}%
  \def\reserved@b{#2}%
  \futurelet\@let@token\ion@ifnextdigit@}
\def\ion@ifnextdigit@{%
  \ifx\@let@token1\let\reserved@c\reserved@a\else%
    \ifx\@let@token2\let\reserved@c\reserved@a\else%
      \ifx\@let@token3\let\reserved@c\reserved@a\else%
        \ifx\@let@token4\let\reserved@c\reserved@a\else%
          \ifx\@let@token5\let\reserved@c\reserved@a\else%
            \ifx\@let@token6\let\reserved@c\reserved@a\else%
              \ifx\@let@token7\let\reserved@c\reserved@a\else%
                \ifx\@let@token8\let\reserved@c\reserved@a\else%
                  \ifx\@let@token9\let\reserved@c\reserved@a\else%
                    \ifx\@let@token0\let\reserved@c\reserved@a\else%
                      \let\reserved@c\reserved@b%
                    \fi%
                  \fi%
                \fi%
              \fi%
            \fi%
          \fi%
        \fi%
      \fi%
    \fi%
  \fi%
  \reserved@c}
%    \end{macrocode}
%
% \ldots{} for a separator |.,|, \ldots
%    \begin{macrocode}
\long\def\ion@ifnextseparator#1#2{%
  \def\reserved@a{#1}%
  \def\reserved@b{#2}%
  \futurelet\@let@token\ion@ifnextseparator@}
\def\ion@ifnextseparator@{%
  \ifx\@let@token,\let\reserved@c\reserved@a\else%
    \ifx\@let@token.\let\reserved@c\reserved@a\else%
      \let\reserved@c\reserved@b%
    \fi%
  \fi%
  \reserved@c}
%    \end{macrocode}
%
% \ldots{} and for a sign |+-| as next character.
%    \begin{macrocode}
\long\def\ion@ifnextsign#1#2{%
  \def\reserved@a{#1}%
  \def\reserved@b{#2}%
  \futurelet\@let@token\ion@ifnextsign@}
\def\ion@ifnextsign@{%
  \ifx\@let@token+\let\reserved@c\reserved@a\else%
    \ifx\@let@token-\let\reserved@c\reserved@a\else%
      \let\reserved@c\reserved@b%
    \fi%
  \fi%
  \reserved@c}
%    \end{macrocode}
%
% An additional test for an arbitrary character is also added. It obeys white
% spaces in contrast to \LaTeX{}'s |\@ifnextchar|.
%    \begin{macrocode}
\long\def\ion@ifnextchar#1#2#3{%
  \let\reserved@d=#1%
  \def\reserved@a{#2}%
  \def\reserved@b{#3}%
  \futurelet\@let@token\ion@ifnextchar@}
\def\ion@ifnextchar@{%
  \ifx\@let@token\reserved@d%
    \let\reserved@c\reserved@a%
  \else%
    \let\reserved@c\reserved@b%
  \fi%
  \reserved@c}
%    \end{macrocode}
%
% ---------------------------------------------------------------------
%
% \subsection{Test for conflicts with other packages}
%
% First of all we test for some packages known to conflict with
% \textsf{ionumbers}. This will be done by checking at the begining of the
% document, if one of these packages has been loaded and an error/warning will
% be issued.
%    \begin{macrocode}
\newcommand*{\ion@conflict@package}[1]{%
  \@ifpackageloaded{#1}{%
    \PackageError{ionumbers}%
    {Packages #1 and ionumbers conflict!\MessageBreak%
    Do not load both packages in the same document}{}%
  }{}%
}
\newcommand*{\ion@problem@package}[2]{%
  \@ifpackageloaded{#1}{%
    \PackageWarning{ionumbers}%
    {Loading #1 and ionumbers is problematic!\MessageBreak#2}%
  }{}%
}

\AtBeginDocument{%
  \ion@conflict@package{ziffer}%
  \ion@problem@package{dcolumn}{Use `tabular's inside \string\ionumbersoff}%
  \ion@problem@package{amsmath}{Load ionumbers after amsmath}%
  \ion@problem@package{amsmath}{Use \string\operatorname\space inside
    \string\ionumbersoff}%
  \ion@problem@package{amsopn}{Use \string\operatorname\space inside
    \string\ionumbersoff}%
}
%    \end{macrocode}
%
% Next the characters |.,+-0123456789| are checked for macro definitions (by
% other packages). This way conflicts with other packages may be detected with
% some probability (but only if the conflicting package has already been
% loaded).
%    \begin{macrocode}
\newcommand*\ion@conflict@definedtest[1]{%
  \ifx#1\@undefined\else\PackageWarning{ionumbers}%
    {Potential conflict with other package(s) detected.\MessageBreak%
    `\string#1' has already been defined. I will redefine it.\MessageBreak%
    This might break other package(s)!\MessageBreak}\fi}
\begingroup
  \ion@separators@active\ion@signs@active\ion@digits@active
  \ion@conflict@definedtest{,}
  \ion@conflict@definedtest{.}
  \ion@conflict@definedtest{+}
  \ion@conflict@definedtest{-}
  \ion@conflict@definedtest{0}
  \ion@conflict@definedtest{1}
  \ion@conflict@definedtest{2}
  \ion@conflict@definedtest{3}
  \ion@conflict@definedtest{4}
  \ion@conflict@definedtest{5}
  \ion@conflict@definedtest{6}
  \ion@conflict@definedtest{7}
  \ion@conflict@definedtest{8}
  \ion@conflict@definedtest{9}
\endgroup
%    \end{macrocode}
%
% After the above test the definitions of the characters of
% \textsf{ionumbers} can be applied.
%    \begin{macrocode}
\ion@define@charmacros
%    \end{macrocode}
%
% Additionally, \textsf{ionumbers} tests for redefinitions of the macros
% of the characters at the begining of the document.
%    \begin{macrocode}
\newcommand*{\ion@conflict@redefinedtest}[2]{%
  \ifx#1#2\else\PackageWarning{ionumbers}%
    {Potential conflict with other package(s) detected.\MessageBreak%
    `\string#1' has been redefined. This might break ionumbers!\MessageBreak}%
  \fi}
\begingroup
  \ion@separators@active\ion@signs@active\ion@digits@active
  \gdef\ion@conflict@redefinedtest@macro{%
    \ion@conflict@redefinedtest{,}{\ion@comma}%
    \ion@conflict@redefinedtest{.}{\ion@point}%
    \ion@conflict@redefinedtest{+}{\ion@plus}%
    \ion@conflict@redefinedtest{-}{\ion@minus}%
    \ion@conflict@redefinedtest{0}{\ion@zero}%
    \ion@conflict@redefinedtest{1}{\ion@one}%
    \ion@conflict@redefinedtest{2}{\ion@two}%
    \ion@conflict@redefinedtest{3}{\ion@three}%
    \ion@conflict@redefinedtest{4}{\ion@four}%
    \ion@conflict@redefinedtest{5}{\ion@five}%
    \ion@conflict@redefinedtest{6}{\ion@six}%
    \ion@conflict@redefinedtest{7}{\ion@seven}%
    \ion@conflict@redefinedtest{8}{\ion@eight}%
    \ion@conflict@redefinedtest{9}{\ion@nine}%
  }
\endgroup
\AtBeginDocument{\ion@conflict@redefinedtest@macro}
%    \end{macrocode}
%
% ---------------------------------------------------------------------
%
% \subsection{Commands for current number}
%
% Numbers are processed by first storing one character after the other in an
% internal macro to be able to automatically group digits. The basic idea when
% adding single characters is
% \begin{itemize}
%   \item remember, whether we are processing the thousands or the thousandths
%     part of a number (|\ifion@beforedecimal|)
%   \item calculate the number of digits processed modulo 3 plus 1 in the
%     current part and
%     \begin{itemize}
%       \item for the thousands part: add |\ion@thousands@sepa| for 1,
%         |\ion@thousands@sepb| for 2, and |\ion@thousands@sepc| for 3 after
%         a digit
%       \item for the thousandths part: add |\ion@thousandths@sep| after each
%         third digit
%     \end{itemize}
% \end{itemize}
% The macros |\ion@thousands@sep...| and |\ion@thousandths@sep| are empty by
% default. Before outputting the number, the number of digits in the thousands
% part is known and the correct |\ion@thousands@sep...| macro can be set to the
% thousands separator for correct grouping.
%
% First of all, the |if|s, counters and empty separator macros are initialized.
%    \begin{macrocode}
\newif\ifion@currnum@firstchar\ion@currnum@firstchartrue
\newif\ifion@beforedecimal\ion@beforedecimaltrue
\newif\ifion@noexplicitthousands\ion@noexplicitthousandstrue
\newif\ifion@currnum@exponent\ion@currnum@exponentfalse
\newif\ifion@exponent@superscript\ion@exponent@superscriptfalse
\newcount\ion@thousands@currpos\ion@thousands@currpos=0
\newcount\ion@thousandths@currpos\ion@thousandths@currpos=0
\def\ion@currnum{}
\def\ion@thousands@sepa{}
\def\ion@thousands@sepb{}
\def\ion@thousands@sepc{}
\def\ion@thousandths@sep{}
%    \end{macrocode}
%
% The macro |\ion@currnum@append| adds the character in its argument to the
% end of |\ion@currnum|. In the starred version adding of an empty separator
% macros is omitted.
%    \begin{macrocode}
\newcommand{\ion@currnum@append}{%
  \ion@currnum@firstcharfalse%
  \@ifstar{\ion@currnum@append@@}{\ion@currnum@append@}%
}
\newcommand*{\ion@currnum@append@@}[1]{%
  \ion@addto@macro{\ion@currnum}{#1}%
}
\newcommand*{\ion@currnum@append@}[1]{%
  \ifion@beforedecimal%
    %% push back (empty) separator and character
    \ifcase\ion@thousands@currpos%
      \ion@addto@macro{\ion@currnum}{#1}%
    \or%
      \ion@addto@macro{\ion@currnum}{\ion@thousands@sepa#1}%
    \or%
      \ion@addto@macro{\ion@currnum}{\ion@thousands@sepb#1}%
    \or%
      \ion@addto@macro{\ion@currnum}{\ion@thousands@sepc#1}%
    \fi%
    %% advance thousands counter
    \advance\ion@thousands@currpos by1\relax%
    \ifnum\ion@thousands@currpos>3%
      \ion@thousands@currpos=1%
    \fi%
  \else%
    %% push back (empty) separator and character
    \ifnum\ion@thousandths@currpos=3%
      \ion@addto@macro{\ion@currnum}{\ion@thousandths@sep#1}%
    \else%
      \ion@addto@macro{\ion@currnum}{#1}%
    \fi%
    %% advance thousandths counter
    \advance\ion@thousandths@currpos by1\relax%
    \ifnum\ion@thousandths@currpos>3%
      \ion@thousandths@currpos=1%
    \fi%
  \fi%
}
%    \end{macrocode}
%
% The |\ion@currnum@output| macro defines the empty separator macros (depending
% on the current configuration), outputs the current number, and resets
% everything for the next number.
%    \begin{macrocode}
\newcommand*{\ion@currnum@output}{%
  \begingroup%
    %% set automatic thousands separator
    \ifion@autothousands%
      \ifion@noexplicitthousands%
        \ifcase\ion@thousands@currpos%
          %% do nothing
        \or%
          \def\ion@thousands@sepa{\ion@thousands@curr}%
        \or%
          \def\ion@thousands@sepb{\ion@thousands@curr}%
        \or%
          \def\ion@thousands@sepc{\ion@thousands@curr}%
        \fi%
      \fi%
    \fi%
    %% set automatic thousandths separator
    \ifion@autothousandths%
      \def\ion@thousandths@sep{\ion@thousandths@curr}%
    \fi%
    %% output number
    \ifion@currnum@exponent%
      \ifion@exponent@superscript%
        ^{\ion@currnum}%
      \else%
        {\ion@currnum}%
      \fi%
    \else
      \ion@currnum%
    \fi
  \endgroup%
  %% reset stuff for next number
  \ion@thousands@currpos=0%
  \ion@thousandths@currpos=0%
  \def\ion@currnum{}%
  \ion@currnum@firstchartrue%
  \ion@beforedecimaltrue%
  \ion@noexplicitthousandstrue%
  \ion@currnum@exponentfalse%
  \ion@exponent@superscriptfalse%
}
%    \end{macrocode}
%
% This macro is identical to |\l@addto@macro| from \textsf{koma-script bundle}.
%    \begin{macrocode}
\newcommand{\ion@addto@macro}[2]{%
  \begingroup\toks@\expandafter{#1#2}%
    \edef\@tempa{\endgroup\def\noexpand#1{\the\toks@}}%
  \@tempa}
%    \end{macrocode}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \Finale
%
\endinput
