Debian Codegraph @

texmf-dist/source/latex/siunitx/siunitx-table.dtx

raw
1 % \iffalse meta-comment
2 %
3 % File: siunitx-table.dtx Copyright (C) 2016-2019,2021-2026 Joseph Wright
4 %
5 % It may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this
7 % license or (at your option) any later version. The latest version
8 % of this license is in the file
9 %
10 % https://www.latex-project.org/lppl.txt
11 %
12 % This file is part of the "siunitx bundle" (The Work in LPPL)
13 % and all files in that bundle must be distributed together.
14 %
15 % The released version of this bundle is available from CTAN.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/josephwright/siunitx
22 %
23 % for those people who are interested.
24 %
25 % -----------------------------------------------------------------------
26 %
27 %<*driver>
28 \documentclass{l3doc}
29 % Additional commands needed in this source
30 \ProvideDocumentCommand\email{m}{\href{mailto:#1}{\nolinkurl{#1}}}
31 \ProvideDocumentCommand\foreign{m}{\textit{#1}}
32 % The next line is needed so that \GetFileInfo will be able to pick up
33 % version data
34 \usepackage{siunitx}
35 \begin{document}
36 \DocInput{\jobname.dtx}
37 \end{document}
38 %</driver>
39 % \fi
40 %
41 % \GetFileInfo{siunitx.sty}
42 %
43 % \title{^^A
44 % \pkg{siunitx-table} -- Formatting numbers in tables^^A
45 % \thanks{This file describes \fileversion,
46 % last revised \filedate.}^^A
47 % }
48 %
49 % \author{^^A
50 % Joseph Wright^^A
51 % \thanks{^^A
52 % E-mail:
53 % \email{[email protected]}^^A
54 % }^^A
55 % }
56 %
57 % \date{Released \filedate}
58 %
59 % \maketitle
60 %
61 % \begin{documentation}
62 %
63 % \section{Numbers in tables}
64 %
65 % This submodule is concerned with formatting numbers in table cells or
66 % similar fixed-width contexts. The main function, \cs{siunitx_cell_begin:w},
67 % is designed to work with the normal \LaTeXe{} tabular cell construct
68 % featuring \cs{ignorespaces}. Therefore, if used outside of a \LaTeXe{}
69 % tabular, it is necessary to provide this token.
70 %
71 % \begin{function}{\siunitx_cell_begin:w, \siunitx_cell_end:}
72 % \begin{syntax}
73 % \cs{siunitx_cell_begin:w} \meta{preamble} \cs{ignorespaces}
74 % \meta{content}
75 % \cs{siunitx_cell_end:}
76 % \end{syntax}
77 % Collects the \meta{preamble} and \meta{content} tokens, and determines
78 % if it is text or a number (as parsed by \cs{siunitx_number_parse:nN}).
79 % It produces output of a fixed width suitable for alignment in a table,
80 % although it is not \emph{required} that the code is used within a cell.
81 % Note that |\ignorespaces| must occur in the \enquote{cell}: it marks
82 % the end of the \TeX{} |\halign| template.
83 % \end{function}
84 %
85 % \begin{function}{table-align-comparator}
86 % \begin{syntax}
87 % |table-align-comparator| = |true|\verb"|"|false|
88 % \end{syntax}
89 % Switch which determines whether alignment of comparators is attempted
90 % within table cells. The standard setting is |true|.
91 % \end{function}
92 %
93 % \begin{function}{table-align-exponent}
94 % \begin{syntax}
95 % |table-align-exponent| = |true|\verb"|"|false|
96 % \end{syntax}
97 % Switch which determines whether alignment of exponents is attempted
98 % within table cells. The standard setting is |true|.
99 % \end{function}
100 %
101 % \begin{function}{table-align-text-after}
102 % \begin{syntax}
103 % |table-align-text-after| = |true|\verb"|"|false|
104 % \end{syntax}
105 % Switch which determines whether alignment of text falling after a number
106 % is attempted within table cells. The standard setting is |true|.
107 % \end{function}
108 %
109 % \begin{function}{table-align-text-before}
110 % \begin{syntax}
111 % |table-align-text-before| = |true|\verb"|"|false|
112 % \end{syntax}
113 % Switch which determines whether alignment of text falling before a number
114 % is attempted within table cells. The standard setting is |true|.
115 % \end{function}
116 %
117 % \begin{function}{table-align-uncertainty}
118 % \begin{syntax}
119 % |table-align-uncertainty| = |true|\verb"|"|false|
120 % \end{syntax}
121 % Switch which determines whether alignment of separated uncertainty values
122 % is attempted within table cells. The standard setting is |true|.
123 % \end{function}
124 %
125 % \begin{function}{table-alignment}
126 % \begin{syntax}
127 % |table-alignment| = |center|\verb"|"|left|\verb"|"|right|
128 % \end{syntax}
129 % Selects the alignment of all tabular content with the margins of the table
130 % cell (or other boundary). See also |table-number-alignment| and
131 % |table-text-alignment|. The standard setting is |center|.
132 % \end{function}
133 %
134 % \begin{function}{table-alignment-mode}
135 % \begin{syntax}
136 % |table-alignment-mode| = |format|\verb"|"|marker|\verb"|"|none|
137 % \end{syntax}
138 % Selects the method used to align numbers with the desired position in the
139 % cell (set by |table-alignment|). When set to |format|, a dedicated
140 % amount of space is calculated from the |table-format|. When |marker|
141 % is selected, alignment is carried out symmetrically around the decimal
142 % marker. Finally, |none| switches off all alignment: numbers are parsed
143 % and formatted but with no attempt at placement within the cell. The
144 % standard setting is |marker|.
145 % \end{function}
146 %
147 % \begin{function}{table-auto-round}
148 % \begin{syntax}
149 % |table-auto-round| = |true|\verb"|"|false|
150 % \end{syntax}
151 % Switch which determines whether numbers are rounded to fit within
152 % the |table-format| specification (if possible). The standard
153 % setting is |false|.
154 % \end{function}
155 %
156 % \begin{function}{table-column-width}
157 % \begin{syntax}
158 % |table-column-width| = \meta{width}
159 % \end{syntax}
160 % Sets the width of the table column used for numbers. This is only used
161 % when |table-fixed-width| is |true|.
162 % \end{function}
163 %
164 % \begin{function}{table-fixed-width}
165 % \begin{syntax}
166 % |table-fixed-width| = |true|\verb"|"|false|
167 % \end{syntax}
168 % Switch which determines whether a fixed-width column is used for numbers
169 % in tables. When |true|, the width is taken from |table-column-width|. The
170 % standard setting is |false|.
171 % \end{function}
172 %
173 % \begin{function}{table-format}
174 % \begin{syntax}
175 % |table-format| = \meta{format}
176 % \end{syntax}
177 % Describes the amount of space that should be reserved when
178 % |table-alignment-mode| is set to |format|. The \meta{format} takes the
179 % same general form as input for a table cell, with the numerical parts
180 % describing how many digits to reserve space for. For example, |1.2e3|
181 % would allow space for one digit in the integer part, two in the decimal
182 % part and three in the exponent part. Signs can be allowed for using any
183 % valid input sign, so for example |+1.2 \pm 1.2| would allow for a sign,
184 % a number with one integer and two decimal digits and an uncertainty of
185 % the same size.
186 % \end{function}
187 %
188 % \begin{function}{table-model-setup}
189 % \begin{syntax}
190 % |table-model-setup| = \meta{commands}
191 % \end{syntax}
192 % Additional commands to be inserted when using the |table-format| to
193 % create a model for alignment of cells. Typically this will be used to
194 % handle variable-width fonts in columns. The standard setting is empty.
195 % \end{function}
196 %
197 % \begin{function}{table-number-alignment}
198 % \begin{syntax}
199 % |table-number-alignment| = |center|\verb"|"|left|\verb"|"|right|
200 % \end{syntax}
201 % Selects the alignment of numerical content with the margins of the table
202 % cell (or other boundary). See also |table-alignment| and
203 % |table-text-alignment|. The standard setting is |center|.
204 % \end{function}
205 %
206 % \begin{function}{table-text-alignment}
207 % \begin{syntax}
208 % |table-text-alignment| = |center|\verb"|"|left|\verb"|"|none|\verb"|"|right|
209 % \end{syntax}
210 % Selects the alignment of non-numerical content with the margins of the
211 % table cell (or other boundary). See also |table-alignment| and
212 % |table-number-alignment|. Notice the additional support for |none| here.
213 % The standard setting is |center|.
214 % \end{function}
215 %
216 % \end{documentation}
217 %
218 % \begin{implementation}
219 %
220 % \section{\pkg{siunitx-table} implementation}
221 %
222 % Start the \pkg{DocStrip} guards.
223 % \begin{macrocode}
224 %<*package>
225 % \end{macrocode}
226 %
227 % Identify the internal prefix.
228 % \begin{macrocode}
229 %<@@=siunitx_table>
230 % \end{macrocode}
231 %
232 % \begin{variable}{\l_@@_tmp_box, \l_@@_tmp_dim, \l_@@_tmp_tl}
233 % Scratch space.
234 % \begin{macrocode}
235 \box_new:N \l_@@_tmp_box
236 \dim_new:N \l_@@_tmp_dim
237 \tl_new:N \l_@@_tmp_tl
238 % \end{macrocode}
239 % \end{variable}
240 %
241 % \subsection{Interface functions}
242 %
243 % \begin{variable}{\l_@@_text_bool}
244 % Used to track that a cell is purely text.
245 % \begin{macrocode}
246 \bool_new:N \l_@@_text_bool
247 % \end{macrocode}
248 % \end{variable}
249 %
250 % \begin{macro}{\siunitx_cell_begin:w}
251 % \begin{macro}{\siunitx_cell_end:}
252 % The start and end of the cell need to deal with the possibility of
253 % a cell containing only text.
254 % \begin{macrocode}
255 \cs_new_protected:Npn \siunitx_cell_begin:w
256 {
257 \bool_set_false:N \l_@@_text_bool
258 \bool_if:NTF \l_siunitx_number_parse_bool
259 { \@@_collect_begin: }
260 { \@@_direct_begin: }
261 }
262 \cs_new_protected:Npn \siunitx_cell_end:
263 {
264 \bool_if:NF \l_@@_text_bool
265 {
266 \bool_if:NTF \l_siunitx_number_parse_bool
267 { \@@_collect_end: }
268 { \@@_direct_end: }
269 }
270 }
271 % \end{macrocode}
272 % \end{macro}
273 % \end{macro}
274 %
275 % \subsection{Collecting tokens}
276 %
277 % \begin{variable}{\l_@@_collect_tl}
278 % Space for tokens.
279 % \begin{macrocode}
280 \tl_new:N \l_@@_collect_tl
281 % \end{macrocode}
282 % \end{variable}
283 %
284 % \begin{macro}{\@@_collect_begin:}
285 % \begin{macro}{\@@_collect_begin:w}
286 % Collecting a tabular cell means doing a token-by-token collection.
287 % In previous versions of \pkg{siunitx} that was done along with picking
288 % out the numerical part, but the code flow ends up very tricky. Here,
289 % therefore, we just collect up the unchanged tokens first. Whilst each
290 % cell forms a group, as we require definitions to say local
291 % to the collections code, an additional group is required.
292 % We use an auxiliary to
293 % fish out the |\ignorespaces| from the template: that has to go to avoid
294 % issues with the peek-ahead code (everything before the |#| needs to be
295 % read \emph{before} the Appendix~D trick gets applied). Some packages
296 % add additional tokens before the |\ignorespaces|, which are dealt with by
297 % the delimited argument.
298 % \begin{macrocode}
299 \cs_new_protected:Npn \@@_collect_begin:
300 {
301 \group_begin:
302 \tl_clear:N \l_@@_collect_tl
303 \@@_collect_begin:w
304 }
305 \cs_new_protected:Npn \@@_collect_begin:w #1 \ignorespaces
306 { \@@_collect_loop: #1 }
307 % \end{macrocode}
308 % \end{macro}
309 % \end{macro}
310 %
311 % \begin{macro}{\@@_collect_loop:}
312 % \begin{macro}{\@@_collect_group:n}
313 % \begin{macro}{\@@_collect_token:N, \@@_collect_token_aux:N}
314 % \begin{macro}{\@@_collect_relax:N}
315 % \begin{macro}{\@@_collect_search:NnF}
316 % \begin{macro}{\@@_collect_search_aux:NNn}
317 % Collecting up the cell content needs a loop: this is done using
318 % a |peek| approach as it's most natural. (A slower approach is possible
319 % using something like the |\text_lowercase:n| loop code.) The set of
320 % possible tokens is somewhat limited compared to an arbitrary cell
321 % (\foreign{cf.}~the approach in \pkg{collcell}): the special cases are
322 % pulled out for manual handling. The flexible lookup approach is more-or-less
323 % the same idea as in the kernel |case| functions. The |\relax| special case
324 % covers the case where |\\| has been expanded in an empty cell. This has to
325 % be an explicit token as we can get the same meaning from |\protect|.
326 % \begin{macrocode}
327 \cs_new_protected:Npn \@@_collect_loop:
328 {
329 \peek_remove_spaces:n
330 {
331 \peek_catcode:NTF \c_group_begin_token
332 { \@@_collect_group:n }
333 { \@@_collect_token:N }
334 }
335 }
336 \cs_new_protected:Npn \@@_collect_group:n #1
337 {
338 \tl_put_right:Nn \l_@@_collect_tl { {#1} }
339 \@@_collect_loop:
340 }
341 \cs_new_protected:Npn \@@_collect_token:N #1
342 {
343 \@@_collect_search:NnF #1
344 {
345 \unskip { \@@_collect_loop: }
346 \textonly@unskip { \@@_collect_loop: }
347 \end { \@@_collect_pre_cr: \tabularnewline \end }
348 \relax { \@@_collect_relax:N #1 }
349 \tabularnewline { \@@_collect_pre_cr: \tabularnewline }
350 \siunitx_cell_end: { \siunitx_cell_end: }
351 }
352 { \@@_collect_token_aux:N #1 }
353 }
354 \cs_new_protected:Npn \@@_collect_token_aux:N #1
355 {
356 \tl_put_right:Nn \l_@@_collect_tl {#1}
357 \@@_collect_loop:
358 }
359 \cs_new_protected:Npn \@@_collect_relax:N #1
360 {
361 \str_if_eq:nnTF {#1} { \relax }
362 { \relax }
363 { \@@_collect_token_aux:N #1 }
364 }
365 \AtBeginDocument
366 {
367 \@ifpackageloaded { mdwtab }
368 {
369 \cs_set_protected:Npn \@@_collect_token:N #1
370 {
371 \@@_collect_search:NnF #1
372 {
373 \@maybe@unskip { \@@_collect_loop: }
374 \tab@setcr { \@@_collect_loop: }
375 \unskip { \@@_collect_loop: }
376 \end { \@@_collect_pre_cr: \tabularnewline \end }
377 \relax { \@@_collect_relax:N #1 }
378 \tabularnewline { \@@_collect_pre_cr: \tabularnewline }
379 \siunitx_cell_end: { \siunitx_cell_end: }
380 }
381 { \@@_collect_token_aux:N #1 }
382 }
383 }
384 { }
385 }
386 \cs_new_protected:Npn \@@_collect_search:NnF #1#2#3
387 {
388 \@@_collect_search_aux:NNn #1
389 #2
390 #1 {#3}
391 \q_stop
392 }
393 \cs_new_protected:Npn \@@_collect_search_aux:NNn #1#2#3
394 {
395 \token_if_eq_meaning:NNTF #1 #2
396 { \use_i_delimit_by_q_stop:nw {#3} }
397 { \@@_collect_search_aux:NNn #1 }
398 }
399 % \end{macrocode}
400 % \end{macro}
401 % \end{macro}
402 % \end{macro}
403 % \end{macro}
404 % \end{macro}
405 % \end{macro}
406 %
407 % \begin{macro}{\@@_collect_pre_cr:}
408 % The definition of \cs{cr} is used to allow collection of any tokens
409 % from the \tn{halign} template after |#| when we are in the last cell of
410 % the row. (The approach is based on that in \pkg{collcell}.) Note that
411 % \TeX{} inserts these tokens when it sees the \tn{cr} primitive, so
412 % there is no expansion to consider. Whilst in most cases the group formed
413 % by each cell will tidy up, nested tabulars (for example in a header row)
414 % will break if \tn{cr} is redefined too widely. Thus we use a targeted
415 % approach: only apply when needed, and use the additional group inside the
416 % cell to keep control.
417 % \begin{macrocode}
418 \cs_new_protected:Npn \@@_collect_pre_cr:
419 {
420 \if_false: { \fi:
421 \cs_set_protected:Npn \cr
422 {
423 \@@_collect_loop:
424 \tex_cr:D
425 }
426 \if_false: } \fi:
427 }
428 % \end{macrocode}
429 % \end{macro}
430 %
431 % \subsection{Separating collected material}
432 %
433 % The input needs to be divided into numerical tokens and those which appear
434 % before and after them. This needs a second loop and validation.
435 %
436 % \begin{variable}{\l_@@_before_tl, \l_@@_number_tl, \l_@@_after_tl}
437 % Space for tokens.
438 % \begin{macrocode}
439 \tl_new:N \l_@@_before_tl
440 \tl_new:N \l_@@_number_tl
441 \tl_new:N \l_@@_after_tl
442 % \end{macrocode}
443 % \end{variable}
444 %
445 % \begin{macro}{\@@_collect_end:}
446 % \begin{macro}{\@@_collect_end:n}
447 % \begin{macro}[EXP]{\@@_collect_end_aux:n}
448 % \begin{macro}[EXP]{\@@_collect_end:w}
449 % At the end of the cell, escape the group and check for expansion. We
450 % only do that if the entire content is not a brace group: there is more
451 % likely to be problematic content in the case of a header.
452 % \begin{macrocode}
453 \cs_new_protected:Npn \@@_collect_end:
454 {
455 \exp_args:NNV \group_end:
456 \@@_collect_end:n \l_@@_collect_tl
457 \@@_split:VNNN
458 \l_@@_collect_tl
459 \l_@@_before_tl
460 \l_@@_number_tl
461 \l_@@_after_tl
462 \tl_if_empty:NTF \l_@@_number_tl
463 { \@@_print_text:V \l_@@_before_tl }
464 {
465 \@@_print:VVV
466 \l_@@_before_tl
467 \l_@@_number_tl
468 \l_@@_after_tl
469 }
470 }
471 % \end{macrocode}
472 % To cover the use of REV\TeX{}, we need to allow for the insertion of
473 % \cs{array@row@rst} into cell content: that explodes inside
474 % \cs{protected@edef}. We use the classical solution of making locally
475 % equal to \cs{scan_stop:}.
476 % \begin{macrocode}
477 \cs_new_protected:Npn \@@_collect_end:n #1
478 {
479 \str_if_eq:eeTF { \exp_not:n {#1} }
480 { { \@@_collect_end_aux:n {#1} } }
481 { \tl_set:Nn }
482 {
483 \cs_if_exist:NT \array@row@rst
484 { \cs_set_eq:NN \array@row@rst \scan_stop: }
485 \protected@edef
486 }
487 \l_@@_collect_tl {#1}
488 }
489 \cs_new:Npn \@@_collect_end_aux:n #1
490 { \exp_after:wN \@@_collect_end:w #1 \q_stop }
491 \cs_new:Npn \@@_collect_end:w #1 \q_stop
492 { \exp_not:n {#1} }
493 % \end{macrocode}
494 % \end{macro}
495 % \end{macro}
496 % \end{macro}
497 % \end{macro}
498 %
499 % \begin{macro}{\@@_split:nNNN, \@@_split:VNNN}
500 % \begin{macro}{\@@_split_loop:NNN}
501 % \begin{macro}{\@@_split_group:NNNn}
502 % \begin{macro}{\@@_split_token:NNNN}
503 % Splitting into parts uses the fact that numbers cannot contain groups
504 % and that we can track where we are up to based on the content of the
505 % token lists.
506 % \begin{macrocode}
507 \cs_new_protected:Npn \@@_split:nNNN #1#2#3#4
508 {
509 \tl_clear:N #2
510 \tl_clear:N #3
511 \tl_clear:N #4
512 \@@_split_loop:NNN #2#3#4 #1 \q_recursion_tail \q_recursion_stop
513 \@@_split_tidy:N #2
514 \@@_split_tidy:N #4
515 }
516 \cs_generate_variant:Nn \@@_split:nNNN { V }
517 \cs_new_protected:Npn \@@_split_loop:NNN #1#2#3
518 {
519 \peek_remove_spaces:n
520 {
521 \peek_catcode:NTF \c_group_begin_token
522 { \@@_split_group:NNNn #1#2#3 }
523 { \@@_split_token:NNNN #1#2#3 }
524 }
525 }
526 \cs_new_protected:Npn \@@_split_group:NNNn #1#2#3#4
527 {
528 \tl_if_empty:NTF #2
529 { \tl_put_right:Nn #1 { {#4} } }
530 { \tl_put_right:Nn #3 { {#4} } }
531 \@@_split_loop:NNN #1#2#3
532 }
533 \cs_new_protected:Npn \@@_split_token:NNNN #1#2#3#4
534 {
535 \quark_if_recursion_tail_stop:N #4
536 \tl_if_empty:NTF \l_@@_after_tl
537 {
538 \siunitx_if_number_token:NTF #4
539 { \tl_put_right:Nn #2 {#4} }
540 {
541 \tl_if_empty:NTF #2
542 { \tl_put_right:Nn #1 {#4} }
543 { \tl_put_right:Nn #3 {#4} }
544 }
545 }
546 { \tl_put_right:Nn #3 {#4} }
547 \@@_split_loop:NNN #1#2#3
548 }
549 % \end{macrocode}
550 % \end{macro}
551 % \end{macro}
552 % \end{macro}
553 % \end{macro}
554 %
555 % \begin{macro}{\@@_split_tidy:N}
556 % \begin{macro}{\@@_split_tidy:Nn, \@@_split_tidy:NV}
557 % A quick test for the entire content being surrounded by a set of braces:
558 % rather than look explicitly, use the fact that a string comparison can
559 % detect the same thing. The auxiliary is needed to avoid having to go
560 % \foreign{via} a |:D| function (for the expansion behaviour).
561 % \begin{macrocode}
562 \cs_new_protected:Npn \@@_split_tidy:N #1
563 {
564 \tl_if_empty:NF #1
565 { \@@_split_tidy:NV #1 #1 }
566 }
567 \cs_new_protected:Npn \@@_split_tidy:Nn #1#2
568 {
569 \str_if_eq:onT { \exp_after:wN { \use:n #2 } } {#2}
570 { \tl_set:No #1 { \use:n #2 } }
571 }
572 \cs_generate_variant:Nn \@@_split_tidy:Nn { NV }
573 % \end{macrocode}
574 % \end{macro}
575 % \end{macro}
576 %
577 % \subsection{Printing numbers in cells: spacing}
578 %
579 % Getting the general alignment correct in tables is made more complex than one
580 % would like by the \pkg{colortbl} package. In the original \LaTeXe{}
581 % definition, cell material is centred by a construction of the (primitive)
582 % form
583 % \begin{verbatim}
584 % \hfil
585 % #
586 % \hfil
587 % \end{verbatim}
588 % which only uses \texttt{fil} stretch. That is altered by \pkg{colortbl} to
589 % broadly
590 % \begin{verbatim}
591 % \hskip 0pt plus 0.5fill
592 % \kern 0pt
593 % #
594 % \hskip 0pt plus 0.5fill
595 % \end{verbatim}
596 % which means there is \texttt{fill} stretch to worry about and the kern as
597 % well.
598 %
599 % \begin{macro}{\@@_skip:n}
600 % To prevent combination of skips, a kern is inserted after each one.
601 % This is best handled as a short auxiliary.
602 % \begin{macrocode}
603 \cs_new_protected:Npn \@@_skip:n #1
604 {
605 \skip_horizontal:n {#1}
606 \tex_kern:D \c_zero_skip
607 }
608 % \end{macrocode}
609 % \end{macro}
610 %
611 % \begin{variable}{\l_@@_column_width_dim, \l_@@_fixed_width_bool}
612 % Settings which apply to aligned columns in general.
613 % \begin{macrocode}
614 \dim_new:N \l_@@_column_width_dim
615 \keys_define:nn { siunitx }
616 {
617 table-column-width .code:n =
618 {
619 \dim_set:Nn \l_@@_column_width_dim {#1}
620 \dim_compare:nNnT \l_@@_column_width_dim > \c_zero_dim
621 { \bool_set_true:N \l_@@_fixed_width_bool }
622 } ,
623 table-fixed-width .bool_set:N =
624 \l_@@_fixed_width_bool
625 }
626 % \end{macrocode}
627 % \end{variable}
628 %
629 % \begin{macro}
630 % {\@@_align_center:n, \@@_align_left:n, \@@_align_right:n, \@@_align_none:n}
631 % \begin{macro}{\@@_align_auxi:nn, \@@_align_auxii:nn, \@@_align_auxiii:nn}
632 % The beginning and end of each table cell have to adjust the position of
633 % the content using glue. When \pkg{colortbl} is loaded the glue is done in
634 % two parts: one for our positioning and one to explicitly override that from
635 % the package. Using a two-step auxiliary chain avoids needing to repeat any
636 % code and the impact of the extra expansion should be trivial. There is a
637 % further wrinkle, in that if \pkg{tabularray} is being used, we need to skip
638 % the \pkg{colortbl} fix (see
639 % \url{https://github.com/lvjr/tabularray/issues/512}).
640 % \begin{macrocode}
641 \cs_new_protected:Npn \@@_align_center:n #1
642 { \@@_align_auxi:nn {#1} { 0pt~plus~0.5fill } }
643 \cs_new_protected:Npn \@@_align_left:n #1
644 { \@@_align_auxi:nn {#1} { 0pt } }
645 \cs_new_protected:Npn \@@_align_right:n #1
646 { \@@_align_auxi:nn {#1} { 0pt~plus~1fill } }
647 \cs_new_protected:Npn \@@_align_none:n #1
648 {
649 \bool_if:NTF \l_@@_fixed_width_bool
650 { \hbox_to_wd:nn \l_@@_column_width_dim }
651 { \use:n }
652 {#1}
653 }
654 \cs_new_protected:Npn \@@_align_auxi:nn #1#2
655 {
656 \bool_if:NTF \l_@@_fixed_width_bool
657 { \hbox_to_wd:nn \l_@@_column_width_dim }
658 { \use:n }
659 {
660 \@@_skip:n {#2}
661 #1
662 \@@_skip:n { 0pt~plus~1fill - #2 }
663 }
664 }
665 \AtBeginDocument
666 {
667 \@ifpackageloaded { colortbl }
668 {
669 \cs_new_eq:NN
670 \@@_align_auxii:nn
671 \@@_align_auxi:nn
672 \cs_gset_protected:Npn \@@_align_auxi:nn #1#2
673 {
674 \@@_skip:n { 0pt~plus~-0.5fill }
675 \@@_align_auxii:nn {#1} {#2}
676 \@@_skip:n { 0pt~plus~-0.5fill }
677 }
678 \@ifpackageloaded { tabularray }
679 {
680 \cs_new_eq:NN
681 \@@_align_auxiii:nn
682 \@@_align_auxi:nn
683 \cs_gset_protected:Npe \@@_align_auxi:nn #1#2
684 {
685 \exp_not:N \int_compare:nNnTF
686 \cs_if_exist:NTF \gTblrLevelInt
687 { \exp_not:N \gTblrLevelInt }
688 { \exp_not:N \g_tblr_level_int }
689 = 0
690 { \@@_align_auxii:nn }
691 { \@@_align_auxiii:nn }
692 {#1} {#2}
693 }
694 }
695 { }
696 }
697 { }
698 }
699 % \end{macrocode}
700 % \end{macro}
701 % \end{macro}
702 %
703 % \subsection{Printing just text}
704 %
705 % In cases where there is no numerical part, \pkg{siunitx} allows alignment
706 % of the \enquote{escaped} text independent of the underlying column type.
707 %
708 % \begin{variable}{\l_@@_align_text_tl}
709 % Alignment is handled using a |tl| as this allows a fast lookup at the
710 % point of use.
711 % \begin{macrocode}
712 \keys_define:nn { siunitx }
713 {
714 table-text-alignment .choices:nn =
715 { center , left , right , none }
716 { \tl_set:Nn \l_@@_align_text_tl {#1} } ,
717 }
718 \tl_new:N \l_@@_align_text_tl
719 % \end{macrocode}
720 % \end{variable}
721 %
722 % \begin{macro}{\@@_print_text:n, \@@_print_text:V}
723 % Printing escaped text is easy: just place it in correctly in the
724 % column.
725 % \begin{macrocode}
726 \cs_new_protected:Npn \@@_print_text:n #1
727 {
728 \bool_set_true:N \l_@@_text_bool
729 \use:c { @@_align_ \l_@@_align_text_tl :n } {#1}
730 }
731 \cs_generate_variant:Nn \@@_print_text:n { V }
732 % \end{macrocode}
733 % \end{macro}
734 %
735 % \subsection{Number alignment: core ideas}
736 %
737 % \begin{variable}{\l_@@_integer_box, \l_@@_decimal_box}
738 % Boxes for the content before and after the decimal marker.
739 % \begin{macrocode}
740 \box_new:N \l_@@_integer_box
741 \box_new:N \l_@@_decimal_box
742 % \end{macrocode}
743 % \end{variable}
744 %
745 % \begin{variable}{\l_@@_uncert_box}
746 % An extra one for aligning separated uncertainty values.
747 % \begin{macrocode}
748 \box_new:N \l_@@_uncert_box
749 % \end{macrocode}
750 % \end{variable}
751 %
752 % \begin{macro}{\@@_fil:, \@@_fill:}
753 % Primitives renamed.
754 % \begin{macrocode}
755 \cs_new_eq:NN \@@_fil: \tex_hfil:D
756 \cs_new_eq:NN \@@_fill: \tex_hfill:D
757 % \end{macrocode}
758 % \end{macro}
759 %
760 % \begin{macro}{\@@_cleanup_decimal:w}
761 % To remove the excess marker tokens in a decimal part.
762 % \begin{macrocode}
763 \cs_new:Npn \@@_cleanup_decimal:w
764 #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_nil #6 \q_nil #7 \q_nil
765 { #1#2#3#4#5#6#7 }
766 % \end{macrocode}
767 % \end{macro}
768 %
769 % \begin{macro}{\@@_color_check:N}
770 % \begin{macro}{\@@_color_check:w}
771 % \begin{macro}{\@@_color_check:Nnw}
772 % Handle the fact that splitting a number can leave a negative color
773 % dangling.
774 % \begin{macrocode}
775 \cs_new_protected:Npn \@@_color_check:N #1
776 { \exp_after:wN \@@_color_check:w #1 \q_stop }
777 \cs_new_protected:Npn \@@_color_check:w #1 \q_nil #2 \q_stop
778 {
779 \tl_if_head_eq_meaning:nNT {#1} \color
780 { \@@_color_check:Nnw #1 \q_stop }
781 }
782 \cs_new_protected:Npn \@@_color_check:Nnw #1#2#3 \q_stop
783 { \keys_set:nn { siunitx } { number-color = #2 } }
784 % \end{macrocode}
785 % \end{macro}
786 % \end{macro}
787 % \end{macro}
788 %
789 % \begin{macro}{\@@_center_marker:}
790 % When centering on the decimal marker, the easiest approach is to simply
791 % re-box the two parts. That is needed whether or not we are parsing numbers,
792 % so is best as a short auxiliary. Notice that we need to allow for the width
793 % of the decimal marker itself. When not aligning non-numerical material, we
794 % put the extra space into the boxes around the number.
795 % \begin{macrocode}
796 \cs_new_protected:Npn \@@_center_marker:
797 {
798 \hbox_set:Nn \l_@@_tmp_box
799 { \ensuremath { \mathord { \l_siunitx_number_output_decimal_tl } } }
800 \dim_compare:nNnTF
801 { \box_wd:N \l_@@_integer_box }
802 >
803 {
804 \box_wd:N \l_@@_decimal_box
805 - \box_wd:N \l_@@_tmp_box
806 }
807 {
808 \bool_if:NTF \l_@@_align_after_bool
809 {
810 \@@_center_marker_aux:Nnnn \l_@@_decimal_box
811 {
812 \box_wd:N \l_@@_integer_box
813 + \box_wd:N \l_@@_tmp_box
814 }
815 }
816 {
817 \@@_center_marker_aux:Nnnn \l_@@_after_box
818 {
819 \box_wd:N \l_@@_after_box
820 + \box_wd:N \l_@@_integer_box
821 - \box_wd:N \l_@@_decimal_box
822 + \box_wd:N \l_@@_tmp_box
823 }
824 }
825 { } { \@@_fil: }
826 }
827 {
828 \bool_if:NTF \l_@@_align_before_bool
829 {
830 \@@_center_marker_aux:Nnnn \l_@@_integer_box
831 {
832 \box_wd:N \l_@@_decimal_box
833 - \box_wd:N \l_@@_tmp_box
834 }
835 }
836 {
837 \@@_center_marker_aux:Nnnn \l_@@_before_box
838 {
839 \box_wd:N \l_@@_before_box
840 + \box_wd:N \l_@@_decimal_box
841 - \box_wd:N \l_@@_integer_box
842 - \box_wd:N \l_@@_tmp_box
843 }
844 }
845 { \@@_fil: } { }
846 }
847 }
848 \cs_new_protected:Npn \@@_center_marker_aux:Nnnn #1#2#3#4
849 {
850 \hbox_set_to_wd:Nnn #1 {#2}
851 {
852 #3
853 \hbox_unpack:N #1
854 #4
855 }
856 }
857 % \end{macrocode}
858 % \end{macro}
859 %
860 % \begin{variable}
861 % {
862 % \l_@@_auto_round_bool ,
863 % \l_@@_model_setup_tl ,
864 % \l_@@_align_mode_str ,
865 % \l_@@_align_number_str
866 % }
867 % Options for tables with defined space.
868 % \begin{macrocode}
869 \keys_define:nn { siunitx }
870 {
871 table-alignment .meta:n =
872 { table-number-alignment = #1 , table-text-alignment = #1 },
873 table-alignment-mode .choices:nn =
874 { none , format , marker }
875 { \str_set_eq:NN \l_@@_align_mode_str \l_keys_choice_str } ,
876 table-auto-round .bool_set:N =
877 \l_@@_auto_round_bool ,
878 table-format .code:n =
879 {
880 \group_begin:
881 \protected@edef \l_@@_tmp_tl {#1}
882 \exp_args:NNV \group_end:
883 \@@_split:nNNN \l_@@_tmp_tl
884 \l_@@_before_model_tl
885 \l_@@_model_tl
886 \l_@@_after_model_tl
887 \@@_generate_model:V \l_@@_model_tl
888 \tl_set:Nn \l_@@_align_mode_str { format }
889 } ,
890 table-model-setup .tl_set:N =
891 \l_@@_model_setup_tl ,
892 table-number-alignment .choices:nn =
893 { center , left , right }
894 { \str_set_eq:NN \l_@@_align_number_str \l_keys_choice_str }
895 }
896 \str_new:N \l_@@_align_mode_str
897 \str_new:N \l_@@_align_number_str
898 % \end{macrocode}
899 % \end{variable}
900 %
901 % \begin{variable}{\l_@@_format_tl, \l_@@_model_tl}
902 % The input and output versions of the model entry in a table.
903 % \begin{macrocode}
904 \tl_new:N \l_@@_format_tl
905 \tl_new:N \l_@@_before_model_tl
906 \tl_new:N \l_@@_model_tl
907 \tl_new:N \l_@@_after_model_tl
908 % \end{macrocode}
909 % \end{variable}
910 %
911 % \begin{macro}{\@@_generate_model:n, \@@_generate_model:V}
912 % \begin{macro}{\@@_generate_model:nnnnnnn}
913 % \begin{macro}[EXP]{\@@_generate_model_S:nnw}
914 % \begin{macro}[EXP]{\@@_generate_model_S:nnn, \@@_generate_model_S:een}
915 % Creating a model for a table at this stage means parsing the format and
916 % converting that to an appropriate model. Things are quite straight-forward
917 % other than the uncertainty part. At this stage there is no point in
918 % formatting the model: that has to happen at point-of-use. Notice that
919 % the uncertainty part needs to allow for the case where we cross the
920 % decimal.
921 % \begin{macrocode}
922 \cs_new_protected:Npn \@@_generate_model:n #1
923 {
924 \group_begin:
925 \bool_set_true:N \l_siunitx_number_parse_bool
926 \keys_set:nn { siunitx } { retain-explicit-plus = true }
927 \siunitx_number_parse:nN {#1} \l_@@_format_tl
928 \exp_args:NNNV \group_end:
929 \tl_set:Nn \l_@@_format_tl \l_@@_format_tl
930 \tl_if_empty:NF \l_@@_format_tl
931 {
932 \exp_after:wN \@@_generate_model:nnnnnnn
933 \l_@@_format_tl
934 }
935 }
936 \cs_generate_variant:Nn \@@_generate_model:n { V }
937 \cs_new_protected:Npn \@@_generate_model:nnnnnnn #1#2#3#4#5#6#7
938 {
939 \tl_set:Ne \l_@@_model_tl
940 {
941 \exp_not:n { {#1} {#2} }
942 {
943 \int_compare:nNnTF {#3} = 0
944 { 0 }
945 { \prg_replicate:nn {#3} { 8 } }
946 }
947 { \prg_replicate:nn { 0 #4 } { 8 } }
948 {
949 \tl_if_blank:nF {#5}
950 {
951 \use:c { @@_generate_model_ \tl_head:n {#5} :nnw }
952 {#4} #5
953 }
954 }
955 \exp_not:n { {#6} }
956 {
957 \int_compare:nNnTF { 0#7 } = 0
958 { 0 }
959 { \prg_replicate:nn { 0#7 } { 8 } }
960 }
961 }
962 }
963 \cs_new:Npn \@@_generate_model_S:nnw #1#2#3
964 {
965 { S }
966 {
967 \@@_generate_model_S:een
968 { \tl_count:n {#1} } { \tl_count:n {#3} }
969 {#3}
970 }
971 }
972 \cs_new:Npn \@@_generate_model_S:nnn #1#2#3
973 {
974 \prg_replicate:nn
975 {
976 \int_compare:nNnTF {#2} > {#1}
977 {
978 \str_range:nnn {#3} { 1 } {#1}
979 +
980 \str_range:nnn {#3} { 1 + #1 } {#2}
981 }
982 {#3}
983 }
984 { 8 }
985 }
986 \cs_generate_variant:Nn \@@_generate_model_S:nnn { ee }
987 % \end{macrocode}
988 % \end{macro}
989 % \end{macro}
990 % \end{macro}
991 % \end{macro}
992 %
993 % \subsection{Directly printing without collection}
994 %
995 % Collecting the number allows for various effects but is not as fast
996 % as simply aligning on the first token that is a decimal marker. The
997 % strategy here is that used by \pkg{dcolumn}.
998 %
999 % \begin{macro}{\@@_direct_begin:}
1000 % \begin{macro}{\@@_direct_begin:w}
1001 % \begin{macro}{\@@_direct_end:}
1002 % \begin{macro}
1003 % {
1004 % \@@_direct_marker: ,
1005 % \@@_direct_marker_switch: ,
1006 % \@@_direct_marker_end:
1007 % }
1008 % \begin{macro}{\@@_direct_format:}
1009 % \begin{macro}[EXP]{\@@_direct_format:nnnnnnn}
1010 % \begin{macro}{\@@_direct_format:w}
1011 % \begin{macro}
1012 % {
1013 % \@@_direct_format_switch: ,
1014 % \@@_direct_format_end:
1015 % }
1016 % \begin{macro}{\@@_direct_none:, \@@_direct_none_end:}
1017 % After removing the |\ignorespaces| at the start of the cell (see comments
1018 % for \cs{@@_collect_begin:N}), check to see if there is a |{| and branch
1019 % as appropriate.
1020 % \begin{macrocode}
1021 \cs_new_protected:Npn \@@_direct_begin:
1022 { \@@_direct_begin:w }
1023 \cs_new_protected:Npn \@@_direct_begin:w #1 \ignorespaces
1024 {
1025 #1
1026 \peek_remove_spaces:n
1027 {
1028 \peek_catcode:NTF \c_group_begin_token
1029 { \@@_print_text:n }
1030 {
1031 \m@th
1032 \use:c { @@_direct_ \l_@@_align_mode_str : }
1033 }
1034 }
1035 }
1036 \cs_new_protected:Npn \@@_direct_end:
1037 { \use:c { @@_direct_ \l_@@_align_mode_str _end: } }
1038 % \end{macrocode}
1039 % When centring the content about a decimal marker, the trick is
1040 % to collect everything into two boxes and then compare the sizes.
1041 % As we are always in math mode, we can use a math active token
1042 % to make the switch. The up-front setting of the |decimal| box deals
1043 % with the case where there is no decimal part.
1044 % \begin{macrocode}
1045 \cs_new_protected:Npn \@@_direct_marker:
1046 {
1047 \hbox_set:Nn \l_@@_tmp_box
1048 { \ensuremath { \mathord { \l_siunitx_number_output_decimal_tl } } }
1049 \hbox_set_to_wd:Nnn \l_@@_decimal_box
1050 { \box_wd:N \l_@@_tmp_box }
1051 { \@@_fil: }
1052 \hbox_set:Nw \l_@@_integer_box
1053 \c_math_toggle_token
1054 \tl_map_inline:Nn \l_siunitx_number_input_decimal_tl
1055 {
1056 \char_set_active_eq:NN ##1 \@@_direct_marker_switch:
1057 \char_set_mathcode:nn { `##1 } { "8000 }
1058 }
1059 }
1060 \cs_new_protected:Npn \@@_direct_marker_switch:
1061 {
1062 \c_math_toggle_token
1063 \hbox_set_end:
1064 \hbox_set:Nw \l_@@_decimal_box
1065 \c_math_toggle_token
1066 \l_siunitx_number_output_decimal_tl
1067 }
1068 % \end{macrocode}
1069 % We set the two alignment booleans here so that a single auxiliary can
1070 % cover this case as well as the one for centering the marker when also
1071 % parsing.
1072 % \begin{macrocode}
1073 \cs_new_protected:Npn \@@_direct_marker_end:
1074 {
1075 \c_math_toggle_token
1076 \hbox_set_end:
1077 \bool_set_true:N \l_@@_align_before_bool
1078 \bool_set_true:N \l_@@_align_after_bool
1079 \@@_center_marker:
1080 \use:c { @@_align_ \l_@@_align_text_tl :n }
1081 {
1082 \box_use_drop:N \l_@@_integer_box
1083 \box_use_drop:N \l_@@_decimal_box
1084 }
1085 }
1086 % \end{macrocode}
1087 % For the version where there is space reserved, first format and decompose
1088 % that, then create appropriately-sized boxes.
1089 % \begin{macrocode}
1090 \cs_new_protected:Npn \@@_direct_format:
1091 {
1092 \tl_set:Ne \l_@@_tmp_tl
1093 { \siunitx_number_output:NN \l_@@_model_tl \q_nil }
1094 \exp_after:wN \@@_direct_format_aux:w
1095 \l_@@_tmp_tl \q_stop
1096 }
1097 \cs_new_protected:Npn \@@_direct_format_aux:w
1098 #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_stop
1099 {
1100 \hbox_set:Nn \l_@@_tmp_box
1101 { \ensuremath { \@@_cleanup_decimal:w #4 } }
1102 \hbox_set_to_wd:Nnn \l_@@_decimal_box
1103 { \box_wd:N \l_@@_tmp_box }
1104 { \@@_fil: }
1105 \hbox_set:Nn \l_@@_tmp_box { \ensuremath { #1#2#3 } }
1106 \hbox_set_to_wd:Nnw \l_@@_integer_box
1107 { \box_wd:N \l_@@_tmp_box }
1108 \c_math_toggle_token
1109 \tl_map_inline:Nn \l_siunitx_number_input_decimal_tl
1110 {
1111 \char_set_active_eq:NN ##1 \@@_direct_format_switch:
1112 \char_set_mathcode:nn { `##1 } { "8000 }
1113 }
1114 \@@_fill:
1115 }
1116 \cs_new_protected:Npn \@@_direct_format_switch:
1117 {
1118 \c_math_toggle_token
1119 \hbox_set_end:
1120 \hbox_set_to_wd:Nnw \l_@@_decimal_box
1121 { \box_wd:N \l_@@_decimal_box }
1122 \c_math_toggle_token
1123 \mathord { \l_siunitx_number_output_decimal_tl }
1124 }
1125 \cs_new_protected:Npn \@@_direct_format_end:
1126 {
1127 \c_math_toggle_token
1128 \@@_fil:
1129 \hbox_set_end:
1130 \use:c { @@_align_ \l_@@_align_number_str :n }
1131 {
1132 \box_use_drop:N \l_@@_integer_box
1133 \box_use_drop:N \l_@@_decimal_box
1134 }
1135 }
1136 % \end{macrocode}
1137 % No parsing and no alignment is easy.
1138 % \begin{macrocode}
1139 \cs_new_protected:Npn \@@_direct_none: { \c_math_toggle_token }
1140 \cs_new_protected:Npn \@@_direct_none_end: { \c_math_toggle_token }
1141 % \end{macrocode}
1142 % \end{macro}
1143 % \end{macro}
1144 % \end{macro}
1145 % \end{macro}
1146 % \end{macro}
1147 % \end{macro}
1148 % \end{macro}
1149 % \end{macro}
1150 % \end{macro}
1151 %
1152 % \subsection{Printing numbers in cells: main functions}
1153 %
1154 % \begin{variable}{\l_@@_before_box, \l_@@_after_box}
1155 % For alignment of text outside of a number.
1156 % \begin{macrocode}
1157 \box_new:N \l_@@_before_box
1158 \box_new:N \l_@@_after_box
1159 % \end{macrocode}
1160 % \end{variable}
1161 %
1162 % \begin{variable}{\l_@@_before_dim}
1163 % Space reserved for any non-numerical text before the number: as we need
1164 % to allow for this to be available after setting the integer part, we need
1165 % to carry it along for a bit.
1166 % \begin{macrocode}
1167 \dim_new:N \l_@@_before_dim
1168 % \end{macrocode}
1169 % \end{variable}
1170 %
1171 % \begin{variable}{\l_@@_carry_dim}
1172 % Used to \enquote{carry forward} the amount of white space which needs to
1173 % be inserted after the decimal marker.
1174 % \begin{macrocode}
1175 \dim_new:N \l_@@_carry_dim
1176 % \end{macrocode}
1177 % \end{variable}
1178 %
1179 % \begin{variable}
1180 % {
1181 % \l_@@_align_comparator_bool ,
1182 % \l_@@_align_exponent_bool ,
1183 % \l_@@_align_after_bool ,
1184 % \l_@@_align_before_bool ,
1185 % \l_@@_align_uncertainty_bool
1186 % }
1187 % \begin{macrocode}
1188 \keys_define:nn { siunitx }
1189 {
1190 table-align-comparator .bool_set:N =
1191 \l_@@_align_comparator_bool ,
1192 table-align-exponent .bool_set:N =
1193 \l_@@_align_exponent_bool ,
1194 table-align-text-after .bool_set:N =
1195 \l_@@_align_after_bool ,
1196 table-align-text-before .bool_set:N =
1197 \l_@@_align_before_bool ,
1198 table-align-uncertainty .bool_set:N =
1199 \l_@@_align_uncertainty_bool
1200 }
1201 % \end{macrocode}
1202 % \end{variable}
1203 %
1204 % \begin{macro}{\@@_print:nnn, \@@_print:VVV}
1205 % \begin{macro}{\@@_print_marker:nnn}
1206 % \begin{macro}{\@@_print_marker:w}
1207 % \begin{macro}[EXP]{\@@_print_marker_aux:w}
1208 % \begin{macro}{\@@_print_format:nnn}
1209 % \begin{macro}[EXP]{\@@_print_format:nnnnnn}
1210 % \begin{macro}
1211 % {
1212 % \@@_print_format_auxi:w ,
1213 % \@@_print_format_auxii:w ,
1214 % \@@_print_format_auxiii:w ,
1215 % \@@_print_format_auxiv:w ,
1216 % \@@_print_format_auxv:w ,
1217 % \@@_print_format_auxvi:w ,
1218 % \@@_print_format_auxvii:w ,
1219 % \@@_print_format_auxviii:w ,
1220 % \@@_print_format_auxix:w ,
1221 % \@@_print_format_auxx:w ,
1222 % \@@_print_format_auxxi:w ,
1223 % \@@_print_format_auxxii:w ,
1224 % \@@_print_format_auxxiii:w
1225 % }
1226 % \begin{macro}
1227 % {
1228 % \@@_print_format_box:Nn, \@@_print_format_box:No ,
1229 % \@@_print_model_box:Nn, \@@_print_model_box:No
1230 % }
1231 % \begin{macro}{\@@_print_format_box:nNn, \@@_print_format_box:VNn}
1232 % \begin{macro}{\@@_print_format_after:N}
1233 % \begin{macro}{\@@_print_none:nnn}
1234 % \begin{macrocode}
1235 \cs_new_protected:Npn \@@_print:nnn #1#2#3
1236 { \use:c { @@_print_ \l_@@_align_mode_str :nnn } {#1} {#2} {#3} }
1237 \cs_generate_variant:Nn \@@_print:nnn { VVV }
1238 % \end{macrocode}
1239 % When centering on the decimal marker, alignment is relatively simple, and
1240 % close in concept to that used without parsing. First we need to deal with
1241 % any text before or after the number. For text \emph{before}, there's the
1242 % case where is has no width and might be a font or color change: that has to
1243 % be filtered out first. Then we can adjust the size of this material and
1244 % that after the number such that they are equal. The number itself can then
1245 % be formatted, splitting at he decimal marker. A bit more size adjustment,
1246 % then the number itself and any text at the end can be inserted.
1247 % \begin{macrocode}
1248 \cs_new_protected:Npn \@@_print_marker:nnn #1#2#3
1249 {
1250 \hbox_set:Nn \l_@@_before_box {#1}
1251 \dim_compare:nNnT { \box_wd:N \l_@@_before_box } = { 0pt }
1252 {
1253 \box_clear:N \l_@@_before_box
1254 #1
1255 }
1256 \hbox_set:Nn \l_@@_after_box {#3}
1257 \dim_compare:nNnTF
1258 { \box_wd:N \l_@@_after_box }
1259 > { \box_wd:N \l_@@_before_box }
1260 {
1261 \hbox_set_to_wd:Nnn \l_@@_before_box
1262 { \box_wd:N \l_@@_after_box }
1263 {
1264 \@@_fil:
1265 \hbox_unpack:N \l_@@_before_box
1266 }
1267 }
1268 {
1269 \hbox_set_to_wd:Nnn \l_@@_after_box
1270 { \box_wd:N \l_@@_before_box }
1271 {
1272 \hbox_unpack:N \l_@@_after_box
1273 \@@_fil:
1274 }
1275 }
1276 \siunitx_number_parse:nN {#2} \l_@@_tmp_tl
1277 \siunitx_number_process:NN \l_@@_tmp_tl \l_@@_tmp_tl
1278 \tl_set:Ne \l_@@_tmp_tl
1279 { \siunitx_number_output:NN \l_@@_tmp_tl \q_nil }
1280 \@@_color_check:N \l_@@_tmp_tl
1281 \exp_after:wN \@@_print_marker:w
1282 \l_@@_tmp_tl \q_stop
1283 }
1284 \cs_new_protected:Npn \@@_print_marker:w
1285 #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_stop
1286 {
1287 \hbox_set:Nn \l_@@_integer_box
1288 { \siunitx_print_number:n { #1#2#3 } }
1289 \hbox_set:Nn \l_@@_decimal_box
1290 {
1291 \siunitx_print_number:e
1292 { \@@_print_marker_aux:w #4 \q_stop }
1293 }
1294 \@@_center_marker:
1295 \use:c { @@_align_ \l_@@_align_text_tl :n }
1296 {
1297 \box_use_drop:N \l_@@_before_box
1298 \box_use_drop:N \l_@@_integer_box
1299 \box_use_drop:N \l_@@_decimal_box
1300 \box_use_drop:N \l_@@_after_box
1301 }
1302 }
1303 \cs_new:Npn \@@_print_marker_aux:w
1304 #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_nil #5
1305 \q_nil #6 \q_nil #7 \q_nil #8 \q_stop
1306 {
1307 \bool_lazy_and:nnTF
1308 { \tl_if_blank_p:n {#1#2} }
1309 { ! \tl_if_blank_p:n {#3} }
1310 { { } }
1311 { \exp_not:n {#1#2} }
1312 \exp_not:n {#3#4#5#6}
1313 \tl_if_blank:nT {#1#2#3#4#5#6} { { } }
1314 \exp_not:n {#7#8}
1315 }
1316 % \end{macrocode}
1317 % For positioning based on a format, we have to work part-by-part as there
1318 % are a number of alignment points to get right. As for the |marker| approach,
1319 % first we check if the material before the numerical content is of zero
1320 % width. Next we need to format the model and content numbers, before
1321 % starting an auxiliary chain to pick out the various parts in order. We have
1322 % to carry the amount of space for the non-numerical material before the cell
1323 % forward: this may end up being enlarged by unused parts of the integer.
1324 % \begin{macrocode}
1325 \cs_new_protected:Npn \@@_print_format:nnn #1#2#3
1326 {
1327 \hbox_set:Nn \l_@@_tmp_box { \l_@@_before_model_tl }
1328 \hbox_set:Nn \l_@@_before_box {#1}
1329 \dim_compare:nNnT { \box_wd:N \l_@@_before_box } = { 0pt }
1330 {
1331 \box_clear:N \l_@@_before_box
1332 #1
1333 }
1334 \dim_set:Nn \l_@@_before_dim { \box_wd:N \l_@@_tmp_box }
1335 \siunitx_number_parse:nN {#2} \l_@@_tmp_tl
1336 \group_begin:
1337 \bool_if:NT \l_@@_auto_round_bool
1338 {
1339 \keys_set:ne { siunitx }
1340 {
1341 round-mode = places ,
1342 round-pad = true ,
1343 round-precision =
1344 \exp_after:wN \@@_print_format:nnnnnn
1345 \l_@@_format_tl
1346 }
1347 }
1348 \siunitx_number_process:NN \l_@@_tmp_tl \l_@@_tmp_tl
1349 \exp_args:NNNV \group_end:
1350 \tl_set:Nn \l_@@_tmp_tl \l_@@_tmp_tl
1351 \tl_set:Ne \l_@@_tmp_tl
1352 {
1353 \siunitx_number_output:NN \l_@@_model_tl \q_nil
1354 \exp_not:N \q_mark
1355 \siunitx_number_output:NN \l_@@_tmp_tl \q_nil
1356 }
1357 \exp_after:wN \@@_print_format_auxi:w
1358 \l_@@_tmp_tl \q_stop
1359 \hbox_set:Nn \l_@@_tmp_box { \l_@@_after_model_tl }
1360 \hbox_set_to_wd:Nnn \l_@@_after_box
1361 { \box_wd:N \l_@@_tmp_box + \l_@@_carry_dim }
1362 {
1363 \bool_if:NT \l_@@_align_after_bool
1364 { \skip_horizontal:n { \l_@@_carry_dim } }
1365 #3
1366 \@@_fil:
1367 }
1368 \use:c { @@_align_ \l_@@_align_number_str :n }
1369 {
1370 \box_use_drop:N \l_@@_before_box
1371 \box_use_drop:N \l_@@_integer_box
1372 \box_use_drop:N \l_@@_decimal_box
1373 \box_use_drop:N \l_@@_after_box
1374 }
1375 }
1376 \cs_new:Npn \@@_print_format:nnnnnn #1#2#3#4#5#6#7
1377 { 0 #4 }
1378 % \end{macrocode}
1379 % The first numerical part to handle is the comparator. Any white space we
1380 % need to add goes into the text part \emph{if} alignment is not active
1381 % (\foreign{i.e.}~we are looking \enquote{backwards} to place this filler).
1382 % \begin{macrocode}
1383 \cs_new_protected:Npn \@@_print_format_auxi:w
1384 #1 \q_nil #2 \q_mark #3 \q_nil #4 \q_stop
1385 {
1386 \@@_color_check:w #3 \q_nil \q_stop
1387 \@@_print_model_box:Nn \l_@@_tmp_box {#1}
1388 \bool_if:NTF \l_@@_align_before_bool
1389 {
1390 \hbox_set_to_wd:Nnn \l_@@_integer_box
1391 { \box_wd:N \l_@@_tmp_box }
1392 {
1393 \@@_fil:
1394 \tl_if_blank:nF {#3}
1395 { \siunitx_print_number:n {#3} }
1396 }
1397 }
1398 {
1399 \@@_print_format_box:Nn \l_@@_integer_box {#3}
1400 \dim_add:Nn \l_@@_before_dim
1401 {
1402 \box_wd:N \l_@@_tmp_box
1403 - \box_wd:N \l_@@_integer_box
1404 }
1405 }
1406 \@@_print_format_auxii:w #2 \q_mark #4 \q_stop
1407 }
1408 % \end{macrocode}
1409 % The integer part follows much the same pattern, except now it is control
1410 % of the comparator alignment that determines where the white space goes.
1411 % We want to make sure that \emph{if} the integer is too long, it pokes out
1412 % to the left, and at the same time we want to issue exactly one overfull
1413 % box warning. That is achieved using an explicit test, such that if there
1414 % is an issue we force the size of the content appropriately. That process
1415 % will lead to a hit but only for \enquote{defective} input.
1416 %
1417 % As we already have content in the |integer| box, we need to measure how
1418 % much \emph{extra} material has been added.
1419 %
1420 % As the integer part is completed here, we are able to finalise the width
1421 % of the pre-numeral part, reboxing it to have the correct width and possibly
1422 % to force a single overfull warning if appropriate.
1423 % \begin{macrocode}
1424 \cs_new_protected:Npn \@@_print_format_auxii:w
1425 #1 \q_nil #2 \q_nil #3 \q_mark #4 \q_nil #5 \q_nil #6 \q_stop
1426 {
1427 \@@_print_model_box:Nn \l_@@_tmp_box {#1#2}
1428 \dim_set:Nn \l_@@_tmp_dim { \box_wd:N \l_@@_tmp_box }
1429 \@@_print_format_box:Nn \l_@@_tmp_box {#4#5}
1430 \dim_compare:nNnT { \box_wd:N \l_@@_tmp_box } > \l_@@_tmp_dim
1431 {
1432 \hbox_set_to_wd:Nnn \l_@@_tmp_box \l_@@_tmp_dim
1433 { \hbox_unpack:N \l_@@_tmp_box }
1434 \hbox_set_to_wd:Nnn \l_@@_tmp_box \l_@@_tmp_dim
1435 {
1436 \tex_hss:D
1437 \hbox_unpack:N \l_@@_tmp_box
1438 }
1439 }
1440 \bool_lazy_and:nnTF
1441 { \l_@@_align_comparator_bool }
1442 { \dim_compare_p:nNn { \box_wd:N \l_@@_integer_box } > { 0pt } }
1443 {
1444 \hbox_set_to_wd:Nnn \l_@@_integer_box
1445 {
1446 \box_wd:N \l_@@_integer_box
1447 + \l_@@_tmp_dim
1448 }
1449 {
1450 \hbox_unpack:N \l_@@_integer_box
1451 \@@_fill:
1452 \hbox_unpack:N \l_@@_tmp_box
1453 }
1454 }
1455 {
1456 \bool_if:NTF \l_@@_align_before_bool
1457 {
1458 \hbox_set_to_wd:Nnn \l_@@_integer_box
1459 {
1460 \box_wd:N \l_@@_integer_box
1461 + \l_@@_tmp_dim
1462 }
1463 {
1464 \@@_fil:
1465 \hbox_unpack:N \l_@@_integer_box
1466 \hbox_unpack:N \l_@@_tmp_box
1467 }
1468 }
1469 {
1470 \hbox_set:Nn \l_@@_integer_box
1471 {
1472 \hbox_unpack:N \l_@@_integer_box
1473 \hbox_unpack:N \l_@@_tmp_box
1474 }
1475 \dim_add:Nn \l_@@_before_dim
1476 {
1477 \l_@@_tmp_dim
1478 - \box_wd:N \l_@@_tmp_box
1479 }
1480 }
1481 }
1482 \hbox_set_to_wd:Nnn \l_@@_before_box \l_@@_before_dim
1483 {
1484 \@@_fil:
1485 \hbox_unpack:N \l_@@_before_box
1486 }
1487 \@@_print_format_auxiii:w ? #3 \q_mark ? #6 \q_stop
1488 }
1489 % \end{macrocode}
1490 % We now deal with the decimal part: there is nothing already in the
1491 % |decimal| box, so the basics are easy. We need to \enquote{carry forward}
1492 % any white space, as where it gets inserted depends on the options for
1493 % subsequent parts. The \cs{use_none:n} here remove the two |?| above:
1494 % those are added to preserve braces around the decimal marker.
1495 % \begin{macrocode}
1496 \cs_new_protected:Npn \@@_print_format_auxiii:w
1497 #1 \q_nil #2 \q_nil #3 \q_mark #4 \q_nil #5 \q_nil #6 \q_stop
1498 {
1499 \@@_print_model_box:No \l_@@_tmp_box { \use_none:n #1#2 }
1500 \@@_print_format_box:No \l_@@_decimal_box { \use_none:n #4#5 }
1501 \dim_set:Nn \l_@@_carry_dim
1502 {
1503 \box_wd:N \l_@@_tmp_box
1504 - \box_wd:N \l_@@_decimal_box
1505 }
1506 \@@_print_format_auxiv:w #3 \q_mark #6 \q_stop
1507 }
1508 % \end{macrocode}
1509 % Any separated uncertainty is now picked up. That has a number of parts, so
1510 % the first step is to look for a sign (which will be |#1|). We then split,
1511 % either simply tidying up the markers if there is no uncertainty, or
1512 % setting it.
1513 % \begin{macrocode}
1514 \cs_new_protected:Npn \@@_print_format_auxiv:w
1515 #1 \q_nil #2 \q_mark #3 \q_nil #4 \q_stop
1516 {
1517 \tl_if_blank:nTF {#1}
1518 { \@@_print_format_auxv:w }
1519 { \@@_print_format_auxvii:w }
1520 #1 \q_nil #2 \q_mark #3 \q_nil #4 \q_stop
1521 }
1522 \cs_new_protected:Npn \@@_print_format_auxv:w
1523 #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_mark
1524 #6 \q_stop
1525 { \@@_print_format_auxvi:w #5 \q_mark #6 \q_stop }
1526 \cs_new_protected:Npn \@@_print_format_auxvi:w
1527 #1 \q_mark
1528 #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_nil #6 \q_stop
1529 { \@@_print_format_auxxiii:w #1 \q_mark #6 \q_stop }
1530 % \end{macrocode}
1531 % Sorting out the placement of the uncertainty requires both the model and
1532 % real data widths, so we store the former to avoiding needing more boxes.
1533 % It's then just a case of putting the carry-over white space in the right
1534 % place.
1535 % \begin{macrocode}
1536 \cs_new_protected:Npn \@@_print_format_auxvii:w
1537 #1 \q_mark #2 \q_stop
1538 {
1539 \bool_if:NTF \l_@@_align_uncertainty_bool
1540 { \@@_print_format_auxviii:w }
1541 { \@@_print_format_auxx:w }
1542 #1 \q_mark #2 \q_stop
1543 }
1544 % \end{macrocode}
1545 % For an aligned uncertainty, we need to work with the different parts.
1546 % \begin{macrocode}
1547 \cs_new_protected:Npn \@@_print_format_auxviii:w
1548 #1 \q_nil #2 \q_nil #3 \q_mark
1549 #4 \q_nil #5 \q_nil #6 \q_stop
1550 {
1551 \hbox_set:Nn \l_@@_tmp_box
1552 { \siunitx_print_number:n { { } #1#2 { } } }
1553 \hbox_set_to_wd:Nnn \l_@@_uncert_box
1554 { \box_wd:N \l_@@_tmp_box }
1555 {
1556 \siunitx_print_number:n { { } #4 { } }
1557 \@@_fil:
1558 \tl_if_blank:nF {#5}
1559 { \siunitx_print_number:n {#5} }
1560 }
1561 \@@_print_format_auxix:w
1562 #3 \q_mark #6 \q_stop
1563 }
1564 \cs_new_protected:Npn \@@_print_format_auxix:w
1565 #1 \q_nil #2 \q_nil #3 \q_mark
1566 #4 \q_nil #5 \q_nil #6 \q_stop
1567 {
1568 \@@_print_model_box:Nn \l_@@_tmp_box {#1#2}
1569 \hbox_set_to_wd:Nnn \l_@@_tmp_box
1570 {
1571 \box_wd:N \l_@@_uncert_box
1572 + \box_wd:N \l_@@_tmp_box
1573 }
1574 {
1575 \box_use:N \l_@@_uncert_box
1576 \tl_if_blank:nF {#2#5}
1577 { \siunitx_print_number:n {#4#5} }
1578 \@@_fil:
1579 }
1580 \dim_set:Nn \l_@@_tmp_dim { \box_wd:N \l_@@_tmp_box }
1581 \@@_print_format_auxxii:w #3 \q_mark #6 \q_stop
1582 }
1583 % \end{macrocode}
1584 % Non-aligned uncertainty: we have all of the information needed as
1585 % |#1|/|#3|, so the work is trivial.
1586 % \begin{macrocode}
1587 \cs_new_protected:Npn \@@_print_format_auxx:w
1588 #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_mark
1589 #5 \q_nil #6 \q_nil #7 \q_nil #8 \q_stop
1590 {
1591 \@@_print_format_auxxi:w
1592 #1#2#3#4 \q_mark #5#6#7#8 \q_stop
1593 }
1594 \cs_new_protected:Npn \@@_print_format_auxxi:w
1595 #1 \q_nil #2 \q_mark
1596 #3 \q_nil #4 \q_stop
1597 {
1598 \@@_print_model_box:Nn \l_@@_tmp_box { { } #1 }
1599 \dim_set:Nn \l_@@_tmp_dim { \box_wd:N \l_@@_tmp_box }
1600 \@@_print_format_box:Nn \l_@@_tmp_box { { } #3 }
1601 \@@_print_format_auxxii:w #2 \q_mark #4 \q_stop
1602 }
1603 % \end{macrocode}
1604 % Back together to put the uncertainty part onto the main value, then
1605 % move on to the exponent.
1606 % \begin{macrocode}
1607 \cs_new_protected:Npn \@@_print_format_auxxii:w
1608 #1 \q_mark #2 \q_stop
1609 {
1610 \@@_print_format_after:N \l_@@_align_uncertainty_bool
1611 \@@_print_format_auxxiii:w #1 \q_mark #2 \q_stop
1612 }
1613 % \end{macrocode}
1614 % Finally, we get to the exponent part: the multiplication symbol is
1615 % |#1| and the number itself is |#2|. The code is almost the same as for
1616 % uncertainties, which allows a shared auxiliary to be used. As |#3| could
1617 % be entirely empty, there needs to be a bit of work to ensure alignment
1618 % is retained in all cases.
1619 % \begin{macrocode}
1620 \cs_new_protected:Npn \@@_print_format_auxxiii:w
1621 #1 \q_nil #2 \q_mark #3 \q_nil #4 \q_stop
1622 {
1623 \tl_if_blank:nTF {#2}
1624 {
1625 \tl_if_blank:nF {#4}
1626 { \msg_warning:nn { siunitx } { table-exponent-space } }
1627 }
1628 {
1629 \@@_print_model_box:Nn \l_@@_tmp_box { { } #1#2 }
1630 \dim_set:Nn \l_@@_tmp_dim { \box_wd:N \l_@@_tmp_box }
1631 \@@_print_format_box:Nn \l_@@_tmp_box
1632 {
1633 \bool_lazy_and:nnT
1634 { \l_@@_align_exponent_bool }
1635 { \tl_if_blank_p:n {#3} }
1636 {
1637 \@@_print_model_box:Nn \l_@@_tmp_box { { } #1 { } }
1638 \@@_skip:n { \box_wd:N \l_@@_tmp_box }
1639 }
1640 { } #3#4
1641 }
1642 \@@_print_format_after:N \l_@@_align_exponent_bool
1643 }
1644 }
1645 % \end{macrocode}
1646 % A simple auxiliary to avoid relatively expensive use of the print routine
1647 % for empty parts. There is a separate function for the model as this allows
1648 % for the case where different font widths are in use.
1649 % \begin{macrocode}
1650 \cs_new_protected:Npn \@@_print_format_box:Nn #1#2
1651 { \@@_print_format_box:nNn { } #1 {#2} }
1652 \cs_generate_variant:Nn \@@_print_format_box:Nn { No }
1653 \cs_new_protected:Npn \@@_print_model_box:Nn #1#2
1654 { \@@_print_format_box:VNn \l_@@_model_setup_tl #1 {#2} }
1655 \cs_generate_variant:Nn \@@_print_model_box:Nn { No }
1656 \cs_new_protected:Npn \@@_print_format_box:nNn #1#2#3
1657 {
1658 \hbox_set:Nn #2
1659 {
1660 \tl_if_blank:nF {#3}
1661 {
1662 #1
1663 \siunitx_print_number:n {#3}
1664 }
1665 }
1666 }
1667 \cs_generate_variant:Nn \@@_print_format_box:nNn { V }
1668 % \end{macrocode}
1669 % A common routine for placing material after the decimal marker and
1670 % \enquote{shuffling}.
1671 % \begin{macrocode}
1672 \cs_new_protected:Npn \@@_print_format_after:N #1
1673 {
1674 \bool_if:NTF #1
1675 {
1676 \hbox_set_to_wd:Nnn \l_@@_decimal_box
1677 {
1678 \box_wd:N \l_@@_decimal_box
1679 + \l_@@_carry_dim
1680 + \box_wd:N \l_@@_tmp_box
1681 }
1682 {
1683 \box_use:N \l_@@_decimal_box
1684 \@@_fil:
1685 \hbox_unpack:N \l_@@_tmp_box
1686 }
1687 \dim_set:Nn \l_@@_carry_dim
1688 {
1689 \l_@@_tmp_dim
1690 - \box_wd:N \l_@@_tmp_box
1691 }
1692 }
1693 {
1694 \hbox_set:Nn \l_@@_decimal_box
1695 {
1696 \hbox_unpack:N \l_@@_decimal_box
1697 \hbox_unpack:N \l_@@_tmp_box
1698 }
1699 \dim_add:Nn \l_@@_carry_dim
1700 {
1701 \l_@@_tmp_dim
1702 - \box_wd:N \l_@@_tmp_box
1703 }
1704 }
1705 }
1706 % \end{macrocode}
1707 % With no alignment, everything supplied is treated more-or-less the
1708 % same as \cs{num} (but without the \pkg{xparse} wrapper).
1709 % \begin{macrocode}
1710 \cs_new_protected:Npn \@@_print_none:nnn #1#2#3
1711 {
1712 \use:c { @@_align_ \l_@@_align_number_str :n }
1713 {
1714 #1
1715 \siunitx_number_format:nN {#2} \l_@@_tmp_tl
1716 \siunitx_print_number:V \l_@@_tmp_tl
1717 #3
1718 }
1719 }
1720 % \end{macrocode}
1721 % \end{macro}
1722 % \end{macro}
1723 % \end{macro}
1724 % \end{macro}
1725 % \end{macro}
1726 % \end{macro}
1727 % \end{macro}
1728 % \end{macro}
1729 % \end{macro}
1730 % \end{macro}
1731 % \end{macro}
1732 %
1733 % \begin{macrocode}
1734 \msg_new:nnn { siunitx } { table-exponent-space }
1735 { Insufficient~space~reserved~for~tabular~exponent~\msg_line_context: }
1736 % \end{macrocode}
1737 %
1738 % \subsection{Standard settings for module options}
1739 %
1740 % Some of these follow naturally from the point of definition
1741 % (\foreign{e.g.}~boolean variables are always |false| to begin with),
1742 % but for clarity everything is set here.
1743 % \begin{macrocode}
1744 \keys_set:nn { siunitx }
1745 {
1746 table-align-comparator = true ,
1747 table-align-exponent = true ,
1748 table-align-text-after = true ,
1749 table-align-text-before = true ,
1750 table-align-uncertainty = true ,
1751 table-alignment = center ,
1752 table-auto-round = false ,
1753 table-column-width = 0pt ,
1754 table-fixed-width = false ,
1755 table-format = 2.2 ,
1756 table-number-alignment = center ,
1757 table-text-alignment = center ,
1758 % \end{macrocode}
1759 % Out of order as |table-format| sets this implicitly too.
1760 % \begin{macrocode}
1761 table-alignment-mode = marker
1762 }
1763 % \end{macrocode}
1764 %
1765 % \begin{macrocode}
1766 %</package>
1767 % \end{macrocode}
1768 %
1769 % \end{implementation}
1770 %
1771 % \PrintIndex