-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsiunitx-table.dtx
1768 lines (1768 loc) · 55.5 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,2021-2024 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}
% Additional commands needed in this source
\ProvideDocumentCommand\email{m}{\href{mailto:#1}{\nolinkurl{#1}}}
\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:
% \email{[email protected]}^^A
% }^^A
% }
%
% \date{Released \filedate}
%
% \maketitle
%
% \begin{documentation}
%
% \section{Numbers in tables}
%
% This submodule is concerned with formatting numbers in table cells or
% similar fixed-width contexts. The main function, \cs{siunitx_cell_begin:w},
% is designed to work with the normal \LaTeXe{} tablular cell construct
% featuring \cs{ignorespaces}. Therefore, if used outside of a \LaTeXe{}
% tabular, it is necessary to provide this token.
%
% \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}
% Switch which determines whether alignment of comparators is attempted
% within table cells. The standard setting is |true|.
% \end{function}
%
% \begin{function}{table-align-exponent}
% \begin{syntax}
% |table-align-exponent| = |true|\verb"|"|false|
% \end{syntax}
% Switch which determines whether alignment of exponents is attempted
% within table cells. The standard setting is |true|.
% \end{function}
%
% \begin{function}{table-align-text-after}
% \begin{syntax}
% |table-align-text-after| = |true|\verb"|"|false|
% \end{syntax}
% Switch which determines whether alignment of text falling after a number
% is attempted within table cells. The standard setting is |true|.
% \end{function}
%
% \begin{function}{table-align-text-before}
% \begin{syntax}
% |table-align-text-before| = |true|\verb"|"|false|
% \end{syntax}
% Switch which determines whether alignment of text falling before a number
% is attempted within table cells. The standard setting is |true|.
% \end{function}
%
% \begin{function}{table-align-uncertainty}
% \begin{syntax}
% |table-align-uncertainty| = |true|\verb"|"|false|
% \end{syntax}
% Switch which determines whether alignment of separated uncertainty values
% is attempted within table cells. The standard setting is |true|.
% \end{function}
%
% \begin{function}{table-alignment}
% \begin{syntax}
% |table-alignment| = |center|\verb"|"|left|\verb"|"|right|
% \end{syntax}
% Selects the alignment of all tabular content with the margins of the table
% cell (or other boundary). See also |table-number-alignment| and
% |table-text-alignment|. The standard setting is |center|.
% \end{function}
%
% \begin{function}{table-alignment-mode}
% \begin{syntax}
% |table-alignment-mode| = |format|\verb"|"|marker|\verb"|"|none|
% \end{syntax}
% Selects the method used to align numbers with the desired position in the
% cell (set by |table-alignment|). When set to |format|, a dedicated
% amount of space is calculated from the |table-format|. When |marker|
% is selected, alignment is carried out symmetrically around the decimal
% marker. Finally, |none| switches off all alignment: numbers are parsed
% and formatted but with no attempt at placement within the cell. The
% standard setting is |marker|.
% \end{function}
%
% \begin{function}{table-auto-round}
% \begin{syntax}
% |table-auto-round| = |true|\verb"|"|false|
% \end{syntax}
% Switch which determines whether numbers are rounded to fit within
% the |table-format| specification (if possible). The standard
% setting is |false|.
% \end{function}
%
% \begin{function}{table-column-width}
% \begin{syntax}
% |table-column-width| = \meta{width}
% \end{syntax}
% Sets the width of the table column used for numbers. This is only used
% when |table-fixed-width| is |true|.
% \end{function}
%
% \begin{function}{table-fixed-width}
% \begin{syntax}
% |table-fixed-width| = |true|\verb"|"|false|
% \end{syntax}
% Switch which determines whether a fixed-width column is used for numbers
% in tables. When |true|, the width is taken from |table-column-width|. The
% standard setting is |false|.
% \end{function}
%
% \begin{function}{table-format}
% \begin{syntax}
% |table-format| = \meta{format}
% \end{syntax}
% Describes the amount of space that should be reserved when
% |table-alignment-mode| is set to |format|. The \meta{format} takes the
% same general form as input for a table cell, with the numerical parts
% describing how many digits to reserve space for. For example, |1.2e3|
% would allow space for one digit in the integer part, two in the decimal
% part and three in the exponent part. Signs can be allowed for using any
% valid input sign, so for example |+1.2 \pm 1.2| would allow for a sign,
% a number with one integer and two decimal digits and an uncertainty of
% the same size.
% \end{function}
%
% \begin{function}{table-model-setup}
% \begin{syntax}
% |table-model-setup| = \meta{commands}
% \end{syntax}
% Additional commands to be inserted when using the |table-format| to
% create a model for alignment of cells. Typically this will be used to
% handle variable-width fonts in columns. The standard setting is empty.
% \end{function}
%
% \begin{function}{table-number-alignment}
% \begin{syntax}
% |table-number-alignment| = |center|\verb"|"|left|\verb"|"|right|
% \end{syntax}
% Selects the alignment of numerical content with the margins of the table
% cell (or other boundary). See also |table-alignment| and
% |table-text-alignment|. The standard setting is |center|.
% \end{function}
%
% \begin{function}{table-text-alignment}
% \begin{syntax}
% |table-text-alignment| = |center|\verb"|"|left|\verb"|"|none|\verb"|"|right|
% \end{syntax}
% Selects the alignment of non-numerical content with the margins of the
% table cell (or other boundary). See also |table-alignment| and
% |table-number-alignment|. Notice the additional support for |none| here.
% The standard setting is |center|.
% \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.
% \begin{macrocode}
%<@@=siunitx_table>
% \end{macrocode}
%
% Required core variants.
% \begin{macrocode}
\cs_generate_variant:Nn \keys_set:nn { nx }
% \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. Whilst each
% cell forms a group, as we require definitions to say local
% to the collections code, an additional group is required.
% 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:
{
\group_begin:
\tl_clear:N \l_@@_collect_tl
\@@_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, \@@_collect_token_aux:N}
% \begin{macro}{\@@_collect_relax: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. This has to
% be an explicit token as we can get the same meaning from |\protect|.
% \begin{macrocode}
\cs_new_protected:Npn \@@_collect_loop:
{
\peek_remove_spaces:n
{
\peek_catcode: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: }
\textonly@unskip { \@@_collect_loop: }
\end { \@@_collect_pre_cr: \tabularnewline \end }
\relax { \@@_collect_relax:N #1 }
\tabularnewline { \@@_collect_pre_cr: \tabularnewline }
\siunitx_cell_end: { \siunitx_cell_end: }
}
{ \@@_collect_token_aux:N #1 }
}
\cs_new_protected:Npn \@@_collect_token_aux:N #1
{
\tl_put_right:Nn \l_@@_collect_tl {#1}
\@@_collect_loop:
}
\cs_new_protected:Npn \@@_collect_relax:N #1
{
\str_if_eq:nnTF {#1} { \relax }
{ \relax }
{ \@@_collect_token_aux:N #1 }
}
\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 { \@@_collect_pre_cr: \tabularnewline \end }
\relax { \@@_collect_relax:N #1 }
\tabularnewline { \@@_collect_pre_cr: \tabularnewline }
\siunitx_cell_end: { \siunitx_cell_end: }
}
{ \@@_collect_token_aux:N #1 }
}
}
{ }
}
\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}
% \end{macro}
%
% \begin{macro}{\@@_collect_pre_cr:}
% The definition of \cs{cr} is used to allow collection of any tokens
% from the \tn{halign} template after |#| when we are in the last cell of
% the row. (The approach is based on that in \pkg{collcell}.) Note that
% \TeX{} inserts these tokens when it sees the \tn{cr} primitive, so
% there is no expansion to consider. Whilst in most cases the group formed
% by each cell will tidy up, nested tabulars (for example in a header row)
% will break if \tn{cr} is redefined too widely. Thus we use a targetted
% approach: only apply when needed, and use the additional group inside the
% cell to keep control.
% \begin{macrocode}
\cs_new_protected:Npn \@@_collect_pre_cr:
{
\if_false: { \fi:
\cs_set_protected:Npn \cr
{
\@@_collect_loop:
\tex_cr:D
}
\if_false: } \fi:
}
% \end{macrocode}
% \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:}
% \begin{macro}{\@@_collect_end:n}
% \begin{macro}[EXP]{\@@_collect_end_aux:n}
% \begin{macro}[EXP]{\@@_collect_end:w}
% At the end of the cell, escape the group and check for expansion. We
% only do that if the entire content is not a brace group: there is more
% likely to be problematic content in the case of a header.
% \begin{macrocode}
\cs_new_protected:Npn \@@_collect_end:
{
\exp_args:NNV \group_end:
\@@_collect_end:n \l_@@_collect_tl
\@@_split:VNNN
\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}
% To cover the use of REV\TeX{}, we need to allow for the insertion of
% \cs{array@row@rst} into cell content: that explodes inside
% \cs{protected@edef}. We use the classical solution of making locally
% equal to \cs{scan_stop:}.
% \begin{macrocode}
\cs_new_protected:Npn \@@_collect_end:n #1
{
\str_if_eq:eeTF { \exp_not:n {#1} }
{ { \@@_collect_end_aux:n {#1} } }
{ \tl_set:Nn }
{
\cs_if_exist:NT \array@row@rst
{ \cs_set_eq:NN \array@row@rst \scan_stop: }
\protected@edef
}
\l_@@_collect_tl {#1}
}
\cs_new:Npn \@@_collect_end_aux:n #1
{ \exp_after:wN \@@_collect_end:w #1 \q_stop }
\cs_new:Npn \@@_collect_end:w #1 \q_stop
{ \exp_not:n {#1} }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_split:nNNN, \@@_split:VNNN}
% \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_generate_variant:Nn \@@_split:nNNN { V }
\cs_new_protected:Npn \@@_split_loop:NNN #1#2#3
{
\peek_remove_spaces:n
{
\peek_catcode: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}
\dim_new:N \l_@@_column_width_dim
\keys_define:nn { siunitx }
{
table-column-width .code:n =
{
\dim_set:Nn \l_@@_column_width_dim {#1}
\dim_compare:nNnT \l_@@_column_width_dim > \c_zero_dim
{ \bool_set_true:N \l_@@_fixed_width_bool }
} ,
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, \@@_align_none:n}
% \begin{macro}{\@@_align_auxi:nn, \@@_align_auxii:nn, \@@_align_auxiii: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. There is a
% further wrinkle, in that if \pkg{tabularray} is being used, we need to skip
% the \pkg{colortbl} fix (see
% \url{https://github.com/lvjr/tabularray/issues/512}).
% \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_none:n #1
{
\bool_if:NTF \l_@@_fixed_width_bool
{ \hbox_to_wd:nn \l_@@_column_width_dim }
{ \use:n }
{#1}
}
\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_gset_protected:Npn \@@_align_auxi:nn #1#2
{
\@@_skip:n{ 0pt~plus~-0.5fill }
\@@_align_auxii:nn {#1} {#2}
\@@_skip:n { 0pt~plus~-0.5fill }
}
\@ifpackageloaded { tabularray }
{
\cs_new_eq:NN
\@@_align_auxiii:nn
\@@_align_auxi:nn
\cs_gset_protected:Npn \@@_align_auxi:nn #1#2
{
\int_compare:nNnTF \g_tblr_level_int = 0
{ \@@_align_auxii:nn }
{ \@@_align_auxiii:nn }
{#1} {#2}
}
}
{ }
}
{ }
}
% \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 , none }
{ \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{variable}{\l_@@_uncert_box}
% An extra one for aligning separated uncertainty values.
% \begin{macrocode}
\box_new:N \l_@@_uncert_box
% \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_fil:, \@@_fill:}
% Primitives renamed.
% \begin{macrocode}
\cs_new_eq:NN \@@_fil: \tex_hfil:D
\cs_new_eq:NN \@@_fill: \tex_hfill: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}{\@@_color_check:N}
% \begin{macro}{\@@_color_check:w}
% \begin{macro}{\@@_color_check:Nnw}
% Handle the fact that splitting a number can leave a negative color
% dangling.
% \begin{macrocode}
\cs_new_protected:Npn \@@_color_check:N #1
{ \exp_after:wN \@@_color_check:w #1 \q_stop }
\cs_new_protected:Npn \@@_color_check:w #1 \q_nil #2 \q_stop
{
\tl_if_head_eq_meaning:nNT {#1} \color
{ \@@_color_check:Nnw #1 \q_stop }
}
\cs_new_protected:Npn \@@_color_check:Nnw #1#2#3 \q_stop
{ \keys_set:nn { siunitx } { number-color = #2 } }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \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. Notice that we need to allow for the width
% of the decimal marker itself. When not aligning non-numerical material, we
% put the extra space into the boxes around the number.
% \begin{macrocode}
\cs_new_protected:Npn \@@_center_marker:
{
\hbox_set:Nn \l_@@_tmp_box
{ \ensuremath { \mathord { \l_siunitx_number_output_decimal_tl } } }
\dim_compare:nNnTF
{ \box_wd:N \l_@@_integer_box }
>
{
\box_wd:N \l_@@_decimal_box
- \box_wd:N \l_@@_tmp_box
}
{
\bool_if:NTF \l_@@_align_after_bool
{
\@@_center_marker_aux:Nnnn \l_@@_decimal_box
{
\box_wd:N \l_@@_integer_box
+ \box_wd:N \l_@@_tmp_box
}
}
{
\@@_center_marker_aux:Nnnn \l_@@_after_box
{
\box_wd:N \l_@@_after_box
+ \box_wd:N \l_@@_integer_box
- \box_wd:N \l_@@_decimal_box
+ \box_wd:N \l_@@_tmp_box
}
}
{ } { \@@_fil: }
}
{
\bool_if:NTF \l_@@_align_before_bool
{
\@@_center_marker_aux:Nnnn \l_@@_integer_box
{
\box_wd:N \l_@@_decimal_box
- \box_wd:N \l_@@_tmp_box
}
}
{
\@@_center_marker_aux:Nnnn \l_@@_before_box
{
\box_wd:N \l_@@_before_box
+ \box_wd:N \l_@@_decimal_box
- \box_wd:N \l_@@_integer_box
- \box_wd:N \l_@@_tmp_box
}
}
{ \@@_fil: } { }
}
}
\cs_new_protected:Npn \@@_center_marker_aux:Nnnn #1#2#3#4
{
\hbox_set_to_wd:Nnn #1 {#2}
{
#3
\hbox_unpack:N #1
#4
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{variable}
% {
% \l_@@_auto_round_bool,
% \l_@@_model_setup_tl ,
% \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 =
{
\group_begin:
\protected@edef \l_@@_tmp_tl {#1}
\exp_args:NNV \group_end:
\@@_split:nNNN \l_@@_tmp_tl
\l_@@_before_model_tl
\l_@@_model_tl
\l_@@_after_model_tl
\@@_generate_model:V \l_@@_model_tl
\tl_set:Nn \l_@@_align_mode_tl { format }
} ,
table-model-setup .tl_set:N =
\l_@@_model_setup_tl ,
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, \@@_generate_model:V}
% \begin{macro}{\@@_generate_model:nnnnnnn}
% \begin{macro}[EXP]{\@@_generate_model_S:nnw}
% \begin{macro}[EXP]{\@@_generate_model_S:nnn, \@@_generate_model_S:een}
% 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. Notice that
% the uncertainty part needs to allow for the case where we cross the
% decimal.
% \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 } { retain-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_generate_variant:Nn \@@_generate_model:n { V }
\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} }
{
\int_compare:nNnTF {#3} = 0
{ 0 }
{ \prg_replicate:nn {#3} { 8 } }
}
{ \prg_replicate:nn { 0 #4 } { 8 } }
{
\tl_if_blank:nF {#5}
{
\use:c { @@_generate_model_ \tl_head:n {#5} :nnw }
{#4} #5
}
}
\exp_not:n { {#6} }
{
\int_compare:nNnTF { 0#7 } = 0
{ 0 }
{ \prg_replicate:nn { 0#7 } { 8 } }
}
}
}
\cs_new:Npn \@@_generate_model_S:nnw #1#2#3
{
{ S }
{
\@@_generate_model_S:een
{ \tl_count:n {#1} } { \tl_count:n {#3} }
{#3}
}
}
\cs_new:Npn \@@_generate_model_S:nnn #1#2#3
{
\prg_replicate:nn
{
\int_compare:nNnTF {#2} > {#1}
{
\str_range:nnn {#3} { 1 } {#1}
+
\str_range:nnn {#3} { 1 + #1 } {#2}
}
{#3}
}
{ 8 }
}
\cs_generate_variant:Nn \@@_generate_model_S:nnn { ee }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Directly printing without collection}
%