summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/decimalcomma/decimalcomma.dtx
blob: ae1fb4fa99f19b712df179345bb80589398a41a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
% \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/06 v1.0 .dtx decimalcomma file]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[charter]{mathdesign}
\usepackage{decimalcomma}
\usepackage{sectsty}
\usepackage{xcolor}
\usepackage{centeredline}

\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{26}
%
% \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}
%
% \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 Anglo-Saxon countries, 
% the comma is used as a 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 Lua\LaTeX, 
% \textsf{unicode-math}, and "\setmathfont" fails.
% 
% 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 that 
% of \textsf{ncccomma} but with lighter code and without the 
% aforementioned incompatibilities.
% 
% \section{Bugs and problems}
% Here, we are revisiting what Walter Schmidt wrote in the documentation of his 
% \textsf{icomma} package:
% 
% In case the \textsf{decimalcomma} is used together with the \textsf{dcolumn} package, 
% 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}
% We have taken up Walter Schmidt's fundamental idea for looking up the next character,
% 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.
% 
% At |\begin{document}|, we store the original "\mathcode" of the comma,
% in the |\mathcomma| macro, and then we make the comma active in math mode.
%    \begin{macrocode}
\AtBeginDocument{%
  \mathchardef\mathcomma\mathcode`\,%
  \mathcode`\,="8000%
}

%    \end{macrocode}
% The active comma checks the next input character. 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|.
%    \begin{macrocode}
{\catcode`,=\active
  \gdef,{\futurelet\@next\sm@rtcomma}}
  
\def\sm@rtcomma{%
  \@tfor\@let@token:=0123456789%
  \do{\expandafter\ifx\@let@token\@next\mathord\@break@tfor\fi}%
  \mathcomma}

%    \end{macrocode}
%
% \begin{thebibliography}{4}
% % \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.
% \end{raggedright}
% \end{thebibliography}
% \Finale
\endinput