forked from josephwright/siunitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siunitx-table.dtx
1339 lines (1339 loc) · 40.2 KB
/
siunitx-table.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-table.dtx Copyright (C) 2016-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-table} -- Formatting numbers in tables^^A
% \thanks{This file describes \fileversion,
% last revised \filedate.}^^A
% }
%
% \author{^^A
% Joseph Wright^^A
% \thanks{^^A
% E-mail:
% \href{mailto:[email protected]}
% {[email protected]}^^A
% }^^A
% }
%
% \date{Released \filedate}
%
% \maketitle
%
% \begin{documentation}
%
% \section{Numbers in tables}
%
% \begin{function}{\siunitx_cell_begin:w, \siunitx_cell_end:}
% \begin{syntax}
% \cs{siunitx_cell_begin:w} \meta{preamble} \cs{ignorespaces}
% \meta{content}
% \cs{siunitx_cell_end:}
% \end{syntax}
% Collects the \meta{preamble} and \meta{content} tokens, and determines
% if it is text or a number (as parsed by \cs{siunitx_number_parse:nN}).
% It produces output of a fixed width suitable for alignment in a table,
% although it is not \emph{required} that the code is used within a cell.
% Note that |\ignorespaces| must occur in the \enquote{cell}: it marks
% the end of the \TeX{} |\halign| template.
% \end{function}
%
% \subsection{Key--value options}
%
% The options defined by this submodule are available within the \pkg{l3keys}
% |siunitx| tree.
%
% \begin{function}{table-align-comparator}
% \begin{syntax}
% |table-align-comparator| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-align-exponent}
% \begin{syntax}
% |table-align-exponent| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-align-text-after}
% \begin{syntax}
% |table-align-text-after| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-align-text-before}
% \begin{syntax}
% |table-align-text-before| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-align-uncertainty}
% \begin{syntax}
% |table-align-uncertainty| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-alignment}
% \begin{syntax}
% |table-alignment| = |center|\verb"|"|left|\verb"|"|right|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-alignment-mode}
% \begin{syntax}
% |table-alignment-mode| = |format|\verb"|"|marker|\verb"|"|none|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-auto-round}
% \begin{syntax}
% |table-auto-round| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-column-width}
% \begin{syntax}
% |table-column-width| = \meta{width}
% \end{syntax}
% \end{function}
%
% \begin{function}{table-fixed-width}
% \begin{syntax}
% |table-fixed-width| = |true|\verb"|"|false|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-format}
% \begin{syntax}
% |table-format| = \meta{format}
% \end{syntax}
% \end{function}
%
% \begin{function}{table-number-alignment}
% \begin{syntax}
% |table-number-alignment| = |center|\verb"|"|left|\verb"|"|right|
% \end{syntax}
% \end{function}
%
% \begin{function}{table-text-alignment}
% \begin{syntax}
% |table-text-alignment| = |center|\verb"|"|left|\verb"|"|right|
% \end{syntax}
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{siunitx-table} 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_table>
% \end{macrocode}
%
% \begin{variable}{\l_@@_tmp_box, \l_@@_tmp_dim, \l_@@_tmp_tl}
% Scratch space.
% \begin{macrocode}
\box_new:N \l_@@_tmp_box
\dim_new:N \l_@@_tmp_dim
\tl_new:N \l_@@_tmp_tl
% \end{macrocode}
% \end{variable}
%
% \subsection{Interface functions}
%
% \begin{variable}{\l_@@_text_bool}
% Used to track that a cell is purely text.
% \begin{macrocode}
\bool_new:N \l_@@_text_bool
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\siunitx_cell_begin:w}
% \begin{macro}{\siunitx_cell_end:}
% The start and end of the cell need to deal with the possibility of
% a cell containing only text.
% \begin{macrocode}
\cs_new_protected:Npn \siunitx_cell_begin:w
{
\bool_set_false:N \l_@@_text_bool
\bool_if:NTF \l_siunitx_number_parse_bool
{ \@@_collect_begin: }
{ \@@_direct_begin: }
}
\cs_new_protected:Npn \siunitx_cell_end:
{
\bool_if:NF \l_@@_text_bool
{
\bool_if:NTF \l_siunitx_number_parse_bool
{ \@@_collect_end: }
{ \@@_direct_end: }
}
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Collecting tokens}
%
% \begin{variable}{\l_@@_collect_tl}
% Space for tokens.
% \begin{macrocode}
\tl_new:N \l_@@_collect_tl
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_collect_begin:}
% \begin{macro}{\@@_collect_begin:w}
% Collecting a tabular cell means doing a token-by-token collection.
% In previous versions of \pkg{siunitx} that was done along with picking
% out the numerical part, but the code flow ends up very tricky. Here,
% therefore, we just collect up the unchanged tokens first. The definition of
% \cs{cr} is used to allow collection of any tokens
% inserted after the main content when dealing with the last cell of a row:
% the \enquote{group} around it is needed to avoid issues with the underlying
% |\halign|. (The approach is based on that in \pkg{collcell}.) Notice that
% as each cell forms a group there is no need to reset the definition of
% \cs{cr}. We use an auxiliary to fish out the |\ignorespaces| from the
% template: that has to go to avoid issues with the peek-ahead code
% (everything before the |#| needs to be read \emph{before} the Appendix~D
% trick gets applied). Some packages add additional tokens before the
% |\ignorespaces|, which are dealt with by the delimited argument.
% \begin{macrocode}
\cs_new_protected:Npn \@@_collect_begin:
{
\tl_clear:N \l_@@_collect_tl
\if_false: { \fi:
\cs_set_protected:Npn \cr
{
\@@_collect_loop:
\tex_cr:D
}
\if_false: } \fi:
\@@_collect_begin:w
}
\cs_new_protected:Npn \@@_collect_begin:w #1 \ignorespaces
{ \@@_collect_loop: #1 }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_collect_loop:}
% \begin{macro}{\@@_collect_group:n}
% \begin{macro}{\@@_collect_token:N}
% \begin{macro}{\@@_collect_search:NnF}
% \begin{macro}{\@@_collect_search_aux:NNn}
% Collecting up the cell content needs a loop: this is done using
% a |peek| approach as it's most natural. (A slower approach is possible
% using something like the |\text_lowercase:n| loop code.) The set of
% possible tokens is somewhat limited compared to an arbitrary cell
% (\foreign{cf.}~the approach in \pkg{collcell}): the special cases are
% pulled out for manual handling. The flexible lookup approach is more-or-less
% the same idea as in the kernel |case| functions. The |\relax| special case
% covers the case where |\\| has been expanded in an empty cell.
% \begin{macrocode}
\cs_new_protected:Npn \@@_collect_loop:
{
\peek_catcode_ignore_spaces:NTF \c_group_begin_token
{ \@@_collect_group:n }
{ \@@_collect_token:N }
}
\cs_new_protected:Npn \@@_collect_group:n #1
{
\tl_put_right:Nn \l_@@_collect_tl { {#1} }
\@@_collect_loop:
}
\cs_new_protected:Npn \@@_collect_token:N #1
{
\@@_collect_search:NnF #1
{
\unskip { \@@_collect_loop: }
\end { \tabularnewline \end }
\relax { \relax }
\tabularnewline { \tabularnewline }
\siunitx_cell_end: { \siunitx_cell_end: }
}
{
\tl_put_right:Nn \l_@@_collect_tl {#1}
\@@_collect_loop:
}
}
\AtBeginDocument
{
\@ifpackageloaded { mdwtab }
{
\cs_set_protected:Npn \@@_collect_token:N #1
{
\@@_collect_search:NnF #1
{
\@maybe@unskip { \@@_collect_loop: }
\tab@setcr { \@@_collect_loop: }
\unskip { \@@_collect_loop: }
\end { \tabularnewline \end }
\relax { \relax }
\tabularnewline { \tabularnewline }
\siunitx_cell_end: { \siunitx_cell_end: }
}
{
\tl_put_right:Nn \l_@@_collect_tl {#1}
\@@_collect_loop:
}
}
}
{ }
}
\cs_new_protected:Npn \@@_collect_search:NnF #1#2#3
{
\@@_collect_search_aux:NNn #1
#2
#1 {#3}
\q_stop
}
\cs_new_protected:Npn \@@_collect_search_aux:NNn #1#2#3
{
\token_if_eq_meaning:NNTF #1 #2
{ \use_i_delimit_by_q_stop:nw {#3} }
{ \@@_collect_search_aux:NNn #1 }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Separating collected material}
%
% The input needs to be divided into numerical tokens and those which appear
% before and after them. This needs a second loop and validation.
%
% \begin{variable}{\l_@@_before_tl, \l_@@_number_tl, \l_@@_after_tl}
% Space for tokens.
% \begin{macrocode}
\tl_new:N \l_@@_before_tl
\tl_new:N \l_@@_number_tl
\tl_new:N \l_@@_after_tl
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_collect_end:}
% At the end of the cell, expand all of the content as far as possible then
% split it up into numerical and non-numerical parts.
% \begin{macrocode}
\cs_new_protected:Npn \@@_collect_end:
{
\protected@edef \l_@@_collect_tl
{ \l_@@_collect_tl }
\exp_args:NV \@@_split:nNNN
\l_@@_collect_tl
\l_@@_before_tl
\l_@@_number_tl
\l_@@_after_tl
\tl_if_empty:NTF \l_@@_number_tl
{ \@@_print_text:V \l_@@_before_tl }
{
\@@_print:VVV
\l_@@_before_tl
\l_@@_number_tl
\l_@@_after_tl
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_split:nNNN}
% \begin{macro}{\@@_split_loop:NNN}
% \begin{macro}{\@@_split_group:NNNn}
% \begin{macro}{\@@_split_token:NNNN}
% Splitting into parts uses the fact that numbers cannot contain groups
% and that we can track where we are up to based on the content of the
% token lists.
% \begin{macrocode}
\cs_new_protected:Npn \@@_split:nNNN #1#2#3#4
{
\tl_clear:N #2
\tl_clear:N #3
\tl_clear:N #4
\@@_split_loop:NNN #2#3#4 #1 \q_recursion_tail \q_recursion_stop
\@@_split_tidy:N #2
\@@_split_tidy:N #4
}
\cs_new_protected:Npn \@@_split_loop:NNN #1#2#3
{
\peek_catcode_ignore_spaces:NTF \c_group_begin_token
{ \@@_split_group:NNNn #1#2#3 }
{ \@@_split_token:NNNN #1#2#3 }
}
\cs_new_protected:Npn \@@_split_group:NNNn #1#2#3#4
{
\tl_if_empty:NTF #2
{ \tl_put_right:Nn #1 { {#4} } }
{ \tl_put_right:Nn #3 { {#4} } }
\@@_split_loop:NNN #1#2#3
}
\cs_new_protected:Npn \@@_split_token:NNNN #1#2#3#4
{
\quark_if_recursion_tail_stop:N #4
\tl_if_empty:NTF \l_@@_after_tl
{
\siunitx_if_number_token:NTF #4
{ \tl_put_right:Nn #2 {#4} }
{
\tl_if_empty:NTF #2
{ \tl_put_right:Nn #1 {#4} }
{ \tl_put_right:Nn #3 {#4} }
}
}
{ \tl_put_right:Nn #3 {#4} }
\@@_split_loop:NNN #1#2#3
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_split_tidy:N}
% \begin{macro}{\@@_split_tidy:Nn, \@@_split_tidy:NV}
% A quick test for the entire content being surrounded by a set of braces:
% rather than look explicitly, use the fact that a string comparison can
% detect the same thing. The auxiliary is needed to avoid having to go
% \foreign{via} a |:D| function (for the expansion behaviour).
% \begin{macrocode}
\cs_new_protected:Npn \@@_split_tidy:N #1
{
\tl_if_empty:NF #1
{ \@@_split_tidy:NV #1 #1 }
}
\cs_new_protected:Npn \@@_split_tidy:Nn #1#2
{
\str_if_eq:onT { \exp_after:wN { \use:n #2 } } {#2}
{ \tl_set:No #1 { \use:n #2 } }
}
\cs_generate_variant:Nn \@@_split_tidy:Nn { NV }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Printing numbers in cells: spacing}
%
% Getting the general alignment correct in tables is made more complex than one
% would like by the \pkg{colortbl} package. In the original \LaTeXe{}
% definition, cell material is centred by a construction of the (primitive)
% form
% \begin{verbatim}
% \hfil
% #
% \hfil
% \end{verbatim}
% which only uses \texttt{fil} stretch. That is altered by \pkg{colortbl} to
% broadly
% \begin{verbatim}
% \hskip 0pt plus 0.5fill
% \kern 0pt
% #
% \hskip 0pt plus 0.5fill
% \end{verbatim}
% which means there is \texttt{fill} stretch to worry about and the kern as
% well.
%
% \begin{macro}{\@@_skip:n}
% To prevent combination of skips, a kern is inserted after each one.
% This is best handled as a short auxiliary.
% \begin{macrocode}
\cs_new_protected:Npn \@@_skip:n #1
{
\skip_horizontal:n {#1}
\tex_kern:D \c_zero_skip
}
% \end{macrocode}
% \end{macro}
%
% \begin{variable}{\l_@@_column_width_dim, \l_@@_fixed_width_bool}
% Settings which apply to aligned columns in general.
% \begin{macrocode}
\keys_define:nn { siunitx }
{
table-column-width .dim_set:N =
\l_@@_column_width_dim ,
table-fixed-width .bool_set:N =
\l_@@_fixed_width_bool
}
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_align_center:n, \@@_align_left:n, \@@_align_right:n}
% \begin{macro}{\@@_align_auxi:nn, \@@_align_auxii:nn}
% The beginning and end of each table cell have to adjust the position of
% the content using glue. When \pkg{colortbl} is loaded the glue is done in
% two parts: one for our positioning and one to explicitly override that from
% the package. Using a two-step auxiliary chain avoids needing to repeat any
% code and the impact of the extra expansion should be trivial.
% \begin{macrocode}
\cs_new_protected:Npn \@@_align_center:n #1
{ \@@_align_auxi:nn {#1} { 0pt~plus~0.5fill } }
\cs_new_protected:Npn \@@_align_left:n #1
{ \@@_align_auxi:nn {#1} { 0pt } }
\cs_new_protected:Npn \@@_align_right:n #1
{ \@@_align_auxi:nn {#1} { 0pt~plus~1fill } }
\cs_new_protected:Npn \@@_align_auxi:nn #1#2
{
\bool_if:NTF \l_@@_fixed_width_bool
{ \hbox_to_wd:nn \l_@@_column_width_dim }
{ \use:n }
{
\@@_skip:n {#2}
#1
\@@_skip:n { 0pt~plus~1fill - #2 }
}
}
\AtBeginDocument
{
\@ifpackageloaded { colortbl }
{
\cs_new_eq:NN
\@@_align_auxii:nn
\@@_align_auxi:nn
\cs_set_protected:Npn \@@_align_auxi:nn #1#2
{
\@@_skip:n{ 0pt~plus~-0.5fill }
\@@_align_auxii:nn {#1} {#2}
\@@_skip:n { 0pt~plus~-0.5fill }
}
}
{ }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Printing just text}
%
% In cases where there is no numerical part, \pkg{siunitx} allows alignment
% of the \enquote{escaped} text independent of the underlying column type.
%
% \begin{variable}{\l_@@_align_text_tl}
% Alignment is handled using a |tl| as this allows a fast lookup at the
% point of use.
% \begin{macrocode}
\keys_define:nn { siunitx }
{
table-text-alignment .choices:nn =
{ center , left , right }
{ \tl_set:Nn \l_@@_align_text_tl {#1} } ,
}
\tl_new:N \l_@@_align_text_tl
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_print_text:n, \@@_print_text:V}
% Printing escaped text is easy: just place it in correctly in the
% column.
% \begin{macrocode}
\cs_new_protected:Npn \@@_print_text:n #1
{
\bool_set_true:N \l_@@_text_bool
\use:c { @@_align_ \l_@@_align_text_tl :n } {#1}
}
\cs_generate_variant:Nn \@@_print_text:n { V }
% \end{macrocode}
% \end{macro}
%
% \subsection{Number alignment: core ideas}
%
% \begin{variable}{\l_@@_integer_box, \l_@@_decimal_box}
% Boxes for the content before and after the decimal marker.
% \begin{macrocode}
\box_new:N \l_@@_integer_box
\box_new:N \l_@@_decimal_box
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_fil:}
% A primitive renamed.
% \begin{macrocode}
\cs_new_eq:NN \@@_fil: \tex_hfil:D
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_cleanup_decimal:w}
% To remove the excess marker tokens in a decimal part.
% \begin{macrocode}
\cs_new:Npn \@@_cleanup_decimal:w
#1 \q_nil #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_nil #6 \q_nil #7 \q_nil
{ #1#2#3#4#5#6#7 }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_center_marker:}
% When centering on the decimal marker, the easiest approach is to simply
% re-box the two parts. That is needed whether or not we are parsing numbers,
% so is best as a short auxiliary.
% \begin{macrocode}
\cs_new_protected:Npn \@@_center_marker:
{
\dim_compare:nNnTF
{ \box_wd:N \l_@@_integer_box }
> { \box_wd:N \l_@@_decimal_box }
{
\hbox_set_to_wd:Nnn \l_@@_decimal_box
{ \box_wd:N \l_@@_integer_box }
{
\hbox_unpack:N \l_@@_decimal_box
\@@_fil:
}
}
{
\hbox_set_to_wd:Nnn \l_@@_integer_box
{ \box_wd:N \l_@@_decimal_box }
{
\@@_fil:
\hbox_unpack:N \l_@@_integer_box
}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{variable}
% {\l_@@_auto_round_bool, \l_@@_align_mode_tl, \l_@@_align_number_tl}
% Options for tables with defined space.
% \begin{macrocode}
\keys_define:nn { siunitx }
{
table-alignment .meta:n =
{ table-number-alignment = #1 , table-text-alignment = #1 },
table-alignment-mode .choices:nn =
{ none , format , marker }
{ \tl_set_eq:NN \l_@@_align_mode_tl \l_keys_choice_tl } ,
table-auto-round .bool_set:N =
\l_@@_auto_round_bool ,
table-format .code:n =
{
\@@_split:nNNN {#1}
\l_@@_before_model_tl
\l_@@_model_tl
\l_@@_after_model_tl
\exp_args:NV \@@_generate_model:n \l_@@_model_tl
\tl_set:Nn \l_@@_align_mode_tl { format }
} ,
table-number-alignment .choices:nn =
{ center , left , right }
{ \tl_set_eq:NN \l_@@_align_number_tl \l_keys_choice_tl }
}
\tl_new:N \l_@@_align_mode_tl
\tl_new:N \l_@@_align_number_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_format_tl, \l_@@_model_tl}
% The input and output versions of the model entry in a table.
% \begin{macrocode}
\tl_new:N \l_@@_format_tl
\tl_new:N \l_@@_before_model_tl
\tl_new:N \l_@@_model_tl
\tl_new:N \l_@@_after_model_tl
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_generate_model:n}
% \begin{macro}{\@@_generate_model:nnnnnnn}
% \begin{macro}{\@@_generate_model_S:nw}
% Creating a model for a table at this stage means parsing the format and
% converting that to an appropriate model. Things are quite straight-forward
% other than the uncertainty part. At this stage there is no point in
% formatting the model: that has to happen at point-of-use.
% \begin{macrocode}
\cs_new_protected:Npn \@@_generate_model:n #1
{
\group_begin:
\bool_set_true:N \l_siunitx_number_parse_bool
\keys_set:nn { siunitx } { track-explicit-plus = true }
\siunitx_number_parse:nN {#1} \l_@@_format_tl
\exp_args:NNNV \group_end:
\tl_set:Nn \l_@@_format_tl \l_@@_format_tl
\tl_if_empty:NF \l_@@_format_tl
{
\exp_after:wN \@@_generate_model:nnnnnnn
\l_@@_format_tl
}
}
\cs_new_protected:Npn \@@_generate_model:nnnnnnn #1#2#3#4#5#6#7
{
\tl_set:Nx \l_@@_model_tl
{
\exp_not:n { {#1} {#2} }
{ \prg_replicate:nn {#3} { 8 } }
{ \prg_replicate:nn { 0 #4 } { 8 } }
{
\tl_if_blank:nF {#5}
{
\use:c { @@_generate_model_ \tl_head:n {#5} :nw }
#5
}
}
\exp_not:n { {#6} }
{
\int_compare:nNnTF {#7} = 0
{ 0 }
{ \prg_replicate:nn {#7} { 8 } }
}
}
}
\cs_new:Npn \@@_generate_model_S:nw #1#2
{ { S } { \prg_replicate:nn {#2} { 8 } } }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Directly printing without collection}
%
% Collecting the number allows for various effects but is not as fast
% as simply aligning on the first token that is a decimal marker. The
% strategy here is that used by \pkg{dcolumn}.
%
% \begin{macro}{\@@_direct_begin:}
% \begin{macro}{\@@_direct_begin:w}
% \begin{macro}{\@@_direct_end:}
% \begin{macro}
% {
% \@@_direct_marker: ,
% \@@_direct_marker_switch: ,
% \@@_direct_marker_end:
% }
% \begin{macro}{\@@_direct_format:}
% \begin{macro}[EXP]{\@@_direct_format:nnnnnnn}
% \begin{macro}{\@@_direct_format:w}
% \begin{macro}
% {
% \@@_direct_format_switch: ,
% \@@_direct_format_end:
% }
% \begin{macro}{\@@_direct_none:, \@@_direct_none_end:}
% After removing the |\ignorespaces| at the start of the cell (see comments
% for \cs{@@_collect_begin:N}), check to see if there is a |{| and branch
% as appropriate.
% \begin{macrocode}
\cs_new_protected:Npn \@@_direct_begin:
{ \@@_direct_begin:w }
\cs_new_protected:Npn \@@_direct_begin:w #1 \ignorespaces
{
#1
\peek_catcode_ignore_spaces:NTF \c_group_begin_token
{ \@@_print_text:n }
{
\m@th
\use:c { @@_direct_ \l_@@_align_mode_tl : }
}
}
\cs_new_protected:Npn \@@_direct_end:
{ \use:c { @@_direct_ \l_@@_align_mode_tl _end: } }
% \end{macrocode}
% When centring the content about a decimal marker, the trick is
% to collect everything into two boxes and then compare the sizes.
% As we are always in math mode, we can use a math active token
% to make the switch. The up-front setting of the |decimal| box deals
% with the case where there is no decimal part.
% \begin{macrocode}
\cs_new_protected:Npn \@@_direct_marker:
{
\hbox_set:Nn \l_@@_tmp_box
{ \ensuremath { \mathord { \l_siunitx_number_output_decimal_tl } } }
\hbox_set_to_wd:Nnn \l_@@_decimal_box
{ \box_wd:N \l_@@_tmp_box }
{ \@@_fil: }
\hbox_set:Nw \l_@@_integer_box
\c_math_toggle_token
\tl_map_inline:Nn \l_siunitx_number_input_decimal_tl
{
\char_set_active_eq:NN ##1 \@@_direct_marker_switch:
\char_set_mathcode:nn { `##1 } { "8000 }
}
}
\cs_new_protected:Npn \@@_direct_marker_switch:
{
\c_math_toggle_token
\hbox_set_end:
\hbox_set:Nw \l_@@_decimal_box
\c_math_toggle_token
\l_siunitx_number_output_decimal_tl
}
\cs_new_protected:Npn \@@_direct_marker_end:
{
\c_math_toggle_token
\hbox_set_end:
\@@_center_marker:
\box_use_drop:N \l_@@_integer_box
\box_use_drop:N \l_@@_decimal_box
}
% \end{macrocode}
% For the version where there is space reserved, first format and decompose
% that, then create appropriately-sized boxes.
% \begin{macrocode}
\cs_new_protected:Npn \@@_direct_format:
{
\tl_set:Nx \l_@@_tmp_tl
{ \siunitx_number_format:NN \l_@@_model_tl \q_nil }
\exp_after:wN \@@_direct_format_aux:w
\l_@@_tmp_tl \q_stop
}
\cs_new_protected:Npn \@@_direct_format_aux:w
#1 \q_nil #2 \q_nil #3 \q_stop
{
\hbox_set:Nn \l_@@_tmp_box
{ \ensuremath { \@@_cleanup_decimal:w #3 } }
\hbox_set_to_wd:Nnn \l_@@_decimal_box
{ \box_wd:N \l_@@_tmp_box }
{ \@@_fil: }
\hbox_set:Nn \l_@@_tmp_box { \ensuremath { #1#2 } }
\hbox_set_to_wd:Nnw \l_@@_integer_box
{ \box_wd:N \l_@@_tmp_box }
\c_math_toggle_token
\tl_map_inline:Nn \l_siunitx_number_input_decimal_tl
{
\char_set_active_eq:NN ##1 \@@_direct_format_switch:
\char_set_mathcode:nn { `##1 } { "8000 }
}
\@@_fil:
}
\cs_new_protected:Npn \@@_direct_format_switch:
{
\c_math_toggle_token
\hbox_set_end:
\hbox_set_to_wd:Nnw \l_@@_decimal_box
{ \box_wd:N \l_@@_decimal_box }
\c_math_toggle_token
\mathord { \l_siunitx_number_output_decimal_tl }
}
\cs_new_protected:Npn \@@_direct_format_end:
{
\c_math_toggle_token
\@@_fil:
\hbox_set_end:
\use:c { @@_align_ \l_@@_align_number_tl :n }
{
\box_use_drop:N \l_@@_integer_box
\box_use_drop:N \l_@@_decimal_box
}
}
% \end{macrocode}
% No parsing and no alignment is easy.
% \begin{macrocode}
\cs_new_protected:Npn \@@_direct_none: { \c_math_toggle_token }
\cs_new_protected:Npn \@@_direct_none_end: { \c_math_toggle_token }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Printing numbers in cells: main functions}
%
% \begin{variable}{\l_@@_before_box, \l_@@_after_box}
% For alignment of text outside of a number.
% \begin{macrocode}
\box_new:N \l_@@_before_box
\box_new:N \l_@@_after_box
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_carry_dim}
% Used to \enquote{carry forward} the amount of white space which needs to
% be inserted after the decimal marker.
% \begin{macrocode}
\dim_new:N \l_@@_carry_dim
% \end{macrocode}
% \end{variable}
%
% \begin{variable}
% {
% \l_@@_align_comparator_bool ,
% \l_@@_align_exponent_bool ,
% \l_@@_align_after_bool ,
% \l_@@_align_before_bool ,
% \l_@@_align_uncertainty_bool
% }
% Alignment is handled using a |tl| as this allows a fast lookup at the
% point of use.
% \begin{macrocode}
\keys_define:nn { siunitx }
{
table-align-comparator .bool_set:N =
\l_@@_align_comparator_bool ,
table-align-exponent .bool_set:N =
\l_@@_align_exponent_bool ,
table-align-text-after .bool_set:N =
\l_@@_align_after_bool ,
table-align-text-before .bool_set:N =
\l_@@_align_before_bool ,
table-align-uncertainty .bool_set:N =
\l_@@_align_uncertainty_bool
}
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_print:nnn, \@@_print:VVV}
% \begin{macro}{\@@_print_marker:nnn}
% \begin{macro}{\@@_print_marker:w}
% \begin{macro}{\@@_print_format:nnn}
% \begin{macro}
% {
% \@@_print_marker_auxi:w,
% \@@_print_marker_auxii:w,
% \@@_print_marker_auxiii:w
% }
% \begin{macro}{\@@_print_format_after:N}
% \begin{macro}{\@@_print_format_box:Nn}
% \begin{macro}{\@@_print_none:nnn}
% \begin{macrocode}
\cs_new_protected:Npn \@@_print:nnn #1#2#3
{ \use:c { @@_print_ \l_@@_align_mode_tl :nnn } {#1} {#2} {#3} }
\cs_generate_variant:Nn \@@_print:nnn { VVV }
% \end{macrocode}
% When centering on the decimal marker, alignment is relatively simple, and
% close in concept to that used without parsing. First we need to deal with any
% text before or after the number. For text \emph{before}, there's the case
% where is has no width and might be a font or color change: that has to be
% filtered out first. Then we can adjust the size of this material and that
% after the number such that they are equal. The number itself can then be
% formatted, splitting at he decimal marker. A bit more size adjustment, then
% the number itself and any text at the end can be inserted.
% \begin{macrocode}
\cs_new_protected:Npn \@@_print_marker:nnn #1#2#3
{
\hbox_set:Nn \l_@@_before_box {#1}
\dim_compare:nNnT { \box_wd:N \l_@@_before_box } = { 0pt }
{
\box_clear:N \l_@@_before_box
#1
}
\hbox_set:Nn \l_@@_after_box {#3}
\dim_compare:nNnTF
{ \box_wd:N \l_@@_after_box }
> { \box_wd:N \l_@@_before_box }
{
\hbox_set_to_wd:Nnn \l_@@_before_box
{ \box_wd:N \l_@@_after_box }
{
\@@_fil:
\hbox_unpack:N \l_@@_before_box
}
}
{
\hbox_set_to_wd:Nnn \l_@@_after_box
{ \box_wd:N \l_@@_before_box }
{
\hbox_unpack:N \l_@@_after_box
\@@_fil:
}
}
\box_use_drop:N \l_@@_before_box
\siunitx_number_parse:nN {#2} \l_@@_tmp_tl
\siunitx_number_process:NN \l_@@_tmp_tl \l_@@_tmp_tl
\tl_set:Nx \l_@@_tmp_tl
{ \siunitx_number_format:NN \l_@@_tmp_tl \q_nil }
\exp_after:wN \@@_print_marker:w
\l_@@_tmp_tl \q_stop
\box_use_drop:N \l_@@_after_box
}
\cs_new_protected:Npn \@@_print_marker:w
#1 \q_nil #2 \q_nil #3 \q_stop
{
\hbox_set:Nn \l_@@_integer_box
{ \siunitx_print:nn { number } { #1#2 } }
\hbox_set:Nn \l_@@_decimal_box
{ \siunitx_print:nn { number } { \@@_cleanup_decimal:w #3 } }
\@@_center_marker:
\box_use_drop:N \l_@@_integer_box
\box_use_drop:N \l_@@_decimal_box
}