Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % File: siunitx-print.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-print} -- Printing material with font control^^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{Printing quantities}
64 %
65 % This submodule is focussed on providing controlled printing for numbers and
66 % units. Key to this is control of font: conventions for printing quantities
67 % mean that the exact nature of the output is important. At the same time, this
68 % module provides flexibility for the user in terms of which aspects of the font
69 % are responsive to the surrounding general text. Printing material may also
70 % take place in text or math mode.
71 %
72 % The printing routines assume that normal \LaTeXe{} font selection commands
73 % are available, in particular
74 % \begin{itemize}
75 % \item \cs{bfseries},
76 % \item \cs{mathrm},
77 % \item \cs{mathversion},
78 % \item \cs{fontfamily},
79 % \item \cs{fontseries},
80 % \item \cs{fontshape},
81 % \item \cs{familydefault},
82 % \item \cs{seriesdefault},
83 % \item \cs{shapedefault} and
84 % \item \cs{selectfont}.
85 % \end{itemize}
86 % It also requires the standard \LaTeXe{} kernel commands
87 % \begin{itemize}
88 % \item \cs{ensuremath},
89 % \item \cs{mbox},
90 % \item \cs{textsubscript} and
91 % \item \cs{textsuperscript}
92 % \end{itemize}
93 % for printing in text mode. The following packages are
94 % also required to provide the functionality detailed.
95 % \begin{itemize}
96 % \item \pkg{color}: support for color using \cs{textcolor}
97 % \item \pkg{textcomp}: \cs{textminus}, \cs{textpm}, \item \cs{texttimes} and
98 % \cs{textcenteredperiod} for printing in text mode
99 % \item \pkg{amstext}: the \cs{text} command for printing in text mode
100 % \end{itemize}
101 % For detection of math mode fonts, as well as \cs{mathrm}, the existence of
102 % \cs{symoperators} is assumed; other math font commands are not
103 % \emph{required} to exist.
104 %
105 % \begin{function}
106 % {
107 % \siunitx_print_number:n, \siunitx_print_number:V,
108 % \siunitx_print_number:e,
109 % \siunitx_print_unit:n, \siunitx_print_unit:V, \siunitx_print_unit:e,
110 % \siunitx_print_unit:o
111 % }
112 % \begin{syntax}
113 % \cs{siunitx_print_number:n} \Arg{material}
114 % \cs{siunitx_print_unit:n} \Arg{material}
115 % \end{syntax}
116 % Prints the \meta{material} according the prevailing settings for the
117 % submodule as applicable to the \meta{type} of content (|number| or |unit|).
118 % The \meta{material} should comprise normal
119 % \LaTeX{} mark-up for numbers or units. In particular, units will typically
120 % use |\mathrm| to indicate material to be printed in the current upright
121 % roman font, and |^| and |_| will typically be used to indicate super- and
122 % subscripts, respectively. These elements will be correctly handled when
123 % printing for example using |\mathsf| in math mode, or using only text
124 % fonts. No printing takes place if the \cs{material} is entirely empty
125 % after a single expansion.
126 % \end{function}
127 %
128 % \begin{function}
129 % {\siunitx_print_match:n, \siunitx_print_math:n, \siunitx_print_text:n}
130 % \begin{syntax}
131 % \cs{siunitx_print_match:n} \Arg{material}
132 % \cs{siunitx_print_math:n} \Arg{material}
133 % \cs{siunitx_print_text:n} \Arg{material}
134 % \end{syntax}
135 % Prints the \meta{material} as described for \cs{siunitx_print_\dots:n} but
136 % with a fixed text or math mode output. The printing does \emph{not} set
137 % color (which is managed on a |unit|/|number| basis), but otherwise sets
138 % the font as described above. The |match| function uses either the prevailing
139 % math or text mode. No printing takes place if the \cs{material} is entirely
140 % empty after a single expansion.
141 % \end{function}
142 %
143 % \begin{function}{color}
144 % \begin{syntax}
145 % |color| = \meta{color}
146 % \end{syntax}
147 % Color to apply to printed output: the latter should be a named color
148 % defined for use with \cs{textcolor}. The standard setting is empty (no
149 % color).
150 % \end{function}
151 %
152 % \begin{function}{mode}
153 % \begin{syntax}
154 % |mode| = |match|\verb"|"|math|\verb"|"|text|
155 % \end{syntax}
156 % Selects which mode (math or text) the output is printed in: a choice
157 % from the options |match|, |math| or |text|. The option |match| matches
158 % the mode prevailing at the point \cs{siunitx_print_\dots:n} is called. The
159 % |math| and |text| options choose the relevant \TeX{} mode for printing.
160 % The standard setting is |math|.
161 % \end{function}
162 %
163 % \begin{function}{number-color}
164 % \begin{syntax}
165 % |number-color| = \meta{color}
166 % \end{syntax}
167 % Color to apply to numbers in output: the latter should be a named color
168 % defined for use with \cs{textcolor}. The standard setting is empty (no
169 % color).
170 % \end{function}
171 %
172 % \begin{function}{number-mode}
173 % \begin{syntax}
174 % |number-mode| = |match|\verb"|"|math|\verb"|"|text|
175 % \end{syntax}
176 % Selects which mode (math or text) the numbers are printed in: a choice
177 % from the options |match|, |math| or |text|. The option |match| matches
178 % the mode prevailing at the point \cs{siunitx_prin_number:n} is called. The
179 % |math| and |text| options choose the relevant \TeX{} mode for printing.
180 % The standard setting is |math|.
181 % \end{function}
182 %
183 % \begin{function}{propagate-math-font}
184 % \begin{syntax}
185 % |propagate-math-font| = |true|\verb"|"|false|
186 % \end{syntax}
187 % Switch to determine if the currently-active math font is applied within
188 % printed output. This is relevant only when \cs{siunitx_print_\dots:n} is
189 % called from within math mode: in text mode there is not active math
190 % font. When not active, math mode material will be typeset using
191 % standard math mode fonts without any changes being made to the
192 % supplied argument. The standard setting is |false|.
193 % \end{function}
194 %
195 % \begin{function}{reset-math-version}
196 % \begin{syntax}
197 % |reset-math-version| = |true|\verb"|"|false|
198 % \end{syntax}
199 % Switch to determine whether the active \cs{mathversion} is reset to
200 % |normal| when printing in math mode. Note that math version is typically
201 % used to select \cs{boldmath}, though it is also be used by
202 % \foreign{e.g.}~\pkg{sansmath}. The standard setting is |true|.
203 % \end{function}
204 %
205 % \begin{function}{reset-text-family}
206 % \begin{syntax}
207 % |reset-text-family| = |true|\verb"|"|false|
208 % \end{syntax}
209 % Switch to determine whether the active text family is reset to
210 % \cs{rmfamily} when printing in text mode. The standard setting is |true|.
211 % \end{function}
212 %
213 % \begin{function}{reset-text-series}
214 % \begin{syntax}
215 % |reset-text-series| = |true|\verb"|"|false|
216 % \end{syntax}
217 % Switch to determine whether the active text series is reset to
218 % \cs{mdseries} when printing in text mode. The standard setting is |true|.
219 % \end{function}
220 %
221 % \begin{function}{reset-text-shape}
222 % \begin{syntax}
223 % |reset-text-shape| = |true|\verb"|"|false|
224 % \end{syntax}
225 % Switch to determine whether the active text shape is reset to
226 % \cs{upshape} when printing in text mode. The standard setting is |true|.
227 % \end{function}
228 %
229 % \begin{function}{text-family-to-math}
230 % \begin{syntax}
231 % |text-family-to-math| = |true|\verb"|"|false|
232 % \end{syntax}
233 % Switch to determine if the family of the current text font should be
234 % applied (where possible) to printing in math mode. The standard setting is
235 % |false|.
236 % \end{function}
237 %
238 % \begin{function}{text-font-command}
239 % \begin{syntax}
240 % |text-font-command| = \meta{cmd}
241 % \end{syntax}
242 % Command applied to text during output, inserted after any reset of
243 % font set-up. This can therefore be used to apply non-standard font
244 % set up when printing in text mode. The standard setting is empty.
245 % \end{function}
246 %
247 % \begin{function}{text-series-to-math}
248 % \begin{syntax}
249 % |text-series-to-math| = |true|\verb"|"|false|
250 % \end{syntax}
251 % Switch to determine if the weight of the current text font should be
252 % applied (where possible) to printing in math mode. This is achieved by
253 % setting the \cs{mathversion}, and so will override |reset-math-version|.
254 % The mappings between text and math weight are set . The standard setting is
255 % |false|.
256 % \end{function}
257 %
258 % \begin{function}{text-subscript-command, text-superscript-command}
259 % \begin{syntax}
260 % |text-subscript-command| = \meta{cmd}
261 % |text-superscript-command| = \meta{cmd}
262 % \end{syntax}
263 % Sets the command used when printing material in sub- or superscript
264 % positions in text mode. The standard settings are \cs{textsubscript}
265 % and \cs{textsuperscript}, respectively.
266 % \end{function}
267 %
268 % \begin{function}{unit-color}
269 % \begin{syntax}
270 % |unit-color| = \meta{color}
271 % \end{syntax}
272 % Color to apply to units in output: the latter should be a named color
273 % defined for use with \cs{textcolor}. The standard setting is empty (no
274 % color).
275 % \end{function}
276 %
277 % \begin{function}{unit-mode}
278 % \begin{syntax}
279 % |unit-mode| = |match|\verb"|"|math|\verb"|"|text|
280 % \end{syntax}
281 % Selects which mode (math or text) units are printed in: a choice
282 % from the options |match|, |math| or |text|. The option |match| matches
283 % the mode prevailing at the point \cs{siunitx_print_\dots:n} is called. The
284 % |math| and |text| options choose the relevant \TeX{} mode for printing.
285 % The standard setting is |math|.
286 % \end{function}
287 %
288 % \begin{function}{series-version-mapping}
289 % \begin{syntax}
290 % |series-version-mapping| |/| \meta{weight} = \meta{version}
291 % \end{syntax}
292 % Defines how \pkg{siunitx} maps from text font weight to math font
293 % version. The pre-defined weights are those used as-standard by
294 % \pkg{autoinst}:
295 % \begin{itemize}
296 % \item \texttt{ul}
297 % \item \texttt{el}
298 % \item \texttt{l}
299 % \item \texttt{sl}
300 % \item \texttt{m}
301 % \item \texttt{sb}
302 % \item \texttt{b}
303 % \item \texttt{eb}
304 % \item \texttt{ub}
305 % \end{itemize}
306 % As standard, the \texttt{m} weight maps to \texttt{normal} math version
307 % whilst all of the \texttt{b} weights map to \texttt{bold} and all of the
308 % \texttt{l} weights map to \texttt{light}.
309 % \end{function}
310 %
311 % \end{documentation}
312 %
313 % \begin{implementation}
314 %
315 % \section{\pkg{siunitx-print} implementation}
316 %
317 % Start the \pkg{DocStrip} guards.
318 % \begin{macrocode}
319 %<*package>
320 % \end{macrocode}
321 %
322 % Identify the internal prefix.
323 % \begin{macrocode}
324 %<@@=siunitx_print>
325 % \end{macrocode}
326 %
327 % \subsection{Initial set up}
328 %
329 % \begin{macro}{\@@_ams_text:n}
330 % The printing routines depend on \pkg{amstext} for text mode working.
331 % We also save the definition of \cs{text} here to use it internally, as the
332 % nature of the \emph{document} command may need to vary.
333 % \begin{macrocode}
334 \RequirePackage { amstext }
335 \cs_new_eq:NN \@@_ams_text:n \text
336 % \end{macrocode}
337 % \end{macro}
338 %
339 % Color support is always required; to avoid a potential clash we delay to
340 % the start of the document.
341 % \begin{macrocode}
342 \cs_if_exist:NTF \AddToHook
343 { \AddToHook { begindocument / before } }
344 { \AtBeginDocument }
345 { \RequirePackage { color } }
346 % \end{macrocode}
347 %
348 % Required variants.
349 % \begin{macrocode}
350 \cs_generate_variant:Nn \tl_replace_all:Nnn { NV }
351 % \end{macrocode}
352 %
353 % \begin{variable}{\l_@@_tmp_tl}
354 % Scratch space.
355 % \begin{macrocode}
356 \tl_new:N \l_@@_tmp_tl
357 % \end{macrocode}
358 % \end{variable}
359 %
360 % \subsection{Printing routines}
361 %
362 % \begin{variable}
363 % {
364 % \l_@@_number_color_tl ,
365 % \l_@@_number_mode_str ,
366 % \l_@@_unit_color_tl ,
367 % \l_@@_unit_mode_str ,
368 % \l_@@_math_font_bool ,
369 % \l_@@_math_version_bool ,
370 % \l_@@_math_family_bool ,
371 % \l_@@_text_font_tl ,
372 % \l_@@_text_sub_tl ,
373 % \l_@@_text_super_tl ,
374 % \l_@@_math_series_bool
375 % }
376 % Options which apply to the main formatting routine, and so are not tied
377 % to either symbolic or literal input.
378 % \begin{macrocode}
379 \str_new:N \l_@@_number_mode_str
380 \str_new:N \l_@@_unit_mode_str
381 \keys_define:nn { siunitx }
382 {
383 color .meta:n =
384 { number-color = #1 , unit-color = #1 } ,
385 mode .meta:n =
386 { number-mode = #1 , unit-mode = #1 } ,
387 number-color .tl_set:N =
388 \l_@@_number_color_tl ,
389 number-mode .choices:nn =
390 { match , math , text }
391 {
392 \str_set_eq:NN
393 \l_@@_number_mode_str \l_keys_choice_str
394 } ,
395 propagate-math-font .bool_set:N =
396 \l_@@_math_font_bool ,
397 reset-math-version .bool_set:N =
398 \l_@@_math_version_bool ,
399 reset-text-family .bool_set:N =
400 \l_@@_text_family_bool ,
401 reset-text-series .bool_set:N =
402 \l_@@_text_series_bool ,
403 reset-text-shape .bool_set:N =
404 \l_@@_text_shape_bool ,
405 text-family-to-math .bool_set:N =
406 \l_@@_math_family_bool ,
407 text-font-command .tl_set:N =
408 \l_@@_text_font_tl ,
409 text-subscript-command .tl_set:N =
410 \l_@@_text_sub_tl ,
411 text-superscript-command .tl_set:N =
412 \l_@@_text_super_tl ,
413 text-series-to-math .bool_set:N =
414 \l_@@_math_series_bool ,
415 unit-color .tl_set:N =
416 \l_@@_unit_color_tl ,
417 unit-mode .choices:nn =
418 { match , math , text }
419 {
420 \str_set_eq:NN
421 \l_@@_unit_mode_str \l_keys_choice_str
422 }
423 }
424 % \end{macrocode}
425 % \end{variable}
426 %
427 % \begin{variable}
428 % {
429 % \l_@@_version_ul_tl ,
430 % \l_@@_version_el_tl ,
431 % \l_@@_version_l_tl ,
432 % \l_@@_version_sl_tl ,
433 % \l_@@_version_m_tl ,
434 % \l_@@_version_sb_tl ,
435 % \l_@@_version_b_tl ,
436 % \l_@@_version_eb_tl ,
437 % \l_@@_version_ub_tl
438 % }
439 % One set of \enquote{focussed} options.
440 % \begin{macrocode}
441 \keys_define:nn { siunitx / series-version-mapping }
442 {
443 ul . tl_set:N = \l_@@_version_ul_tl ,
444 el . tl_set:N = \l_@@_version_el_tl ,
445 l . tl_set:N = \l_@@_version_l_tl ,
446 sl . tl_set:N = \l_@@_version_sl_tl ,
447 m . tl_set:N = \l_@@_version_m_tl ,
448 sb . tl_set:N = \l_@@_version_sb_tl ,
449 b . tl_set:N = \l_@@_version_b_tl ,
450 eb . tl_set:N = \l_@@_version_eb_tl ,
451 ub . tl_set:N = \l_@@_version_ub_tl
452 }
453 % \end{macrocode}
454 % \end{variable}
455 %
456 % \begin{macro}
457 % {
458 % \siunitx_print_number:n, \siunitx_print_number:V,
459 % \siunitx_print_number:e,
460 % \siunitx_print_unit:n, \siunitx_print_unit:V, \siunitx_print_unit:e,
461 % \siunitx_print_unit:o
462 % }
463 % \begin{macro}{\@@_aux:nn}
464 % \begin{macro}{\@@_number:n, \@@_unit:n}
465 % The main printing function doesn't actually need to do very much: just set
466 % the color and select the correct sub-function. The \cs{tl_if_empty:oF} is
467 % needed to allow a leading \texttt{1} to be omitted in the case of a unit
468 % starting \cs{per} in cases where we can't simply use an entirely empty
469 % value: see the \pkg{siunitx-compound} submodule.
470 % \begin{macrocode}
471 \cs_new_protected:Npn \siunitx_print_number:n #1
472 { \@@_aux:nn { number } {#1} }
473 \cs_generate_variant:Nn \siunitx_print_number:n { V , e , x }
474 \cs_new_protected:Npn \siunitx_print_unit:n #1
475 { \@@_aux:nn { unit } {#1} }
476 \cs_generate_variant:Nn \siunitx_print_unit:n { V , e , o , x }
477 \cs_new_protected:Npn \@@_aux:nn #1#2
478 {
479 \tl_if_empty:oF {#2}
480 {
481 \tl_if_empty:cTF { l_@@_ #1 _color_tl }
482 { \use:n }
483 { \ExpandArgs { v } \textcolor { l_@@_ #1 _color_tl } }
484 { \use:c { @@ _ #1 :n } {#2} }
485 }
486 }
487 \cs_new_protected:Npn \@@_number:n #1
488 {
489 \bool_if:NTF \l_siunitx_number_parse_bool
490 { \use:c { siunitx_print_ \l_@@_number_mode_str :n } }
491 { \siunitx_print_math:n }
492 {#1}
493 }
494 \cs_new_protected:Npn \@@_unit:n #1
495 { \use:c { siunitx_print_ \l_@@_unit_mode_str :n } {#1} }
496 % \end{macrocode}
497 % \end{macro}
498 % \end{macro}
499 % \end{macro}
500 %
501 % \begin{macro}{\siunitx_print_match:n}
502 % When the \emph{output} mode should match the input, a simple selection of
503 % route can be made.
504 % \begin{macrocode}
505 \cs_new_protected:Npn \siunitx_print_match:n #1
506 {
507 \mode_if_math:TF
508 { \siunitx_print_math:n {#1} }
509 { \siunitx_print_text:n {#1} }
510 }
511 % \end{macrocode}
512 % \end{macro}
513 %
514 % \begin{macro}{\@@_replace_font:N}
515 % A simple auxiliary for \enquote{zapping} the unit font.
516 % \begin{macrocode}
517 \cs_new_protected:Npn \@@_replace_font:N #1
518 {
519 \tl_if_empty:NF \l_siunitx_unit_font_tl
520 {
521 \tl_replace_all:NVn #1
522 \l_siunitx_unit_font_tl
523 { \use:n }
524 }
525 }
526 % \end{macrocode}
527 % \end{macro}
528 %
529 % \begin{variable}
530 % {
531 % \c_@@_series_uc_tl ,
532 % \c_@@_series_ecl_tl ,
533 % \c_@@_series_c_tl ,
534 % \c_@@_series_sc_tl ,
535 % \c_@@_series_sx_tl ,
536 % \c_@@_series_x_tl ,
537 % \c_@@_series_ex_tl ,
538 % \c_@@_series_ux_tl
539 % }
540 % Font widths where the |m| for weight is omitted.
541 % \begin{macrocode}
542 \clist_map_inline:nn { uc , ec , c , sc , sx , x , ex , ux }
543 { \tl_const:cn { c_@@_series_ #1 _tl } { m } }
544 % \end{macrocode}
545 % \end{variable}
546 %
547 % \begin{variable}
548 % {
549 % \c_@@_series_l_tl ,
550 % \c_@@_series_m_tl ,
551 % \c_@@_series_b_tl
552 % }
553 % Font widths with one letter.
554 % \begin{macrocode}
555 \clist_map_inline:nn { l , m , b }
556 { \tl_const:cn { c_@@_series_ #1 _tl } { #1 } }
557 % \end{macrocode}
558 % \end{variable}
559 %
560 % \begin{variable}{\l_@@_math_html_tl}
561 % \begin{macrocode}
562 \tl_new:N \l_@@_math_html_tl
563 % \end{macrocode}
564 % \end{variable}
565 %
566 % \begin{macro}{\siunitx_print_math:n}
567 % \begin{macro}[EXP]{\@@_extract_series:Nw}
568 % \begin{macro}[EXP]{\@@_convert_series:n, \@@_convert_series:v}
569 % \begin{macro}{\@@_math_version:nn, \@@_math_version:Vn}
570 % \begin{macro}
571 % {
572 % \@@_math_auxi:n,
573 % \@@_math_auxii:n,
574 % }
575 % \begin{macro}{\@@_math_text_setup:}
576 % \begin{macro}{\@@_math_replace:Nn}
577 % \begin{macro}
578 % {
579 % \@@_math_auxiii:n ,
580 % \@@_math_auxiii:V ,
581 % \@@_math_auxiv:n ,
582 % \@@_math_auxv:n
583 % }
584 % \begin{macro}{\@@_math_auxvi:Nn, \@@_math_auxvi:NV}
585 % \begin{macro}{\@@_math_auxvii:N}
586 % \begin{macro}{\@@_math_auxviii:w}
587 % \begin{macro}{\@@_math_auxix:Nn}
588 % \begin{macro}{\@@_math_sub:n, \@@_math_super:n, \@@_math_script:n}
589 % \begin{macro}{\@@_math_ensure:n}
590 % The first step in setting in math mode is to check on the math version.
591 % The starting point is the question of whether text series needs to
592 % propagate to math mode: if so, check on the mapping, otherwise check on
593 % the current math version.
594 % \begin{macrocode}
595 \cs_new_protected:Npn \siunitx_print_math:n #1
596 {
597 \bool_lazy_and:nnTF
598 { \l_@@_math_series_bool }
599 { \str_if_eq_p:Vn \math@version { normal } }
600 {
601 \tl_set:Ne \l_@@_tmp_tl
602 { \exp_after:wN \@@_extract_series:Nw \f@series ? \q_stop }
603 \tl_if_empty:NTF \l_@@_tmp_tl
604 { \@@_math_auxi:n {#1} }
605 { \@@_math_version:Vn \l_@@_tmp_tl {#1} }
606 }
607 { \@@_math_auxi:n {#1} }
608 }
609 % \end{macrocode}
610 % Look up the math version from the text series. The weight is omitted
611 % if it is |m| plus there are either one or two letters, so we have a little
612 % work to do. To keep things fast, we use a hash table based lookup rather
613 % than a sequence or property list.
614 % \begin{macrocode}
615 \cs_new:Npn \@@_extract_series:Nw #1#2 ? #3 \q_stop
616 {
617 \cs_if_exist:cTF { c_@@_series_ #1#2 _tl }
618 { \@@_convert_series:v { c_@@_series_ #1#2 _tl } }
619 {
620 \cs_if_exist:cTF { c_@@_series_ #1 _tl }
621 { \@@_convert_series:v { c_@@_series_ #1 _tl } }
622 { \@@_convert_series:n {#1#2} }
623 }
624 }
625 \cs_new:Npn \@@_convert_series:n #1
626 {
627 \tl_if_exist:cTF { l_@@_version_ #1 _tl }
628 { \tl_use:c { l_@@_version_ #1 _tl } }
629 { \msg_expandable_error:nnn { siunitx } { unknown-series } {#1} }
630 }
631 \cs_generate_variant:Nn \@@_convert_series:n { v }
632 \cs_new_protected:Npn \@@_math_auxi:n #1
633 {
634 \bool_if:NTF \l_@@_math_version_bool
635 { \@@_math_version:nn { normal } {#1} }
636 { \@@_math_auxii:n {#1} }
637 }
638 % \end{macrocode}
639 % Any setting which changes the math version can only be set from text mode
640 % (as it applies at the level of a formula). As such, the first test is to
641 % see if that needs to be to check if the math version has to be set: if so,
642 % switch to text mode, sort it out and switch back. That of course means
643 % that in such cases, line breaking will not be possible.
644 % \begin{macrocode}
645 \cs_new_protected:Npn \@@_math_version:nn #1#2
646 {
647 \str_if_eq:VnTF \math@version { #1 }
648 { \@@_math_auxii:n {#2} }
649 {
650 \mode_if_math:TF
651 { \text }
652 { \use:n }
653 {
654 \mathversion {#1}
655 \@@_math_auxii:n {#2}
656 }
657 }
658 }
659 \cs_generate_variant:Nn \@@_math_version:nn { V }
660 % \end{macrocode}
661 % At this point, force math mode then start dealing with setting math font
662 % based on text family. If the text family is roman, life is slightly
663 % different to if it is sanserif or monospaced. In all cases, the outcomes
664 % can be handled using the same routines as for normal math mode treatment.
665 % The test here is on a string basis as |\f@family| and the |\...default|
666 % commands have different |\long| status.
667 % \begin{macrocode}
668 \cs_new_protected:Npn \@@_math_auxii:n #1
669 {
670 \group_begin:
671 \tl_if_head_eq_meaning:nNTF {#1} \sfrac
672 {
673 \cs_set_protected:Npn \text ##1
674 {
675 \cs_set_eq:NN \text \@@_ams_text:n
676 \@@_math_text_setup:
677 ##1
678 }
679 }
680 { \@@_math_text_setup: }
681 \tl_set:Nn \l_@@_tmp_tl {#1}
682 \exp_after:wN \@@_math_replace:Nn
683 \l_@@_math_html_tl
684 \q_recursion_tail { } \q_recursion_stop
685 \@@_math_auxiii:V \l_@@_tmp_tl
686 \group_end:
687 }
688 % \end{macrocode}
689 % Within the math mode argument, there might be a nested \cs{text}, at which
690 % point we need to apply the text-mode support. Within \emph{that}, we need
691 % to reset \cs{text} to normal. There is one wrinkle, which is covered in the
692 % function above: the \cs{sfrac} command from \pkg{xfrac}. In the latter,
693 % there is a \cs{text} applied to the two arguments, and the material that is
694 % passed contains a lot of code: we cannot apply search-and-replace there.
695 % Instead, we delay things by one expansion.
696 % \begin{macrocode}
697 \cs_new_protected:Npn \@@_math_text_setup:
698 {
699 \cs_set_protected:Npn \text ##1
700 {
701 \cs_set_eq:NN \text \@@_ams_text:n
702 \siunitx_print_text:n {##1}
703 }
704 }
705 \cs_new_protected:Npn \@@_math_replace:Nn #1#2
706 {
707 \quark_if_recursion_tail_stop:N #1
708 \tl_replace_all:Nnn \l_@@_tmp_tl {#1} {#2}
709 \@@_math_replace:Nn
710 }
711 \cs_new_protected:Npn \@@_math_auxiii:n #1
712 {
713 \bool_if:NTF \l_@@_math_family_bool
714 {
715 \str_case_e:nnF { \f@family }
716 {
717 { \rmdefault } { \@@_math_auxv:n }
718 { \sfdefault } { \@@_math_auxix:Nn \mathsf }
719 { \ttdefault } { \@@_math_auxix:Nn \mathtt }
720 }
721 { \@@_math_auxiv:n }
722 }
723 { \@@_math_auxiv:n }
724 {#1}
725 }
726 \cs_generate_variant:Nn \@@_math_auxiii:n { V }
727 % \end{macrocode}
728 % Now we deal with the font selection in math mode. There are two possible
729 % cases. First, we are retaining the current math font, and the active one is
730 % \cs{mathsf} or \cs{mathtt}: that needs to be applied to the argument.
731 % Alternatively, if the current font is not retained, ensure that
732 % normal math mode rules are active.
733 % \begin{macrocode}
734 \cs_new_protected:Npn \@@_math_auxiv:n #1
735 {
736 \bool_if:NTF \l_@@_math_font_bool
737 {
738 \@@_math_auxvii:N
739 \mathbf \mathit \mathsf \mathtt
740 \q_recursion_tail \q_recursion_stop
741 }
742 { \@@_math_auxv:n }
743 {#1}
744 }
745 \cs_new_protected:Npn \@@_math_auxv:n #1
746 {
747 \bool_lazy_or:nnTF
748 { \int_compare_p:nNn \fam = { -1 } }
749 { \int_compare_p:nNn \fam = \symoperators }
750 { \@@_math_ensure:n }
751 { \@@_math_auxvi:Nn \mathrm }
752 {#1}
753 }
754 \cs_new_protected:Npn \@@_math_auxvi:Nn #1#2
755 { \@@_math_ensure:n { #1 {#2} } }
756 \cs_generate_variant:Nn \@@_math_auxvi:Nn { NV }
757 \cs_new_protected:Npn \@@_math_auxvii:N #1
758 {
759 \quark_if_recursion_tail_stop_do:Nn #1 { \@@_math_auxv:n }
760 \exp_after:wN \exp_after:wN \exp_after:wN \@@_math_auxviii:w
761 \cs:w \cs_to_str:N #1 \c_space_tl \cs_end:
762 \use@mathgroup ? { -2 } \q_stop #1
763 }
764 \cs_new_protected:Npn \@@_math_auxviii:w #1 \use@mathgroup #2#3 #4 \q_stop #5
765 {
766 \int_compare:nNnTF \fam = {#3}
767 { \use_i_delimit_by_q_recursion_stop:nw { \@@_math_auxix:Nn #5 } }
768 { \@@_math_auxvii:N }
769 }
770 % \end{macrocode}
771 % Search-and-replace fun: deal with any font commands in the argument and
772 % also inside sub/superscripts.
773 % \begin{macrocode}
774 \cs_new_protected:Npe \@@_math_auxix:Nn #1#2
775 {
776 \group_begin:
777 \tl_set:Nn \exp_not:N \l_@@_tmp_tl {#2}
778 \@@_replace_font:N \exp_not:N \l_@@_tmp_tl
779 \tl_replace_all:Nnn \exp_not:N \l_@@_tmp_tl
780 { \char_generate:nn { `\_ } { 8 } }
781 { \exp_not:N \@@_math_sub:n }
782 \tl_replace_all:Nnn \exp_not:N \l_@@_tmp_tl
783 { ^ }
784 { \exp_not:N \@@_math_super:n }
785 \@@_math_auxvi:NV #1 \exp_not:N \l_@@_tmp_tl
786 \group_end:
787 }
788 \cs_new_protected:Npe \@@_math_sub:n #1
789 {
790 \char_generate:nn { `\_ } { 8 }
791 { \exp_not:N \@@_math_script:n {#1} }
792 }
793 \cs_new_protected:Npn \@@_math_super:n #1
794 { ^ { \@@_math_script:n {#1} } }
795 \cs_new_protected:Npn \@@_math_script:n #1
796 {
797 \group_begin:
798 \tl_set:Nn \l_@@_tmp_tl {#1}
799 \@@_replace_font:N \l_@@_tmp_tl
800 \tl_use:N \l_@@_tmp_tl
801 \group_end:
802 }
803 \cs_new_protected:Npn \@@_math_ensure:n #1
804 {
805 \tl_if_blank:nF {#1}
806 {
807 \mode_if_math:TF
808 {#1}
809 { $ #1 $ }
810 }
811 }
812 % \end{macrocode}
813 % For \pkg{tex4ht}, we need to have category code $12$ |^| tokens in math
814 % mode.
815 % \begin{macrocode}
816 \AtBeginDocument
817 {
818 \@ifpackageloaded { tex4ht }
819 { \tl_set:Ne \l_@@_math_html_tl { ^ { \token_to_str:N ^ } } }
820 { }
821 }
822 % \end{macrocode}
823 % \end{macro}
824 % \end{macro}
825 % \end{macro}
826 % \end{macro}
827 % \end{macro}
828 % \end{macro}
829 % \end{macro}
830 % \end{macro}
831 % \end{macro}
832 % \end{macro}
833 % \end{macro}
834 % \end{macro}
835 % \end{macro}
836 % \end{macro}
837 %
838 % \begin{macro}{\siunitx_print_text:n}
839 % \begin{macro}{\@@_text_replace:n, \@@_text_replace_aux:n}
840 % \begin{macro}{\@@_text_replace_font:N}
841 % \begin{macro}[EXP]{\@@_text_replace_first:N}
842 % \begin{macro}{\@@_text_replace:N}
843 % \begin{macro}{\@@_text_replace:NNn}
844 % \begin{macro}{\@@_text_replace:Nnnn}
845 % \begin{macro}{\@@_text_replace_frac:n}
846 % \begin{macro}{\@@_text_sub:n, \@@_text_super:n}
847 % \begin{macro}{\@@_text_scripts:nnN}
848 % \begin{macro}{\@@_text_scripts:}
849 % \begin{macro}{\@@_text_scripts_one:NnN}
850 % \begin{macro}{\@@_text_scripts_two:NnNn}
851 % \begin{macro}{\@@_text_scripts_two:nn}
852 % \begin{macro}{\@@_text_scripts_two:n}
853 % \begin{macro}{\@@_text_fraction:Nnn}
854 % Typesetting in text mode is easy in font control terms but more tricky
855 % in the manipulation of the input. The easy part comes first.
856 % \begin{macrocode}
857 \cs_new_protected:Npn \siunitx_print_text:n #1
858 {
859 \text
860 {
861 \bool_if:NT \l_@@_text_family_bool
862 { \fontfamily { \familydefault } }
863 \bool_if:NT \l_@@_text_series_bool
864 { \fontseries { \seriesdefault } }
865 \bool_if:NT \l_@@_text_shape_bool
866 { \fontshape { \shapedefault } }
867 \bool_lazy_any:nT
868 {
869 { \l_@@_text_family_bool }
870 { \l_@@_text_series_bool }
871 { \l_@@_text_shape_bool }
872 }
873 { \selectfont }
874 \tl_use:N \l_@@_text_font_tl
875 \exp_last_unbraced:Nno \tl_if_head_eq_meaning:nNTF
876 {#1} \l_siunitx_unit_fraction_tl
877 { \@@_text_fraction:Nnn #1 }
878 { \@@_text_replace:n {#1} }
879 }
880 }
881 % \end{macrocode}
882 % To get math mode material to print in text mode, various search-and-replace
883 % steps are needed. We use \tn{protected@edef} to deal with commands, partly
884 % for speed and partly as it deals with anything nested inside \cs{mathord}.
885 % On the other hand, the unit font command has to be handled at exactly one
886 % level, so that it does not affect anything inside for example
887 % \cs{ensuremath}. That means preventing any change inside the
888 % \tn{protected@edef}, hence the \cs{cs_set_eq:NN} to \cs{scan_stop:}
889 % (for \LaTeXe{} robust commands).
890 % \begin{macrocode}
891 \cs_new_protected:Npn \@@_text_replace:n #1
892 {
893 \group_begin:
894 \tl_if_head_eq_meaning:nNTF {#1} \mathchoice
895 { \@@_text_replace:Nnnnn #1 }
896 {
897 \tl_set:Nn \l_@@_tmp_tl {#1}
898 \tl_if_empty:NF \l_@@_tmp_tl
899 {
900 \group_begin:
901 \@@_text_replace_font:N \l_@@_tmp_tl
902 \cs_set:Npn \mathord ##1
903 { \@@_text_replace_first:N ##1 }
904 \tl_if_empty:NF \l_siunitx_unit_font_tl
905 {
906 \exp_after:wN
907 \cs_set_eq:NN \l_siunitx_unit_font_tl \scan_stop:
908 }
909 \tl_map_inline:nn
910 { \mp \approx \sim \ge \le \geq \leq \gg \ll \angle }
911 {
912 \cs_set:Npn ##1
913 { \exp_not:N \ensuremath { \exp_not:N ##1 } }
914 }
915 \cs_set:Npn \sqrt ##1
916 {
917 \exp_not:N \ensuremath
918 {
919 \exp_not:N \sqrt
920 { \exp_not:N \text { \@@_text_replace:n {##1} } }
921 }
922 }
923 \protected@edef \l_@@_tmp_tl
924 { \exp_after:wN \@@_text_replace_first:N \l_@@_tmp_tl }
925 \exp_args:NNNV \group_end:
926 \tl_set:Nn \l_@@_tmp_tl \l_@@_tmp_tl
927 \@@_text_replace:N \l_@@_tmp_tl
928 \@@_text_replace_aux:n { \tl_use:N \l_@@_tmp_tl }
929 }
930 }
931 \group_end:
932 }
933 \cs_new_protected:Npn \@@_text_replace_aux:n #1 {#1}
934 \cs_new_protected:Npn \@@_text_replace_font:N #1
935 {
936 \tl_if_empty:NF \l_siunitx_unit_font_tl
937 {
938 \tl_replace_all:NVn #1
939 \l_siunitx_unit_font_tl { \use:n }
940 }
941 }
942 \cs_new:Npn \@@_text_replace_first:N #1
943 {
944 \str_case:nnF {#1}
945 {
946 { \cdot } { \exp_not:N \textperiodcentered }
947 { \pm } { \exp_not:N \textpm }
948 { \times } { \exp_not:N \texttimes }
949 }
950 {#1}
951 }
952 \cs_new_protected:Npe \@@_text_replace:N #1
953 {
954 \exp_not:N \@@_text_replace:NNn #1
955 -
956 { \exp_not:N \textminus }
957 \char_generate:nn { `\_ } { 8 }
958 { \exp_not:N \@@_text_sub:n }
959 ^
960 { \exp_not:N \@@_text_super:n }
961 \exp_not:N \cdot
962 { \exp_not:N \: \exp_not:N \textperiodcentered \exp_not:N \: }
963 \exp_not:N \pm
964 { \exp_not:N \: \exp_not:N \textpm \exp_not:N \: }
965 \exp_not:N \times
966 { \exp_not:N \: \exp_not:N \texttimes \exp_not:N \: }
967 \exp_not:N \q_recursion_tail
968 { ? }
969 \exp_not:N \q_recursion_stop
970 \@@_text_replace_font:N #1
971 }
972 \cs_new_protected:Npn \@@_text_replace:NNn #1#2#3
973 {
974 \quark_if_recursion_tail_stop:N #2
975 \tl_replace_all:Nnn #1 {#2} {#3}
976 \@@_text_replace:NNn #1
977 }
978 \cs_new_protected:Npn \@@_text_replace:Nnnnn #1#2#3#4#5
979 {
980 \ensuremath
981 {
982 \mathchoice
983 { \@@_print_replace_frac:n {#2} }
984 { \@@_print_replace_frac:n {#3} }
985 { \@@_print_replace_frac:n {#4} }
986 { \@@_print_replace_frac:n {#5} }
987 }
988 }
989 % \end{macrocode}
990 % Almost the same as the lead-off but here we need to deal with re-inserting
991 % a text mode shift.
992 % \begin{macrocode}
993 \cs_new_protected:Npn \@@_print_replace_frac:n #1
994 {
995 \exp_last_unbraced:Nno \tl_if_head_eq_meaning:nNTF
996 {#1} \l_siunitx_unit_fraction_tl
997 { \@@_text_fraction:Nnn #1 }
998 { \mbox { \@@_text_replace:n {#1} } }
999 }
1000 % \end{macrocode}
1001 % When the \pkg{bidi} package is loaded, we need to make sure
1002 % that \cs{text} is doing the correct thing.
1003 % \begin{macrocode}
1004 \sys_if_engine_xetex:T
1005 {
1006 \AtBeginDocument
1007 {
1008 \@ifpackageloaded { bidi }
1009 {
1010 \cs_set_protected:Npn \@@_text_replace_aux:n #1 { \LRE {#1} }
1011 }
1012 { }
1013 }
1014 }
1015 % \end{macrocode}
1016 % Sub- and superscripts can be in any order in the source. The first step
1017 % of handling them is therefore to do a look-ahead to work out whether
1018 % only one or both are present.
1019 % \begin{macrocode}
1020 \cs_new_protected:Npn \@@_text_sub:n #1
1021 {
1022 \@@_text_scripts:VnN
1023 \l_@@_text_sub_tl {#1} \@@_text_super:n
1024 }
1025 \cs_new_protected:Npn \@@_text_super:n #1
1026 {
1027 \@@_text_scripts:VnN
1028 \l_@@_text_super_tl {#1} \@@_text_sub:n
1029 }
1030 \cs_new_protected:Npn \@@_text_scripts:nnN #1#2#3
1031 {
1032 \cs_set_protected:Npn \@@_text_scripts:
1033 {
1034 \if_meaning:w \l_peek_token #3
1035 \exp_after:wN \@@_text_scripts_two:NnNn
1036 \else:
1037 \exp_after:wN \@@_text_scripts_one:Nn
1038 \fi:
1039 #1 {#2}
1040 }
1041 \peek_after:Nw \@@_text_scripts:
1042 }
1043 \cs_generate_variant:Nn \@@_text_scripts:nnN { V }
1044 \cs_new_protected:Npn \@@_text_scripts: { }
1045 % \end{macrocode}
1046 % In the simple case of one script item, we have to do a search-and-replace
1047 % to deal with anything inside the argument.
1048 % \begin{macrocode}
1049 \cs_new_protected:Npn \@@_text_scripts_one:Nn #1#2
1050 {
1051 \group_begin:
1052 \tl_set:Nn \l_@@_tmp_tl {#2}
1053 \@@_text_replace:N \l_@@_tmp_tl
1054 \exp_args:NNV \group_end:
1055 #1 \l_@@_tmp_tl
1056 }
1057 % \end{macrocode}
1058 % For the two scripts case, we cannot use |\textsubscript|/|\textsuperscript|
1059 % as they don't stack directly. Instead, we sort out the ordering then use
1060 % an implementation for both parts that is the same as the kernel text
1061 % scripts.
1062 % \begin{macrocode}
1063 \cs_new_protected:Npn \@@_text_scripts_two:NnNn #1#2#3#4
1064 {
1065 \cs_if_eq:NNTF #1 \textsubscript
1066 { \@@_text_scripts_two:nn {#4} {#2} }
1067 { \@@_text_scripts_two:nn {#2} {#4} }
1068 }
1069 \cs_new_protected:Npe \@@_text_scripts_two:nn #1#2
1070 {
1071 \group_begin:
1072 \exp_not:N \m@th
1073 \exp_not:N \ensuremath
1074 {
1075 ^ { \exp_not:N \@@_text_scripts_two:n {#1} }
1076 \char_generate:nn { `\_ } { 8 }
1077 { \exp_not:N \@@_text_scripts_two:n {#2} }
1078 }
1079 \group_end:
1080 }
1081 \cs_new_protected:Npn \@@_text_scripts_two:n #1
1082 {
1083 \mbox
1084 {
1085 \fontsize \sf@size \z@ \selectfont
1086 \@@_text_scripts_one:Nn \use:n {#1}
1087 }
1088 }
1089 % \end{macrocode}
1090 % Fraction commands are always math mode, so we have to go back and forth:
1091 % this is done after general font setting for performance reasons.
1092 % \begin{macrocode}
1093 \cs_new_protected:Npn \@@_text_fraction:Nnn #1#2#3
1094 {
1095 \ensuremath
1096 {
1097 #1
1098 { \mbox { \@@_text_replace:n {#2} } }
1099 { \mbox { \@@_text_replace:n {#3} } }
1100 }
1101 }
1102 % \end{macrocode}
1103 % \end{macro}
1104 % \end{macro}
1105 % \end{macro}
1106 % \end{macro}
1107 % \end{macro}
1108 % \end{macro}
1109 % \end{macro}
1110 % \end{macro}
1111 % \end{macro}
1112 % \end{macro}
1113 % \end{macro}
1114 % \end{macro}
1115 % \end{macro}
1116 % \end{macro}
1117 % \end{macro}
1118 % \end{macro}
1119 %
1120 % \subsection{Messages}
1121 %
1122 % \begin{macrocode}
1123 \msg_new:nnn { siunitx } { unknown-series }
1124 { Unknown~font~series~"#1"! }
1125 % \end{macrocode}
1126 %
1127 % \subsection{Standard settings for module options}
1128 %
1129 % Some of these follow naturally from the point of definition
1130 % (\foreign{e.g.}~boolean variables are always |false| to begin with),
1131 % but for clarity everything is set here.
1132 % \begin{macrocode}
1133 \keys_set:nn { siunitx }
1134 {
1135 color = ,
1136 mode = math ,
1137 number-color = ,
1138 number-mode = math ,
1139 propagate-math-font = false ,
1140 reset-math-version = true ,
1141 reset-text-shape = true ,
1142 reset-text-series = true ,
1143 reset-text-family = true ,
1144 text-family-to-math = false ,
1145 text-font-command = ,
1146 text-subscript-command = \textsubscript ,
1147 text-superscript-command = \textsuperscript ,
1148 text-series-to-math = false ,
1149 unit-color = ,
1150 unit-mode = math
1151 }
1152 % \end{macrocode}
1153 %
1154 % These are separate as they all fall inside the same key.
1155 % \begin{macrocode}
1156 \keys_set:nn { siunitx / series-version-mapping }
1157 {
1158 ul = light ,
1159 el = light ,
1160 l = light ,
1161 sl = light ,
1162 m = normal ,
1163 sb = bold ,
1164 b = bold ,
1165 eb = bold ,
1166 ub = bold
1167 }
1168 % \end{macrocode}
1169 %
1170 % \begin{macrocode}
1171 %</package>
1172 % \end{macrocode}
1173 %
1174 % \end{implementation}
1175 %
1176 % \PrintIndex