forked from josephwright/siunitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siunitx-number.dtx
executable file
·3051 lines (3051 loc) · 94.8 KB
/
siunitx-number.dtx
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse meta-comment
%
% File: siunitx-number.dtx Copyright (C) 2014-2019 Joseph Wright
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version. The latest version
% of this license is in the file
%
% https://www.latex-project.org/lppl.txt
%
% This file is part of the "siunitx bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% The released version of this bundle is available from CTAN.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
% https://github.com/josephwright/siunitx
%
% for those people who are interested.
%
% -----------------------------------------------------------------------
%
%<*driver>
\documentclass{l3doc}
\usepackage{xparse}
\ProvideDocumentCommand\foreign{m}{\textit{#1}}
% The next line is needed so that \GetFileInfo will be able to pick up
% version data
\usepackage{siunitx}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{siunitx.sty}
%
% \title{^^A
% \pkg{siunitx-number} -- Parsing and formatting numbers^^A
% \thanks{This file describes \fileversion,
% last revised \filedate.}^^A
% }
%
% \author{^^A
% Joseph Wright^^A
% \thanks{^^A
% E-mail:
% \href{mailto:joseph.wright@morningstar2.co.uk}
% {joseph.wright@morningstar2.co.uk}^^A
% }^^A
% }
%
% \date{Released \filedate}
%
% \maketitle
%
% \begin{documentation}
%
% \section{Formatting numbers}
%
% \begin{function}{\siunitx_number_format:nN, \siunitx_number_format:VN}
% \begin{syntax}
% \cs{siunitx_number_format:nN} \Arg{number} \meta{tl~var}
% \end{syntax}
% \end{function}
%
% \begin{function}{\siunitx_number_parse:nN}
% \begin{syntax}
% \cs{siunitx_number_parse:nN} \Arg{number} \meta{tl~var}
% \end{syntax}
% Parses the \emph{number} and stores the resulting internal representation
% in the \meta{tl~var}. The parsing is influenced by the various key--value
% settings for numerical input. The \meta{number} should comprise a single
% real value, possibly with comparator, uncertainty and exponent parts.
% If the number is invalid, or if number parsing is disabled, the result will
% be an entirely empty \meta{tl~var}.
%
% The structure of a valid number is:
% \begin{quote}
% \marg{comparator}\marg{sign}\marg{integer}\marg{decimal}
% \marg{uncertainty}\\
% \marg{exponent sign}\marg{exponent}
% \end{quote}
% where the two sign parts must be single tokens if present,
% and all other components
% must be given in braces. The number will have at least one digit for both the
% \meta{integer} and \meta{exponent} parts: these are required. The
% \meta{uncertainty} part should either be blank or contain an
% \meta{identifier} (as a brace group), followed by one or more data entries.
% Valid \meta{identifiers} currently are
% \begin{itemize}
% \item[\texttt{S}] A single symmetrical uncertainty (\foreign{e.g.}~a
% statistical standard uncertainty)
% \end{itemize}
% \end{function}
%
% \begin{function}{\siunitx_number_process:NN}
% \begin{syntax}
% \cs{siunitx_number_process:N} \meta{tl~var1} \meta{tl~var2}
% \end{syntax}
% Applies a set of number processing operations to the \meta{internal
% number} stored in the \meta{tl~var1}, \foreign{viz.}~in order
% \begin{enumerate}
% \item Dropping uncertainty
% \item Converting to scientific mode (or similar)
% \item Rounding
% \item Dropping zero decimal part
% \item Forcing a minimum number of digits
% \end{enumerate}
% with the result stored in \meta{tl~var2}.
% \end{function}
%
% \begin{function}[rEXP]{\siunitx_number_format:N, \siunitx_number_format:NN}
% \begin{syntax}
% \cs{siunitx_number_format:N} \meta{number}
% \cs{siunitx_number_format:NN} \meta{number} \meta{marker}
% \end{syntax}
% Formats the \meta{number} (in the \pkg{siunitx} internal format),
% producing the result in a form suitable for typesetting in math mode.
% The details for the formatting are controlled by a number of key--value
% options. Note that \emph{formatting} does not apply any manipulation
% (processing) to the number. This function is usable in an \texttt{e}-
% or \texttt{x}-type expansion, and further uncontrolled expansion is
% prevented by appropriate use of |\exp_not:n| internally.
%
% In the \texttt{NN} version, the \meta{marker} token is inserted at each
% possible alignment position in the output, \foreign{viz.}
% \begin{itemize}
% \item Between the comparator and the integer (\emph{before} any
% sign for the integer)
% \item Both sides of the decimal marker
% \item Both sides of the separated uncertainty sign (\emph{i.e.}~after
% the decimal part and before any integer uncertainty part)
% \item Both sides of the decimal marker for a separated uncertainty
% \item Both sides of the multiplication symbol for the exponent part.
% \end{itemize}
% \end{function}
%
% \begin{function}[pTF, EXP]{\siunitx_if_number:n}
% \begin{syntax}
% \cs{siunitx_if_number_token:NTF} \Arg{tokens}
% \Arg{true code} \Arg{false code}
% \end{syntax}
% Determines if the \meta{tokens} form a valid number which can be fully
% parsed by \pkg{siunitx}.
% \end{function}
%
% \begin{function}[TF]{\siunitx_if_number_token:N}
% \begin{syntax}
% \cs{siunitx_if_number_token:NTF} \Arg{token}
% \Arg{true code} \Arg{false code}
% \end{syntax}
% Determines if the \meta{token} is valid in a number based on those
% tokens currently set up for detection in a number.
% \end{function}
%
% \begin{variable}{\l_siunitx_number_parse_bool}
% A switch to control whether any parsing is attempted for numbers.
% \end{variable}
%
% \begin{variable}
% {\l_siunitx_number_input_decimal_tl, \l_siunitx_number_output_decimal_tl}
% The list of possible input decimal marker(s), and the output marker.
% \end{variable}
%
% \subsection{Key--value options}
%
% The options defined by this submodule are available within the \pkg{l3keys}
% |siunitx| tree.
%
% \begin{function}{bracket-negative-numbers}
% \begin{syntax}
% |bracket-negative-numbers| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{drop-exponent}
% \begin{syntax}
% |drop-exponent| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{drop-uncertainty}
% \begin{syntax}
% |drop-uncertainty| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{drop-zero-decimal}
% \begin{syntax}
% |drop-zero-decimal| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{evaluate-expression}
% \begin{syntax}
% |evaluate-expression| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{exponent-base}
% \begin{syntax}
% |exponent-base| = \meta{base}
% \end{syntax}
% \end{function}
%
% \begin{function}{exponent-mode}
% \begin{syntax}
% |exponent-mode| = |engineering|\verb"|"|fixed|\verb"|"|input|\verb"|"|scientific|
% \end{syntax}
% \end{function}
%
% \begin{function}{exponent-product}
% \begin{syntax}
% |exponent-product| = \meta{symbol}
% \end{syntax}
% \end{function}
%
% \begin{function}{expression}
% \begin{syntax}
% |expression| = \meta{expression}
% \end{syntax}
% \end{function}
%
% \begin{function}{fixed-exponent}
% \begin{syntax}
% |fixed-exponent| = \meta{exponent}
% \end{syntax}
% \end{function}
%
% \begin{function}{group-digits}
% \begin{syntax}
% |group-digits| = |all|\verb"|"|decimal|\verb"|"|integer|\verb"|"|none|
% \end{syntax}
% \end{function}
%
% \begin{function}{group-minimum-digits}
% \begin{syntax}
% |group-minimum-digits| = \meta{value}
% \end{syntax}
% \end{function}
%
% \begin{function}{group-separator}
% \begin{syntax}
% |group-separator| = \meta{symbol}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-close-uncertainty}
% \begin{syntax}
% |input-close-uncertainty| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-comparators}
% \begin{syntax}
% |input-comparators| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-close-uncertainty}
% \begin{syntax}
% |input-close-uncertainty| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-decimal-markers}
% \begin{syntax}
% |input-decimal-markers| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-digits}
% \begin{syntax}
% |input-digits| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-exponent-markers}
% \begin{syntax}
% |input-exponent-markers| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-open-uncertainty}
% \begin{syntax}
% |input-open-uncertainty| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-signs}
% \begin{syntax}
% |input-signs| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{input-uncertainty-signs}
% \begin{syntax}
% |input-uncertainty-signs| = \meta{tokens}
% \end{syntax}
% \end{function}
%
% \begin{function}{minimum-decimal-digits}
% \begin{syntax}
% |minimum-decimal-digits| = \meta{min}
% \end{syntax}
% \end{function}
%
% \begin{function}{minimum-integer-digits}
% \begin{syntax}
% |minimum-integer-digits| = \meta{min}
% \end{syntax}
% \end{function}
%
% \begin{function}{negative-color}
% \begin{syntax}
% |negative-color| = \meta{color}
% \end{syntax}
% \end{function}
%
% \begin{function}{number-close-bracket}
% \begin{syntax}
% |number-close-bracket| = \meta{symbol}
% \end{syntax}
% \end{function}
%
% \begin{function}{number-open-bracket}
% \begin{syntax}
% |number-open-bracket| = \meta{symbol}
% \end{syntax}
% \end{function}
%
% \begin{function}{output-close-uncertainty}
% \begin{syntax}
% |output-close-uncertainty| = \meta{symbol}
% \end{syntax}
% \end{function}
%
% \begin{function}{output-decimal-marker}
% \begin{syntax}
% |output-decimal-marker| = \meta{symbol}
% \end{syntax}
% \end{function}
%
% \begin{function}{output-open-uncertainty}
% \begin{syntax}
% |output-open-uncertainty| = \meta{symbol}
% \end{syntax}
% \end{function}
%
% \begin{function}{parse-numbers}
% \begin{syntax}
% |parse-numbers| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{print-implicit-plus}
% \begin{syntax}
% |print-implicit-plus| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{print-unity-mantissa}
% \begin{syntax}
% |print-unity-mantissa| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{print-zero-exponent}
% \begin{syntax}
% |print-zero-exponent| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{round-half}
% \begin{syntax}
% |round-half| = |even|\verb"|"|up|
% \end{syntax}
% \end{function}
%
% \begin{function}{round-minimum}
% \begin{syntax}
% |round-minimum| = \meta{min}
% \end{syntax}
% \end{function}
%
% \begin{function}{round-mode}
% \begin{syntax}
% |round-mode| = |figures|\verb"|"|none|\verb"|"|places|\verb"|"|uncertainty|
% \end{syntax}
% \end{function}
%
% \begin{function}{round-pad}
% \begin{syntax}
% |round-pad| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{round-precision}
% \begin{syntax}
% |round-precision| = \meta{precision}
% \end{syntax}
% \end{function}
%
% \begin{function}{separate-uncertainty}
% \begin{syntax}
% |separate-uncertainty| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{track-explicit-plus}
% \begin{syntax}
% |track-explicit-plus| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{uncertainty-separator}
% \begin{syntax}
% |uncertainty-separator| = \meta{separator}
% \end{syntax}
% \end{function}
%
% \begin{function}{tight-spacing}
% \begin{syntax}
% |tight-spacing| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{siunitx-number} implementation}
%
% Start the \pkg{DocStrip} guards.
% \begin{macrocode}
%<*package>
% \end{macrocode}
%
% Identify the internal prefix (\LaTeX3 \pkg{DocStrip} convention): only
% internal material in this \emph{submodule} should be used directly.
% \begin{macrocode}
%<@@=siunitx_number>
% \end{macrocode}
%
% \subsection{Initial set-up}
%
% Variants not provided by \pkg{expl3}.
% \begin{macrocode}
\cs_generate_variant:Nn \tl_if_blank:nTF { f }
\cs_generate_variant:Nn \tl_if_blank_p:n { f }
\cs_generate_variant:Nn \tl_if_in:NnTF { NV }
\cs_generate_variant:Nn \tl_replace_all:Nnn { NnV }
% \end{macrocode}
%
% \begin{variable}{\l_@@_tmp_tl}
% Scratch space.
% \begin{macrocode}
\tl_new:N \l_@@_tmp_tl
% \end{macrocode}
% \end{variable}
%
% \subsection{Main formatting routine}
%
% \begin{variable}{\l_@@_formatted_tl}
% A token list for the final formatted result: may or may not be generated
% by the parser, depending on settings which are active.
% \begin{macrocode}
\tl_new:N \l_@@_formatted_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_siunitx_number_parse_bool}
% Tracks whether to parse numbers: public as this may affect other
% behaviors.
% \begin{macrocode}
\tl_new:N \l_siunitx_number_parse_bool
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_siunitx_number_parse_bool}
% Top-level options.
% \begin{macrocode}
\keys_define:nn { siunitx }
{
parse-numbers .bool_set:N = \l_siunitx_number_parse_bool
}
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\siunitx_number_format:nN, \siunitx_number_format:VN}
% \begin{macro}{\@@_format:nN}
% \begin{macrocode}
\cs_new_protected:Npn \siunitx_number_format:nN #1#2
{
\group_begin:
\bool_if:NTF \l_siunitx_number_parse_bool
{
\siunitx_number_parse:nN {#1} \l_@@_parsed_tl
\siunitx_number_process:NN \l_@@_parsed_tl \l_@@_parsed_tl
\tl_set:Nx \l_@@_formatted_tl
{ \siunitx_number_format:N \l_@@_parsed_tl }
}
{ \tl_set:Nn \l_@@_formatted_tl { \ensuremath {#1} } }
\exp_args:NNNV \group_end:
\tl_set:Nn #2 \l_@@_formatted_tl
}
\cs_generate_variant:Nn \siunitx_number_format:nN { V }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Parsing numbers}
%
% Before numbers can be manipulated or formatted they need to be parsed into
% an internal form. In particular, if multiple code paths are to be avoided,
% it is necessary to do such parsing even for relatively simple cases such
% as converting |1e10| to |1 \times 10^{10}|.
%
% Storing the result of such parsing can be done in a number of ways. In the
% first version of \pkg{siunitx} a series of separate data stores were used.
% This is potentially quite fast (as recovery of items relies only on \TeX{}'s
% hash table) but makes managing the various data entries somewhat tedious and
% error-prone. For version two of the package, a single data structure
% (property list) was used for each part of the parsed number. Whilst this is
% easy to manage and extend, it is somewhat slower as at a \TeX{} level there
% are repeated pack--unpack steps. In particular, the fact that there are a
% limited number of items to track for a \enquote{number} means that a more
% efficient approach is desirable (contrast parsing units, which is open-ended
% and therefore fits well with using a property list).
%
% In this release, the structure of a valid number is:
% \begin{quote}
% \marg{comparator}\meta{sign}\marg{integer}\marg{decimal}
% \marg{uncertainty}\\
% \meta{exponent sign}\marg{exponent}
% \end{quote}
% where the two sign parts must be single tokens and all other components
% must be given in braces. \emph{All} of the components must be present in
% a stored number (\foreign{i.e.}~at the end of parsing). The number must have
% at least one digit for both the \meta{integer} and \meta{exponent} parts.
%
% \begin{variable}{\l_siunitx_number_input_decimal_tl}
% The input decimal markers(s).
% \begin{macrocode}
\tl_new:N \l_siunitx_number_input_decimal_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}
% {
% \l_@@_expression_bool ,
% \l_@@_input_uncert_close_tl ,
% \l_@@_input_comparator_tl ,
% \l_@@_input_digit_tl ,
% \l_@@_input_exponent_tl ,
% \l_@@_input_ignore_tl ,
% \l_@@_input_uncert_open_tl ,
% \l_@@_input_sign_tl ,
% \l_@@_input_uncert_sign_tl ,
% \l_@@_explicit_plus_bool
% }
% \begin{macro}[EXP]{\@@_expression:n}
% Options which determine the various valid parts of a parsed number.
% \begin{macrocode}
\keys_define:nn { siunitx }
{
evaluate-expression .bool_set:N =
\l_@@_expression_bool ,
expression .code:n =
\cs_set:Npn \@@_expression:n ##1 {#1} ,
input-close-uncertainty .tl_set:N =
\l_@@_input_uncert_close_tl ,
input-comparators .tl_set:N =
\l_@@_input_comparator_tl ,
input-decimal-markers .tl_set:N =
\l_siunitx_number_input_decimal_tl ,
input-digits .tl_set:N =
\l_@@_input_digit_tl ,
input-exponent-markers .tl_set:N =
\l_@@_input_exponent_tl ,
input-ignore .tl_set:N =
\l_@@_input_ignore_tl ,
input-open-uncertainty .tl_set:N =
\l_@@_input_uncert_open_tl ,
input-signs .tl_set:N =
\l_@@_input_sign_tl ,
input-uncertainty-signs .code:n =
{
\tl_set:Nn \l_@@_input_uncert_sign_tl {#1}
\tl_map_inline:nn {#1}
{
\tl_if_in:NnF \l_@@_input_sign_tl {##1}
{ \tl_put_right:Nn \l_@@_input_sign_tl {##1} }
}
} ,
parse-numbers .bool_set:N =
\l_siunitx_number_parse_bool ,
track-explicit-plus .bool_set:N =
\l_@@_explicit_plus_bool
}
\cs_new:Npn \@@_expression:n #1 { }
\tl_new:N \l_@@_input_uncert_sign_tl
% \end{macrocode}
% \end{macro}
% \end{variable}
%
% \begin{variable}{\l_@@_arg_tl}
% The input argument or a part thereof, depending on the position in
% the parsing routine.
% \begin{macrocode}
\tl_new:N \l_@@_arg_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_comparator_tl}
% A comparator, if found, is held here.
% \begin{macrocode}
\tl_new:N \l_@@_comparator_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_exponent_tl}
% The exponent part of a parsed number. It is easiest to find this
% relatively early in the parsing process, but as it needs to go at
% the end of the internal format is held separately until required.
% \begin{macrocode}
\tl_new:N \l_@@_exponent_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_flex_tl}
% In a number with an uncertainty, the exact meaning of a second part is
% not fully resolved until parsing is complete. That is handled using
% this \enquote{flexible} store.
% \begin{macrocode}
\tl_new:N \l_@@_flex_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_parsed_tl}
% The number parsed into internal format.
% \begin{macrocode}
\tl_new:N \l_@@_parsed_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_input_tl}
% The numerical input exactly as given by the user.
% \begin{macrocode}
\tl_new:N \l_@@_input_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_partial_tl}
% To avoid needing to worry about the fact that the final data stores are
% somewhat tricky to add to token-by-token, a simple store is used to build
% up the parsed part of a number before transferring in one go.
% \begin{macrocode}
\tl_new:N \l_@@_partial_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_validate_bool}
% Used to set up for validation with no error production.
% \begin{macrocode}
\bool_new:N \l_@@_validate_bool
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\siunitx_number_parse:nN}
% \begin{macro}{\@@_parse:nN}
% After some initial set up, the parser expands the input and then replaces
% as far as possible tricky tokens with ones that can be handled using
% delimited arguments. To avoid multiple conditionals here, the parser is
% set up as a chain of commands initially, with a loop only later. This
% avoids more conditionals than are necessary.
% \begin{macrocode}
\cs_new_protected:Npn \siunitx_number_parse:nN #1#2
{
\bool_if:NTF \l_siunitx_number_parse_bool
{ \@@_parse:nN {#1} #2 }
{ \tl_clear:N #2 }
}
\cs_new_protected:Npn \@@_parse:nN #1#2
{
\group_begin:
\tl_clear:N \l_@@_parsed_tl
\protected@edef \l_@@_arg_tl
{
\bool_if:NTF \l_@@_expression_bool
{ \fp_eval:n { \@@_expression:n {#1} } }
{#1}
}
\tl_set_eq:NN \l_@@_input_tl \l_@@_arg_tl
\@@_parse_replace:
\tl_if_empty:NF \l_@@_arg_tl
{ \@@_parse_comparator: }
\@@_parse_check:
\exp_args:NNNV \group_end:
\tl_set:Nn #2 \l_@@_parsed_tl
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_check:}
% After the loop there is one case that might need tidying up. If a
% separated uncertainty was found it will be currently in \cs{l_@@_flex_tl}
% and needs moving. A series of tests pick up that case, then the check is
% made that some content was found
% \begin{macrocode}
\cs_new_protected:Npn \@@_parse_check:
{
\tl_if_empty:NF \l_@@_flex_tl
{
\bool_lazy_and:nnTF
{
\tl_if_blank_p:f
{ \exp_after:wN \use_iv:nnnn \l_@@_parsed_tl }
}
{
\tl_if_blank_p:f
{ \exp_after:wN \use_iv:nnnn \l_@@_flex_tl }
}
{
\tl_set:Nx \l_@@_tmp_tl
{ \exp_after:wN \use_i:nnnn \l_@@_flex_tl }
\tl_if_in:NVTF \l_@@_input_uncert_sign_tl
\l_@@_tmp_tl
{ \@@_parse_combine_uncert: }
{ \tl_clear:N \l_@@_parsed_tl }
}
{ \tl_clear:N \l_@@_parsed_tl }
}
\tl_if_empty:NTF \l_@@_parsed_tl
{
\bool_if:NF \l_@@_validate_bool
{
\msg_error:nnx { siunitx } { invalid-number }
{ \exp_not:V \l_@@_input_tl }
}
}
{ \@@_parse_finalise: }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_parse_combine_uncert:}
% \begin{macro}{\@@_parse_combine_uncert_auxi:nnnnnnnn}
% \begin{macro}
% {
% \@@_parse_combine_uncert_auxii:nnnnn,
% \@@_parse_combine_uncert_auxii:fnnnn
% }
% \begin{macro}
% {
% \@@_parse_combine_uncert_auxiii:nnnnnn,
% \@@_parse_combine_uncert_auxiii:fnnnnn
% }
% \begin{macro}{\@@_parse_combine_uncert_auxiv:nnnn}
% \begin{macro}[EXP]{\@@_parse_combine_uncert_auxv:w}
% \begin{macro}[EXP]{\@@_parse_combine_uncert_auxvi:w}
% Conversion of a second numerical part to an uncertainty needs a bit of
% work. The first step is to extract the useful information from the two
% stores: the sign, integer and decimal parts from the real number and the
% integer and decimal parts from the second number. That is done using the
% input stack to avoid lots of assignments.
% \begin{macrocode}
\cs_new_protected:Npn \@@_parse_combine_uncert:
{
\exp_after:wN \exp_after:wN \exp_after:wN
\@@_parse_combine_uncert_auxi:nnnnnnnn
\exp_after:wN \l_@@_parsed_tl \l_@@_flex_tl
}
% \end{macrocode}
% Here, |#4|, |#5| and |#8| are all junk arguments simply there to mop up
% tokens, while |#1| will be recovered later from \cs{l_@@_parsed_tl} so does
% not need to be passed about. The difference in places between the two
% decimal parts is now found: this is done just once to avoid having to
% parse token lists twice. The value is then used to generate a number of
% filler |0| tokens, and these are added to the appropriate part of the
% number. Finally, everything is recombined: the integer part only needs
% a test to avoid an empty main number.
% \begin{macrocode}
\cs_new_protected:Npn
\@@_parse_combine_uncert_auxi:nnnnnnnn #1#2#3#4#5#6#7#8
{
\int_compare:nNnTF { \tl_count:n {#6} } > { \tl_count:n {#2} }
{
\tl_clear:N \l_@@_parsed_tl
\tl_clear:N \l_@@_flex_tl
}
{
\@@_parse_combine_uncert_auxii:fnnnn
{ \int_eval:n { \tl_count:n {#3} - \tl_count:n {#7} } }
{#2} {#3} {#6} {#7}
}
}
\cs_new_protected:Npn
\@@_parse_combine_uncert_auxii:nnnnn #1
{
\@@_parse_combine_uncert_auxiii:fnnnnn
{ \prg_replicate:nn { \int_abs:n {#1} } { 0 } }
{#1}
}
\cs_generate_variant:Nn \@@_parse_combine_uncert_auxii:nnnnn { f }
\cs_new_protected:Npn
\@@_parse_combine_uncert_auxiii:nnnnnn #1#2#3#4#5#6
{
\int_compare:nNnTF {#2} > 0
{
\@@_parse_combine_uncert_auxiv:nnnn
{#3} {#4} {#5} { #6 #1 }
}
{
\@@_parse_combine_uncert_auxiv:nnnn
{#3} { #4 #1 } {#5} {#6}
}
}
\cs_generate_variant:Nn
\@@_parse_combine_uncert_auxiii:nnnnnn { f }
\cs_new_protected:Npn
\@@_parse_combine_uncert_auxiv:nnnn #1#2#3#4
{
\tl_set:Nx \l_@@_parsed_tl
{
{ \tl_head:V \l_@@_parsed_tl }
{ \exp_not:n {#1} }
{
\bool_lazy_and:nnTF
{ \tl_if_blank_p:n {#2} }
{ ! \tl_if_blank_p:n {#4} }
{ 0 }
{ \exp_not:n {#2} }
}
{
\@@_parse_combine_uncert_auxv:w #3#4
\q_recursion_tail \q_recursion_stop
}
}
}
% \end{macrocode}
% A short routine to remove any leading zeros in the uncertainty part,
% which are not needed for the compact representation used by the module.
% \begin{macrocode}
\cs_new:Npn \@@_parse_combine_uncert_auxv:w #1
{
\quark_if_recursion_tail_stop:N #1
\str_if_eq:nnTF {#1} { 0 }
{ \@@_parse_combine_uncert_auxv:w }
{ \@@_parse_combine_uncert_auxvi:w #1 }
}
\cs_new:Npn \@@_parse_combine_uncert_auxvi:w
#1 \q_recursion_tail \q_recursion_stop
{
\tl_if_blank:nF {#1}
{ { S } { \exp_not:n {#1} } }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_comparator:}
% \begin{macro}{\@@_parse_comparator_aux:Nw}
% A comparator has to be the very first token in the input. A such, the
% test for this can be very fast: grab the first token, do a check and
% if appropriate store the result.
% \begin{macrocode}
\cs_new_protected:Npn \@@_parse_comparator:
{
\exp_after:wN \@@_parse_comparator_aux:Nw
\l_@@_arg_tl \q_stop
}
\cs_new_protected:Npn \@@_parse_comparator_aux:Nw #1#2 \q_stop
{
\tl_if_in:NnTF \l_@@_input_comparator_tl {#1}
{
\tl_set:Nn \l_@@_comparator_tl {#1}
\tl_set:Nn \l_@@_arg_tl {#2}
}
{ \tl_clear:N \l_@@_comparator_tl }
\tl_if_empty:NF \l_@@_arg_tl
{ \@@_parse_sign: }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_exponent:}
% \begin{macro}{\@@_parse_exponent_auxi:w}
% \begin{macro}{\@@_parse_exponent_auxii:nn}
% \begin{macro}{\@@_parse_exponent_auxiii:Nw}
% \begin{macro}{\@@_parse_exponent_auxiv:nn}
% \begin{macro}
% {\@@_parse_exponent_zero_test:N, \@@_parse_exponent_check:N}
% \begin{macro}{\@@_parse_exponent_cleanup:N}
% An exponent part of a number has to come at the end and can only occur
% once. Thus it is relatively easy to parse. First, there is a check that
% an exponent part is allowed, and if so a split is made (the previous
% part of the chain checks that there is some content in \cs{l_@@_arg_tl}
% before calling this function). After splitting, if there is no exponent
% then simply save a default. Otherwise, check for a sign and then store
% either this or an implicit plus, and the digits after a check that nothing
% else is present after the~|e|. The only slight complication to all of
% this is allowing an arbitrary token in the input to represent the exponent:
% this is done by setting any exponent tokens to the first of the allowed
% list, then using that in a delimited argument set up. Once an exponent
% part is found, there is a loop to check that each of the tokens is a digit
% then a tidy up step to remove any leading zeros.
% \begin{macrocode}
\cs_new_protected:Npn \@@_parse_exponent:
{
\tl_if_empty:NTF \l_@@_input_exponent_tl
{
\tl_set:Nn \l_@@_exponent_tl { { } 0 }
\tl_if_empty:NF \l_@@_parsed_tl
{ \@@_parse_loop: }
}
{
\tl_set:Nx \l_@@_tmp_tl
{ \tl_head:V \l_@@_input_exponent_tl }
\tl_map_inline:Nn \l_@@_input_exponent_tl
{
\tl_replace_all:NnV \l_@@_arg_tl
{##1} \l_@@_tmp_tl
}
\use:x
{
\cs_set_protected:Npn
\exp_not:N \@@_parse_exponent_auxi:w
####1 \exp_not:V \l_@@_tmp_tl
####2 \exp_not:V \l_@@_tmp_tl
####3 \exp_not:N \q_stop
}
{ \@@_parse_exponent_auxii:nn {##1} {##2} }
\use:x
{
\@@_parse_exponent_auxi:w
\exp_not:V \l_@@_arg_tl
\exp_not:V \l_@@_tmp_tl \exp_not:N \q_nil
\exp_not:V \l_@@_tmp_tl \exp_not:N \q_stop
}
}
}
\cs_new_protected:Npn \@@_parse_exponent_auxi:w { }
\cs_new_protected:Npn \@@_parse_exponent_auxii:nn #1#2
{
\quark_if_nil:nTF {#2}
{ \tl_set:Nn \l_@@_exponent_tl { { } 0 } }
{
\tl_set:Nn \l_@@_arg_tl {#1}
\tl_if_blank:nTF {#2}
{ \tl_clear:N \l_@@_parsed_tl }
{ \@@_parse_exponent_auxiii:Nw #2 \q_stop }
}
\tl_if_empty:NF \l_@@_parsed_tl
{ \@@_parse_loop: }
}
\cs_new_protected:Npn \@@_parse_exponent_auxiii:Nw #1#2 \q_stop
{
\tl_if_in:NnTF \l_@@_input_sign_tl {#1}
{ \@@_parse_exponent_auxiv:nn {#1} {#2} }
{ \@@_parse_exponent_auxiv:nn { } {#1#2} }
\tl_if_empty:NT \l_@@_exponent_tl
{ \tl_clear:N \l_@@_parsed_tl }
}
\cs_new_protected:Npn \@@_parse_exponent_auxiv:nn #1#2
{
\bool_lazy_or:nnTF
{ \l_@@_explicit_plus_bool }
{ ! \str_if_eq_p:nn {#1} { + } }
{ \tl_set:Nn \l_@@_exponent_tl { {#1} } }
{ \tl_set:Nn \l_@@_exponent_tl { { } } }
\tl_if_blank:nTF {#2}
{ \tl_clear:N \l_@@_parsed_tl }
{
\@@_parse_exponent_zero_test:N #2
\q_recursion_tail \q_recursion_stop
}
}
\cs_new_protected:Npn \@@_parse_exponent_zero_test:N #1
{
\quark_if_recursion_tail_stop_do:Nn #1
{ \tl_set:Nn \l_@@_exponent_tl { { } 0 } }
\str_if_eq:nnTF {#1} { 0 }
{ \@@_parse_exponent_zero_test:N }
{ \@@_parse_exponent_check:N #1 }
}
\cs_new_protected:Npn \@@_parse_exponent_check:N #1
{