Debian Codegraph @

texmf-dist/source/latex/decimalcomma/decimalcomma.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2023 by Antoine Missier <[email protected]>
4 %
5 % This file may be distributed and/or modified under the conditions of
6 % the LaTeX Project Public License, either version 1.3 of this license
7 % or (at your option) any later version. The latest version of this
8 % license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.3 or later is part of all distributions of LaTeX version
13 % 2005/12/01 or later.
14 % \fi
15 %
16 % \iffalse
17 %<*driver>
18 \ProvidesFile{decimalcomma.dtx}
19 %</driver>
20 %<*package>
21 \NeedsTeXFormat{LaTeX2e}[2005/12/01]
22 \ProvidesPackage{decimalcomma}
23 [2023/12/30 v1.4 .dtx Comma for decimal numbers]
24 %</package>
25 %<*driver>
26 \documentclass{ltxdoc}
27 %\usepackage{doc}
28 %\MakeShortVerb|
29 \usepackage[utf8]{inputenc}
30 \usepackage[T1]{fontenc}
31 \usepackage[english]{babel}
32 \usepackage[charter]{mathdesign}
33 \usepackage{decimalcomma}
34 \usepackage{sectsty}
35 \usepackage{xcolor}
36 \usepackage{centeredline}
37 \usepackage{metalogo}
38
39 \definecolor{darkbrown}{rgb}{0.5,0.1,0.1}
40 \allsectionsfont{\color{darkbrown}}
41
42 \DisableCrossrefs
43 %\CodelineIndex
44 %\RecordChanges
45 \usepackage{hyperref}
46 \hypersetup{%
47 colorlinks,
48 linkcolor=blue,
49 citecolor=red,
50 pdftitle={decimalcomma},
51 pdfsubject={LaTeX package},
52 pdfauthor={Antoine Missier}
53 }
54 \MakeShortVerb{"}
55 \begin{document}
56 \DocInput{decimalcomma.dtx}
57 %\PrintChanges
58 %\PrintIndex
59 \end{document}
60 %</driver>
61 % \fi
62 %
63 % \CheckSum{18}
64 %
65 % \CharacterTable
66 % {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
67 % 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
68 % Digits \0\1\2\3\4\5\6\7\8\9
69 % Exclamation \! Double quote \" Hash (number) \#
70 % Dollar \$ Percent \% Ampersand \&
71 % Acute accent \' Left paren \( Right paren \)
72 % Asterisk \* Plus \+ Comma \,
73 % Minus \- Point \. Solidus \/
74 % Colon \: Semicolon \; Less than \<
75 % Equals \= Greater than \> Question mark \?
76 % Commercial at \@ Left bracket \[ Backslash \\
77 % Right bracket \] Circumflex \^ Underscore \_
78 % Grave accent \` Left brace \{ Vertical bar \|
79 % Right brace \} Tilde \~}
80 %
81 % \changes{v1.0}{2023/12/06}{Initial version}
82 % \changes{v1.1}{2023/12/19}{Error message when unicode-math loaded after decimalcomma}
83 % \changes{v1.2}{2023/12/20}{Bug correction: bad typo for the boolean DC@unicodemath}
84 % \changes{v1.3}{2023/12/28}{Loading icomma to avoid babel-french + numprint problems}
85 % \changes{v1.4}{2023/12/30}{Code improvement, first boolean unnecessary, thanks to J.F. Burnol}
86 %
87 % \GetFileInfo{decimalcomma.sty}
88 %
89 % \title{The \textsf{decimalcomma} package}
90 % \author{Antoine Missier \\ \texttt{[email protected]}}
91 % \date{\filedate, \fileversion}
92 % \maketitle
93 %
94 % \section{Why this package?}
95 %
96 % In many countries, except notably in the English-speaking countries,
97 % the comma is used as the decimal separator for numbers.
98 % However, in the math mode of \LaTeX, the comma is always, by default,
99 % treated as a punctuation symbol and therefore is followed by a space.
100 % This is appropriate in intervals: "$[a,b]$" results in $[a,b]$,
101 % but is not appropriate for numbers where the comma represents the decimal separator.
102 % For example, "$12,5$" is displayed as $12, 5$ instead of $12,5$.
103 %
104 % \medskip
105 % Two very convenient packages allow handling the decimal comma. In math mode:
106 % \begin{itemize}
107 % \item With \textsf{icomma} (intelligent comma) by Walter Schmidt~\cite{ICOMMA},
108 % the comma behaves as a punctuation character if it is followed by a space;
109 % otherwise, it is treated as an ordinary character.
110 % \item With \textsf{ncccomma} by Alexander I.~Rozhenko~\cite{NCC},
111 % the comma behaves as an ordinary character if it is followed by a digit
112 % (without a space); otherwise, it is considered a punctuation character.
113 % \end{itemize}
114 %
115 % It appears that this second approach is preferable because unlike with icomma,
116 % you are not required to add a space after the comma when it's not followed by a digit;
117 % however, \textsf{ncccomma} poses several compatibility issues,
118 % especially when used with \textsf{babel}
119 % in conjunction with \textsf{numprint}~\cite{NUMP} and its \texttt{autolanguage} option
120 % (at least for certain languages such as French and Spanish).
121 % Another issue with \textsf{ncccomma} is that compiling with \LuaLaTeX\ or \XeLaTeX\
122 % fails when using \textsf{unicode-math} and "\setmathfont".
123 %
124 % Let it be noted that a code proposed by Claudio Beccari~\cite{BECC},
125 % similar to that of \textsf{ncccomma}, presents the same type of issues.
126 %
127 % \medskip
128 % Therefore, we propose a new package, functionally identical to
129 % \textsf{ncccomma} but with lighter code, and without the
130 % aforementioned incompatibilities.
131 %
132 % \section{Bugs and problems}
133 %
134 % When \textsf{unicode-math} is used, it must be loaded \emph{before}
135 % \textsf{decimalcomma}\footnote{This is also true for \textsf{icomma}.}.
136 %
137 % Here, we are revisiting what Walter Schmidt wrote in the documentation of his
138 % \textsf{icomma} package:
139 %
140 % In case \textsf{decimalcomma} is used together with the \textsf{dcolumn}
141 % package~\cite{DCOL}, a comma to be printed as the decimal separator
142 % in a column of type \texttt{D}
143 % is to be specified as "{\mathord\mathcomma}", rather than "{,}",
144 % since the latter leads to an error.
145 % For instance:
146 % \centeredline{"\begin{tabular}{... D{,}{\mathord\mathcomma}{2} ...}"}
147 %
148 % \medskip
149 % Note that specifying the comma as the related input character works as usual.
150 % Generally, since the \textsf{decimalcomma} package makes the comma ‘active’,
151 % further problems are not unlikely.
152 %
153 % \StopEventually{}
154 %
155 % \section{Implementation}
156 %
157 % This first piece of code aims to generate an appropriate error message, if you load
158 % \textsf{unicode-math} \emph{after} \textsf{decimalcomma}.
159 % To achieve this, first we check if \textsf{unicode-math} has been loaded before.
160 % In that case, we do nothing.
161 % Else, at the end of the preamble, when all the packages have been loaded,
162 % we perform a new verification. If \textsf{unicode-math} has been loaded at that time,
163 % the appropriate error message is displayed.
164 %
165 % Without this code, the compilation would produce an error message mentioning
166 % a problem on "\futurelet", incomprehensible for the novice.
167 %
168 % \begin{macrocode}
169
170 \@ifpackageloaded{unicode-math}{}{
171 \AtBeginDocument{%
172 \@ifpackageloaded{unicode-math}{
173 \PackageError{decimalcomma}{decimalcomma must be loaded
174 after unicode-math}{If you didn't load decimalcomma
175 yourself, check which package uses it.}
176 }{}
177 }
178 }
179
180 % \end{macrocode}
181 % We have taken up Walter Schmidt's code for looking up the next character,
182 % and define the comma as active in math mode,
183 % but with an execution loop to test all the digits from 0 to 9 instead of "\space".
184 % We could have also used ten nested "\if" \ldots "\else" \ldots "\fi" structures
185 % and that works very well.
186 %
187 %We chose to directly load \textsf{icomma} instead of transcribing
188 % the beginning portion of its code.
189 % This avoids potential conflicts with an extension
190 % that also uses \textsf{icomma}\footnote{The loading of \textsf{icomma} ensures also
191 % proper functioning when using \textsf{babel-french} and \textsf{numprint}
192 % with its \texttt{autolanguage} option
193 % (an issue that does not arise with \textsf{babel-spanish}, for example).}.
194 % \begin{macrocode}
195 \RequirePackage{icomma}
196
197 % \end{macrocode}
198 %
199 % First we introduce what \textsf{icomma} does.
200 % At |\begin{document}|, the original "\mathcode" of the comma is stored,
201 % in the |\mathcomma| macro, and then the comma is defined as active in math mode.
202 % The active comma checks the next input character.
203 % \begin{verbatim}
204 %\AtBeginDocument{%
205 % \mathchardef\mathcomma\mathcode`\,%
206 % \mathcode`\,="8000 %
207 %}
208 %{\catcode`,=\active
209 % \gdef,{\futurelet\@let@token\sm@rtcomma}
210 %}
211 % \end{verbatim}
212 %
213 % Then \textsf{icomma} defines the |\sm@rtcomma| macro, but this macro
214 % is redefined here to match the expected behavior.
215 % If the next character is in the list 0123456789,
216 % the active comma returns |\mathord| with the saved |\mathcomma|,
217 % so that no space will be added after the comma.
218 % Otherwise, |\mathcomma| is returned without |\mathord|, thus the comma behaves
219 % by default as a |\mathpunct|.
220 % Note that |\@decimal@digit| must be called before |\@let@token| after |\ifx|.
221 % \begin{macrocode}
222 \def\sm@rtcomma{%
223 \@tfor\decimal@digit:=0123456789%
224 \do{\expandafter\ifx\decimal@digit\@let@token\mathord%
225 \@break@tfor\fi}%
226 \mathcomma}
227
228 % \end{macrocode}
229 % Special thanks to J.F. Burnol for his insightful remarks and valuable advice
230 % and let us pay tribute to Walter Schmidt, who is regrettably deceased.
231 %
232 % \begin{thebibliography}{5}
233 % % \begin{raggedright}
234 % \bibitem{ICOMMA} \emph{The \textsf{icomma} package for \LaTeXe}.
235 % Walter Schmidt, CTAN, v2.0 2002/03/10.
236 % \bibitem{NCC} \emph{The \textsf{ncccomma} package}. Alexander I.~Rozhenko,
237 % CTAN, v1.0 2005/02/10.
238 % \bibitem{BECC} \emph{Intelligent commas}. Claudio Beccari, The Prac\TeX\ Journal,
239 % 2011, No.\@1.
240 % \url{https://tug.org/pracjourn/2011-1/beccari/Intcomma.pdf}
241 % \bibitem{NUMP} \emph{The \textsf{numprint} package}. Harald Harders,
242 % CTAN, v1.39 2012/08/20.
243 % \bibitem{DCOL} \emph{The \textsf{dcolumn} package}, David Carlisle.
244 % CTAN, v1.06 2023/07/08.
245 % \end{raggedright}
246 % \end{thebibliography}
247 % \Finale
248 \endinput