% \iffalse meta-comment
%
% Copyright (C) 2023 by Antoine Missier <antoine.missier@ac-toulouse.fr>
%
% This file 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.
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{decimalcomma.dtx}
%</driver>
%<*package> 
\NeedsTeXFormat{LaTeX2e}[2005/12/01]
\ProvidesPackage{decimalcomma}
    [2023/12/30 v1.4 .dtx Comma for decimal numbers]
%</package>
%<*driver>
\documentclass{ltxdoc}
%\usepackage{doc}
%\MakeShortVerb|
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[charter]{mathdesign}
\usepackage{decimalcomma}
\usepackage{sectsty}
\usepackage{xcolor}
\usepackage{centeredline}
\usepackage{metalogo}

\definecolor{darkbrown}{rgb}{0.5,0.1,0.1}
\allsectionsfont{\color{darkbrown}}

\DisableCrossrefs
%\CodelineIndex
%\RecordChanges
\usepackage{hyperref}
\hypersetup{%
    colorlinks,
    linkcolor=blue,
    citecolor=red,
    pdftitle={decimalcomma},
    pdfsubject={LaTeX package},
    pdfauthor={Antoine Missier}
}
\MakeShortVerb{"}
\begin{document}
\DocInput{decimalcomma.dtx}
%\PrintChanges
%\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{18}
%
% \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         \~}
%
% \changes{v1.0}{2023/12/06}{Initial version}
% \changes{v1.1}{2023/12/19}{Error message when unicode-math loaded after decimalcomma}
% \changes{v1.2}{2023/12/20}{Bug correction: bad typo for the boolean DC@unicodemath}
% \changes{v1.3}{2023/12/28}{Loading icomma to avoid babel-french + numprint problems}
% \changes{v1.4}{2023/12/30}{Code improvement, first boolean unnecessary, thanks to J.F. Burnol}
%
% \GetFileInfo{decimalcomma.sty}
%
% \title{The \textsf{decimalcomma} package}
% \author{Antoine Missier \\ \texttt{antoine.missier@ac-toulouse.fr}}
% \date{\filedate, \fileversion}
% \maketitle
%
% \section{Why this package?}
%
% In many countries, except notably in the English-speaking countries, 
% the comma is used as the decimal separator for numbers. 
% However, in the math mode of \LaTeX, the comma is always, by default, 
% treated as a punctuation symbol and therefore is followed by a space. 
% This is appropriate in intervals: "$[a,b]$" results in $[a,b]$,
% but is not appropriate for numbers where the comma represents the decimal separator. 
% For example, "$12,5$" is displayed as $12, 5$ instead of $12,5$.
%
% \medskip
% Two very convenient packages allow handling the decimal comma. In math mode:
% \begin{itemize}
% \item With \textsf{icomma} (intelligent comma) by Walter Schmidt~\cite{ICOMMA}, 
% the comma behaves as a punctuation character if it is followed by a space; 
% otherwise, it is treated as an ordinary character.
% \item With \textsf{ncccomma} by Alexander I.~Rozhenko~\cite{NCC}, 
% the comma behaves as an ordinary character if it is followed by a digit 
% (without a space); otherwise, it is considered a punctuation character.
% \end{itemize}
%
% It appears that this second approach is preferable because unlike with icomma, 
% you are not required to add a space after the comma when it's not followed by a digit;
% however, \textsf{ncccomma} poses several compatibility issues, 
% especially when used with \textsf{babel}
% in conjunction with \textsf{numprint}~\cite{NUMP} and its \texttt{autolanguage} option 
% (at least for certain languages such as French and Spanish). 
% Another issue with \textsf{ncccomma} is that compiling with \LuaLaTeX\ or \XeLaTeX\
% fails when using \textsf{unicode-math} and "\setmathfont".
% 
% Let it be noted that a code proposed by Claudio Beccari~\cite{BECC}, 
% similar to that of \textsf{ncccomma}, presents the same type of issues.
%
% \medskip
% Therefore, we propose a new package, functionally identical to 
% \textsf{ncccomma} but with lighter code, and without the 
% aforementioned incompatibilities.
% 
% \section{Bugs and problems}
%
% When \textsf{unicode-math} is used, it must be loaded \emph{before} 
% \textsf{decimalcomma}\footnote{This is also true for \textsf{icomma}.}.
%
% Here, we are revisiting what Walter Schmidt wrote in the documentation of his 
% \textsf{icomma} package:
% 
% In case \textsf{decimalcomma} is used together with the \textsf{dcolumn} 
% package~\cite{DCOL}, a comma to be printed as the decimal separator 
% in a column of type \texttt{D} 
% is to be specified as "{\mathord\mathcomma}", rather than "{,}", 
% since the latter leads to an error.
% For instance: 
% \centeredline{"\begin{tabular}{... D{,}{\mathord\mathcomma}{2} ...}"}
%
% \medskip
% Note that specifying the comma as the related input character works as usual.
% Generally, since the \textsf{decimalcomma} package makes the comma ‘active’, 
% further problems are not unlikely.
%
% \StopEventually{}
%
% \section{Implementation}
%
% This first piece of code aims to generate an appropriate error message, if you load
% \textsf{unicode-math} \emph{after} \textsf{decimalcomma}.
% To achieve this, first we check if \textsf{unicode-math} has been loaded before.
% In that case, we do nothing.
% Else, at the end of the preamble, when all the packages have been loaded,
% we perform a new verification. If \textsf{unicode-math} has been loaded at that time,
% the appropriate error message is displayed.
%
% Without this code, the compilation would produce an error message mentioning 
% a problem on "\futurelet", incomprehensible for the novice.
% 
%    \begin{macrocode}

\@ifpackageloaded{unicode-math}{}{
  \AtBeginDocument{%
    \@ifpackageloaded{unicode-math}{
      \PackageError{decimalcomma}{decimalcomma must be loaded 
        after unicode-math}{If you didn't load decimalcomma
        yourself, check which package uses it.}
    }{}
  }
}

%    \end{macrocode}
% We have taken up Walter Schmidt's code for looking up the next character,
% and define the comma as active in math mode,
% but with an execution loop to test all the digits from 0 to 9 instead of "\space".
% We could have also used ten nested "\if" \ldots "\else" \ldots "\fi" structures 
% and that works very well.
%
%We chose to directly load \textsf{icomma} instead of transcribing 
% the beginning portion of its code.
% This avoids potential conflicts with an extension 
% that also uses \textsf{icomma}\footnote{The loading of \textsf{icomma} ensures also
% proper functioning when using \textsf{babel-french} and \textsf{numprint} 
% with its \texttt{autolanguage} option 
% (an issue that does not arise with \textsf{babel-spanish}, for example).}. 
%    \begin{macrocode}
\RequirePackage{icomma}  

%    \end{macrocode}
%
% First we introduce what \textsf{icomma} does. 
% At |\begin{document}|, the original "\mathcode" of the comma is stored,
% in the |\mathcomma| macro, and then the comma is defined as active in math mode.
% The active comma checks the next input character. 
% \begin{verbatim}
%\AtBeginDocument{%
%  \mathchardef\mathcomma\mathcode`\,%
%  \mathcode`\,="8000 %
%}
%{\catcode`,=\active
%  \gdef,{\futurelet\@let@token\sm@rtcomma}
%}
% \end{verbatim}
%
% Then \textsf{icomma} defines the |\sm@rtcomma| macro, but this macro
% is redefined here to match the expected behavior.
% If the next character is in the list 0123456789,
% the active comma returns |\mathord| with the saved |\mathcomma|,
% so that no space will be added after the comma.
% Otherwise, |\mathcomma| is returned without |\mathord|, thus the comma behaves
% by default as a |\mathpunct|.
% Note that |\@decimal@digit| must be called before |\@let@token| after |\ifx|.
%    \begin{macrocode}
\def\sm@rtcomma{%
  \@tfor\decimal@digit:=0123456789%
  \do{\expandafter\ifx\decimal@digit\@let@token\mathord%
    \@break@tfor\fi}%
  \mathcomma}

%    \end{macrocode}
% Special thanks to J.F. Burnol for his insightful remarks and valuable advice
% and let us pay tribute to Walter Schmidt, who is regrettably deceased.
%
% \begin{thebibliography}{5}
% % \begin{raggedright}
% \bibitem{ICOMMA} \emph{The \textsf{icomma} package for \LaTeXe}. 
% Walter Schmidt, CTAN, v2.0 2002/03/10.
% \bibitem{NCC} \emph{The \textsf{ncccomma} package}. Alexander I.~Rozhenko, 
% CTAN, v1.0 2005/02/10.
% \bibitem{BECC} \emph{Intelligent commas}. Claudio Beccari, The Prac\TeX\ Journal, 
% 2011, No.\@1.
% \url{https://tug.org/pracjourn/2011-1/beccari/Intcomma.pdf}
% \bibitem{NUMP} \emph{The \textsf{numprint} package}. Harald Harders,
% CTAN, v1.39 2012/08/20.
% \bibitem{DCOL} \emph{The \textsf{dcolumn} package}, David Carlisle.
% CTAN, v1.06 2023/07/08.
% \end{raggedright}
% \end{thebibliography}
% \Finale
\endinput
