Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % File: siunitx-complex.dtx Copyright (C) 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-complex} -- Complex numbers^^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{Formatting complex values}
64 %
65 % This submodule is concerned with formatting complex numbers. It augments the
66 % standard functions \cs{siunitx_number_format:nN} and \cs{siunitx_quantity:nn}
67 % by allowing parsing of numbers with a complex part. There are no additional
68 % assumptions concerning \LaTeXe{} commands in the submodule beyond those in the
69 % core number and unit submodules.
70 %
71 % \begin{function}{\siunitx_complex_number:n, \siunitx_complex_number:e}
72 % \begin{syntax}
73 % \cs{siunitx_complex_number:n} \Arg{number}
74 % \end{syntax}
75 % Parses the \meta{number} and splits into real and complex parts, which are
76 % then formatted as described for \cs{siunitx_number_format:nN}. The results
77 % are combined and printed using the standard functions in the module. If
78 % the setting |complex-mode| is set to \meta{polar}, the input is
79 % parsed, converted to polar form and then passed to
80 % \cs{siunitx_complex_number:nn}. This parsing requires that the complex root
81 % is given as \texttt{i} at the \emph{end} of the value.
82 % \end{function}
83 %
84 % \begin{function}{\siunitx_complex_number:nn}
85 % \begin{syntax}
86 % \cs{siunitx_complex_number:nn} \Arg{magnitude} \Arg{angle}
87 % \end{syntax}
88 % Parses the \meta{magnitude} and \meta{angle} and then formats each as
89 % described for \cs{siunitx_number_format:nN}. The two are separated by the
90 % angle symbol, which is treated as a numerical part. If
91 % |complex-angle-unit| is set to |degrees| then the unit symbol is
92 % added: this is printed as a unit in the usual way. If
93 % the setting |complex-mode| is set to \meta{cartesian}, the input is
94 % parsed, converted to Cartesian form and then passed to
95 % \cs{siunitx_complex_number:n}.
96 % \end{function}
97 %
98 % \begin{function}
99 % {
100 % \siunitx_complex_quantity:nn ,
101 % \siunitx_complex_quantity:en ,
102 % \siunitx_complex_quantity:nnn
103 % }
104 % \begin{syntax}
105 % \cs{siunitx_complex_quantity:nn} \Arg{number} \Arg{units}
106 % \cs{siunitx_complex_quantity:nnn} \Arg{magnitude} \Arg{angle} \Arg{units}
107 % \end{syntax}
108 % These functions treat their numerical argument(s) as described
109 % for the corresponding \texttt{number} functions. They then typeset the
110 % entire numerical part and the unit as described for
111 % \cs{siunitx_quantity:nn}.
112 % \end{function}
113 %
114 % \begin{function}{complex-angle-unit}
115 % \begin{syntax}
116 % |complex-angle-unit| = |degrees|\verb"|"|radians|
117 % \end{syntax}
118 % Sets how the unit for polar complex numbers is treated. This setting
119 % is used to determine how polar \emph{input} is interpreted, how
120 % conversion to polar form works and how output in polar form is typeset.
121 % The standard setting is |degrees|.
122 % \end{function}
123 %
124 % \begin{function}{complex-mode}
125 % \begin{syntax}
126 % |complex-mode| = |cartesian|\verb"|"|input|\verb"|"|polar|
127 % \end{syntax}
128 % Selects how complex values are formatted: a choice from the options
129 % |cartesian|, |input| and |polar|. The option |cartesian| means that
130 % complex values will always be typeset in cartesian ($x + y\mathrm{i}$)
131 % format, whilst |polar| means that complex are typeset as a magnitude
132 % and angle. Finally, |input| setting means that the input format
133 % (\foreign{i.e.}~difference between \cs{siunitx_complex_number:n} and
134 % \cs{siunitx_complex_number:nn}) is maintained. The standard setting is
135 % |input|.
136 % \end{function}
137 %
138 % \begin{function}{complex-phase-command}
139 % \begin{syntax}
140 % |complex-phase-command| = \meta{cmd}
141 % \end{syntax}
142 % Sets the command used during output of the phase of a complex number in
143 % polar form. The standard setting is |\angle|.
144 % \end{function}
145 %
146 % \begin{function}{complex-root-position}
147 % \begin{syntax}
148 % |complex-root-position| = |after-number|\verb"|"|before-number|
149 % \end{syntax}
150 % Choice which determines where the complex root symbol is printed relative
151 % to the numbers. The standard setting is |after-number|.
152 % \end{function}
153 %
154 % \begin{function}{complex-symbol-degree}
155 % \begin{syntax}
156 % |complex-symbol-degree| = \meta{symbol}
157 % \end{syntax}
158 % Sets the symbol used for polar degrees.
159 % \end{function}
160 %
161 % \begin{function}{input-complex-root}
162 % \begin{syntax}
163 % |input-complex-root| = \meta{tokens}
164 % \end{syntax}
165 % The token(s) considered as complexes roots for number parsing.
166 % The standard setting is |ij|.
167 % \end{function}
168 %
169 % \begin{function}{output-complex-root}
170 % \begin{syntax}
171 % |output-complex-root| = \meta{tokens}
172 % \end{syntax}
173 % The token(s) used to show the complex root in output. The standard setting
174 % is |\mathrm{i}|.
175 % \end{function}
176 %
177 % \begin{function}{print-complex-unity}
178 % \begin{syntax}
179 % |print-complex-unity| = |true|\verb"|"|false|
180 % \end{syntax}
181 % Switch to determine if the number \num{1} is printed for a complex
182 % part which is exactly unity.
183 % \end{function}
184 %
185 % \end{documentation}
186 %
187 % \begin{implementation}
188 %
189 % Start the \pkg{DocStrip} guards.
190 % \begin{macrocode}
191 %<*package>
192 % \end{macrocode}
193 %
194 % \section{\pkg{siunitx-complex} implementation}
195 %
196 % Identify the internal prefix.
197 % \begin{macrocode}
198 %<@@=siunitx_complex>
199 % \end{macrocode}
200 %
201 % \subsection{General setup}
202 %
203 % \begin{variable}{\l_@@_tmp_fp, \l_@@_tmp_tl}
204 % \begin{macrocode}
205 \fp_new:N \l_@@_tmp_fp
206 \tl_new:N \l_@@_tmp_tl
207 % \end{macrocode}
208 % \end{variable}
209 %
210 % \begin{variable}{\l_@@_input_tl}
211 % The numerical input exactly as given by the user.
212 % \begin{macrocode}
213 \tl_new:N \l_@@_input_tl
214 % \end{macrocode}
215 % \end{variable}
216 %
217 % \begin{variable}{\l_@@_comparator_tl}
218 % A comparator, if found, is held here.
219 % \begin{macrocode}
220 \tl_new:N \l_@@_comparator_tl
221 % \end{macrocode}
222 % \end{variable}
223 %
224 % \begin{variable}{\l_@@_exp_tl}
225 % The exponent part of a parsed number.
226 % \begin{macrocode}
227 \tl_new:N \l_@@_exp_tl
228 % \end{macrocode}
229 % \end{variable}
230 %
231 % \begin{variable}{\l_@@_real_tl, \l_@@_img_tl}
232 % The real and imaginary parts of cartesian form, respectively.
233 % \begin{macrocode}
234 \tl_new:N \l_@@_real_tl
235 \tl_new:N \l_@@_img_tl
236 % \end{macrocode}
237 % \end{variable}
238 %
239 % \begin{variable}{\l_@@_mag_tl, \l_@@_angle_tl}
240 % The magnitude and angle of polar form, respectively.
241 % \begin{macrocode}
242 \tl_new:N \l_@@_mag_tl
243 \tl_new:N \l_@@_angle_tl
244 % \end{macrocode}
245 % \end{variable}
246 %
247 % \begin{variable}{\l_@@_join_tl, \l_@@_sign_tl}
248 % Staging posts for a joining and leading sign, respectively.
249 % \begin{macrocode}
250 \tl_new:N \l_@@_join_tl
251 \tl_new:N \l_@@_sign_tl
252 % \end{macrocode}
253 % \end{variable}
254 %
255 % \begin{macro}{\@@_suppress_negative:, \@@_suppress_non_polar:}
256 % Pre-compilation of keys to suppress special treatment of negative values
257 % and input not suitable for polar form.
258 % \begin{macrocode}
259 \keys_precompile:nnN
260 { siunitx }
261 {
262 bracket-negative-numbers = false ,
263 negative-color =
264 }
265 \l_@@_tmp_tl
266 \cs_new_protected:Npe \@@_suppress_negative:
267 { \exp_not:V \l_@@_tmp_tl }
268 \keys_precompile:nnN
269 { siunitx }
270 {
271 input-comparators = ,
272 input-exponent-markers = ,
273 input-open-uncertainty = ,
274 input-close-uncertainty =
275 }
276 \l_@@_tmp_tl
277 \cs_new_protected:Npe \@@_suppress_non_polar:
278 { \exp_not:V \l_@@_tmp_tl }
279 % \end{macrocode}
280 % \end{macro}
281 %
282 % \begin{variable}
283 % {
284 % \l_@@_root_after_bool ,
285 % \l_@@_force_cartesian_bool ,
286 % \l_@@_force_polar_bool ,
287 % \l_@@_phase_tl ,
288 % \l_@@_polar_degree_bool ,
289 % \l_@@_symbol_degree_tl ,
290 % \l_@@_input_root_tl ,
291 % \l_@@_output_root_tl
292 % }
293 % \begin{macrocode}
294 \bool_new:N \l_@@_root_after_bool
295 \bool_new:N \l_@@_force_cartesian_bool
296 \bool_new:N \l_@@_force_polar_bool
297 \bool_new:N \l_@@_polar_degree_bool
298 \keys_define:nn { siunitx }
299 {
300 complex-mode .choice: ,
301 complex-mode / cartesian .code:n =
302 {
303 \bool_set_true:N \l_@@_force_cartesian_bool
304 \bool_set_false:N \l_@@_force_polar_bool
305 } ,
306 complex-mode / polar .code:n =
307 {
308 \bool_set_false:N \l_@@_force_cartesian_bool
309 \bool_set_true:N \l_@@_force_polar_bool
310 } ,
311 complex-mode / input .code:n =
312 {
313 \bool_set_false:N \l_@@_force_cartesian_bool
314 \bool_set_false:N \l_@@_force_polar_bool
315 } ,
316 complex-angle-unit .choice: ,
317 complex-angle-unit / degrees .code:n =
318 { \bool_set_true:N \l_@@_polar_degree_bool } ,
319 complex-angle-unit / radians .code:n =
320 { \bool_set_false:N \l_@@_polar_degree_bool } ,
321 complex-phase-command .tl_set:N =
322 \l_@@_phase_tl ,
323 complex-root-position .choice: ,
324 complex-root-position / after-number .code:n =
325 { \bool_set_true:N \l_@@_root_after_bool } ,
326 complex-root-position / before-number .code:n =
327 { \bool_set_false:N \l_@@_root_after_bool } ,
328 complex-symbol-degree .tl_set:N =
329 \l_@@_symbol_degree_tl ,
330 input-complex-root .tl_set:N =
331 \l_@@_input_root_tl ,
332 output-complex-root .tl_set:N =
333 \l_@@_output_root_tl ,
334 print-complex-unity .bool_set:N =
335 \l_@@_print_unity_bool
336 }
337 % \end{macrocode}
338 % \end{variable}
339 %
340 % \subsection{Parsing}
341 %
342 % \begin{macro}{\@@_expand:nN}
343 % A small auxiliary to expand input and replace symbols: we need to do this
344 % quite early so it is separated out. The storage is hard-coded as this is
345 % low-level stuff.
346 % \begin{macrocode}
347 \cs_new_protected:Npn \@@_expand:n #1
348 {
349 \protected@edef \l_@@_arg_tl {#1}
350 \tl_set_eq:NN \l_@@_input_tl \l_@@_arg_tl
351 \siunitx_number_normalize_symbols:N \l_@@_arg_tl
352 }
353 % \end{macrocode}
354 % \end{macro}
355 %
356 % \begin{macro}{\@@_parse:n, \@@_parse:V}
357 % \begin{macro}{\@@_parse_clear:}
358 % Parsing for complex numbers needs some of the same approaches as the
359 % general parser. However, as the aim here is to do only enough to split
360 % the real and imaginary parts before handing off the usual code,
361 % it's not a full repeat. Instead, we shortcut where we can. The |clear|
362 % function here is not only there to make this function shorter: it
363 % also allows a single way to zap any stored data if a parse error occurs.
364 % \begin{macrocode}
365 \cs_new_protected:Npn \@@_parse:n #1
366 {
367 \@@_parse_clear:
368 \tl_if_empty:NF \l_@@_arg_tl
369 { \@@_parse_comparator: }
370 \@@_parse_check:
371 }
372 \cs_generate_variant:Nn \@@_parse:n { V }
373 \cs_new_protected:Npn \@@_parse_clear:
374 {
375 \tl_clear:N \l_@@_real_tl
376 \tl_clear:N \l_@@_img_tl
377 \tl_clear:N \l_@@_exp_tl
378 \tl_clear:N \l_@@_sign_tl
379 \tl_clear:N \l_@@_join_tl
380 }
381 % \end{macrocode}
382 % \end{macro}
383 % \end{macro}
384 %
385 % \begin{macro}{\@@_parse_check:}
386 % \begin{macro}{\@@_parse_check:N}
387 % Now we tidy up and do the main work: passing to the standard formatter for
388 % final parsing.
389 % \begin{macrocode}
390 \cs_new_protected:Npn \@@_parse_check:
391 {
392 \tl_if_empty:NTF \l_@@_img_tl
393 { \@@_parse_check:N \l_@@_real_tl }
394 {
395 \tl_if_empty:NTF \l_@@_real_tl
396 { \@@_parse_check:N \l_@@_img_tl }
397 {
398 \@@_parse_check:N \l_@@_real_tl
399 \tl_set_eq:NN \l_@@_sign_tl \l_@@_join_tl
400 \@@_parse_check:N \l_@@_img_tl
401 }
402 }
403 }
404 \cs_new_protected:Npn \@@_parse_check:N #1
405 {
406 \tl_set:Ne #1
407 {
408 \exp_not:V \l_@@_comparator_tl
409 \exp_not:V \l_@@_sign_tl
410 \exp_not:V #1
411 \exp_not:V \l_@@_exp_tl
412 }
413 \tl_clear:N \l_@@_comparator_tl
414 \tl_clear:N \l_@@_sign_tl
415 \siunitx_number_parse:VN #1 #1
416 }
417 % \end{macrocode}
418 % \end{macro}
419 % \end{macro}
420 % \end{macro}
421 %
422 % \begin{macro}{\@@_parse_comparator:}
423 % \begin{macro}{\@@_parse_comparator_aux:Nw}
424 % The first step is to extract any comparator: this is the same as
425 % for a full number parse.
426 % \begin{macrocode}
427 \cs_new_protected:Npn \@@_parse_comparator:
428 {
429 \exp_after:wN \@@_parse_comparator_aux:Nw
430 \l_@@_arg_tl \q_stop
431 }
432 \cs_new_protected:Npn \@@_parse_comparator_aux:Nw #1#2 \q_stop
433 {
434 \tl_if_in:NnTF \l_siunitx_number_input_comparator_tl {#1}
435 {
436 \tl_set:Nn \l_@@_comparator_tl {#1}
437 \tl_set:Nn \l_@@_arg_tl {#2}
438 }
439 { \tl_clear:N \l_@@_comparator_tl }
440 \tl_if_empty:NF \l_@@_arg_tl
441 { \@@_parse_sign: }
442 }
443 % \end{macrocode}
444 % \end{macro}
445 % \end{macro}
446 %
447 % \begin{macro}{\@@_parse_exponent:}
448 % \begin{macro}{\@@_parse_exponent_auxi:w}
449 % \begin{macro}{\@@_parse_exponent_auxii:nn}
450 % An exponent part of a number has to come at the end and can only occur
451 % once. Thus it is relatively easy to parse. The code here is a simplified
452 % version of that in \pkg{siunitx-number}: we only need to find \emph{some}
453 % exponent, not check on the detail. Notice that we need to retain the
454 % exponent marker here: that is done using the short-lived temporary
455 % variable.
456 % \begin{macrocode}
457 \cs_new_protected:Npn \@@_parse_exponent:
458 {
459 \tl_if_empty:NTF \l_siunitx_number_input_exponent_tl
460 { \@@_parse_root: }
461 {
462 \tl_set:Ne \l_@@_tmp_tl
463 { \tl_head:V \l_siunitx_number_input_exponent_tl }
464 \tl_map_inline:Nn \l_siunitx_number_input_exponent_tl
465 {
466 \tl_replace_all:NnV \l_@@_arg_tl
467 {##1} \l_@@_tmp_tl
468 }
469 \use:e
470 {
471 \cs_set_protected:Npn
472 \exp_not:N \@@_parse_exponent_auxi:w
473 ##1 \exp_not:V \l_@@_tmp_tl
474 ##2 \exp_not:V \l_@@_tmp_tl
475 ##3 \exp_not:N \q_stop
476 }
477 { \@@_parse_exponent_auxii:nn {##1} {##2} }
478 \use:e
479 {
480 \@@_parse_exponent_auxi:w
481 \exp_not:V \l_@@_arg_tl
482 \exp_not:V \l_@@_tmp_tl \exp_not:N \q_nil
483 \exp_not:V \l_@@_tmp_tl \exp_not:N \q_stop
484 }
485 }
486 }
487 \cs_new_protected:Npn \@@_parse_exponent_auxi:w { }
488 \cs_new_protected:Npn \@@_parse_exponent_auxii:nn #1#2
489 {
490 \quark_if_nil:nF {#2}
491 {
492 \tl_set:Nn \l_@@_arg_tl {#1}
493 \tl_set:Ne \l_@@_exp_tl
494 { \exp_not:V \l_@@_tmp_tl \exp_not:n {#2} }
495 }
496 \@@_parse_root:
497 }
498 % \end{macrocode}
499 % \end{macro}
500 % \end{macro}
501 % \end{macro}
502 %
503 % \begin{macro}{\@@_parse_root:}
504 % \begin{macro}{\@@_parse_root_auxi:w}
505 % \begin{macro}{\@@_parse_root_auxii:nnnN}
506 % Splitting at the complex root is much like splitting the exponent.
507 % After dealing with the case where there is no complex root allowed,
508 % use the first possible symbol to do the work.
509 % \begin{macrocode}
510 \cs_new_protected:Npn \@@_parse_root:
511 {
512 \tl_if_empty:NTF \l_@@_input_root_tl
513 { \tl_set_eq:NN \l_@@_real_tl \l_@@_arg_tl }
514 {
515 \tl_set:Ne \l_@@_tmp_tl
516 { \tl_head:V \l_@@_input_root_tl }
517 \tl_map_inline:Nn \l_@@_input_root_tl
518 {
519 \tl_replace_all:NnV \l_@@_arg_tl
520 {##1} \l_@@_tmp_tl
521 }
522 \use:e
523 {
524 \cs_set_protected:Npe
525 \exp_not:N \@@_parse_root_auxi:w
526 ##1 \exp_not:V \l_@@_tmp_tl
527 ##2 \exp_not:V \l_@@_tmp_tl
528 ##3 \exp_not:N \q_stop
529 }
530 {
531 \@@_parse_root_auxii:nnnN {##1} {##2} {##3}
532 \exp_not:V \l_@@_tmp_tl
533 }
534 \use:e
535 {
536 \@@_parse_root_auxi:w
537 \exp_not:V \l_@@_arg_tl
538 \exp_not:V \l_@@_tmp_tl \exp_not:N \q_nil
539 \exp_not:V \l_@@_tmp_tl \exp_not:N \q_stop
540 }
541 }
542 }
543 \cs_new_protected:Npn \@@_parse_root_auxi:w { }
544 % \end{macrocode}
545 % This is where the business end lies. We have five possibilities:
546 % \begin{itemize}
547 % \item There was no complex root at all: |#2| will be |\q_nil|
548 % \item More than one complex root sign was present: |#3| will not
549 % be equal to |\q_nil i|.
550 % \item All of the number is in the complex part with a leading
551 % root: |#1| will be empty. This includes the case where
552 % the input was \emph{just} a root symbol (plus possibly sign,
553 % exponent): we need to cover that.
554 % \item All of the number was before the complex root: |#2| will
555 % be empty and we need to check |#1| fully to split out the two
556 % parts
557 % \item The input has a real part with the complex part starting
558 % with the root symbol: just the last token needs to be separated.
559 % \end{itemize}
560 % \begin{macrocode}
561 \cs_new_protected:Npn \@@_parse_root_auxii:nnnN #1#2#3#4
562 {
563 \quark_if_nil:nTF {#2}
564 { \tl_set:Nn \l_@@_real_tl {#1} }
565 {
566 \str_if_eq:nnTF {#3} { \q_nil #4 }
567 {
568 \tl_set:Nn \l_@@_img_tl {#2}
569 \tl_if_blank:nTF {#1}
570 {
571 \tl_if_blank:nT {#2}
572 { \tl_set:Nn \l_@@_img_tl { 1 } }
573 }
574 {
575 \tl_if_blank:nTF {#2}
576 { \@@_parse_split:n {#1} }
577 { \@@_parse_sign_check:n {#1} }
578 }
579 }
580 {
581 \msg_error:nnV { siunitx } { duplicate-complex-root }
582 \l_@@_input_tl
583 }
584 }
585 }
586 % \end{macrocode}
587 % \end{macro}
588 % \end{macro}
589 % \end{macro}
590 %
591 % \begin{macro}{\@@_parse_sign:}
592 % \begin{macro}{\@@_parse_sign_aux:Nw}
593 % The first token of a number after a comparator could be a sign. A quick
594 % check is made and if found stored. There is no need to worry about the
595 % nature of the sign: we keep them regardless.
596 % \begin{macrocode}
597 \cs_new_protected:Npn \@@_parse_sign:
598 {
599 \exp_after:wN \@@_parse_sign_aux:Nw
600 \l_@@_arg_tl \q_stop
601 }
602 \cs_new_protected:Npn \@@_parse_sign_aux:Nw #1#2 \q_stop
603 {
604 \tl_if_in:NnT \l_siunitx_number_input_sign_tl {#1}
605 {
606 \tl_set:Nn \l_@@_sign_tl {#1}
607 \tl_set:Nn \l_@@_arg_tl {#2}
608 }
609 \tl_if_empty:NF \l_@@_arg_tl
610 { \@@_parse_exponent: }
611 }
612 % \end{macrocode}
613 % \end{macro}
614 % \end{macro}
615 %
616 % \begin{macro}{\@@_parse_sign_check:n}
617 % \begin{macro}{\@@_parse_sign_check:nN}
618 % \begin{macro}{\@@_parse_sign_check:nNw}
619 % Here, we want to check that the last token in the input is a sign.
620 % There cannot be anything after the sign, and there has to be at one
621 % token before the sign: we can therefore signal a parsing error if we
622 % need to.
623 % \begin{macrocode}
624 \cs_new_protected:Npn \@@_parse_sign_check:n #1
625 {
626 \@@_parse_sign_check:nN { } #1 \q_recursion_tail \q_recursion_stop
627 }
628 \cs_new_protected:Npn \@@_parse_sign_check:nN #1#2
629 {
630 \quark_if_recursion_tail_stop_do:Nn #2
631 { \@@_parse_clear: }
632 \tl_if_in:NnTF \l_siunitx_number_input_sign_tl {#2}
633 { \@@_parse_sign_check:nNw {#1} #2 }
634 { \@@_parse_sign_check:nN {#1#2} }
635 }
636 \cs_new_protected:Npn \@@_parse_sign_check:nNw
637 #1#2 #3 \q_recursion_tail \q_recursion_stop
638 {
639 \tl_if_blank:nTF {#3}
640 {
641 \tl_if_blank:nTF {#1}
642 { \@@_parse_clear: }
643 {
644 \tl_set:Nn \l_@@_real_tl {#1}
645 \tl_set:Nn \l_@@_join_tl {#2}
646 }
647 }
648 { \@@_parse_clear: }
649 }
650 % \end{macrocode}
651 % \end{macro}
652 % \end{macro}
653 % \end{macro}
654 %
655 % \begin{macro}{\@@_parse_split:n}
656 % \begin{macro}{\@@_parse_split:nN}
657 % \begin{macro}{\@@_parse_split:w}
658 % Checking for a sign inside the leading part of the number is a simple loop.
659 % There is the possibility that there is no number in the imaginary part
660 % needs to be allowed for. Notice that we do a check that there is some
661 % real part: this covers for example an original input |++1i|, which
662 % otherwise would not be trapped.
663 % \begin{macrocode}
664 \cs_new_protected:Npn \@@_parse_split:n #1
665 {
666 \@@_parse_split:nN { } #1 \q_recursion_tail \q_recursion_stop
667 }
668 \cs_new_protected:Npn \@@_parse_split:nN #1#2
669 {
670 \quark_if_recursion_tail_stop_do:Nn #2
671 { \tl_set:Nn \l_@@_img_tl {#1} }
672 \tl_if_in:NnTF \l_siunitx_number_input_sign_tl {#2}
673 {
674 \tl_set:Nn \l_@@_real_tl {#1}
675 \tl_set:Nn \l_@@_join_tl {#2}
676 \@@_parse_split:w
677 }
678 { \@@_parse_split:nN {#1#2} }
679 }
680 \cs_new_protected:Npn \@@_parse_split:w #1 \q_recursion_tail \q_recursion_stop
681 {
682 \tl_set:Ne \l_@@_img_tl
683 {
684 \tl_if_blank:nTF {#1}
685 { 1 }
686 { \exp_not:n {#1} }
687 }
688 \tl_if_empty:NT \l_@@_real_tl
689 { \@@_parse_clear: }
690 }
691 % \end{macrocode}
692 % \end{macro}
693 % \end{macro}
694 % \end{macro}
695 %
696 % \begin{macro}{\@@_parse_polar:nn}
697 % Almost trivial but repeated in a couple of places so worth an auxiliary.
698 % \begin{macrocode}
699 \cs_new_protected:Npn \@@_parse_polar:nn #1#2
700 {
701 \bool_if:NTF \l_siunitx_number_parse_bool
702 { \siunitx_number_parse:nN {#1} \l_@@_mag_tl }
703 { \tl_set:Nn \l_@@_mag_tl { \ensuremath {#1} } }
704 \group_begin:
705 \@@_suppress_non_polar:
706 \siunitx_number_format:nN {#2} \l_@@_angle_tl
707 \exp_args:NNNV \group_end:
708 \tl_set:Nn \l_@@_angle_tl \l_@@_angle_tl
709 }
710 % \end{macrocode}
711 % \end{macro}
712 %
713 % \section{Formatting}
714 %
715 % \begin{variable}{\l_@@_bracket_close_tl, \l_@@_bracket_open_tl}
716 % Purely internal for the present.
717 % \begin{macrocode}
718 \tl_new:N \l_@@_bracket_close_tl
719 \tl_new:N \l_@@_bracket_open_tl
720 \tl_set:Nn \l_@@_bracket_open_tl { ( }
721 \tl_set:Nn \l_@@_bracket_close_tl { ) }
722 % \end{macrocode}
723 % \end{variable}
724 %
725 % \begin{variable}{\l_@@_unit_tl}
726 % \begin{macrocode}
727 \tl_new:N \l_@@_unit_tl
728 % \end{macrocode}
729 % \end{variable}
730 %
731 % \begin{macro}{\siunitx_complex_number:n, \siunitx_complex_number:e}
732 % \begin{macro}{\siunitx_complex_number:nn, \@@_number:nn}
733 % \begin{macro}{\siunitx_complex_quantity:nn, \siunitx_complex_quantity:en}
734 % \begin{macro}{\siunitx_complex_quantity:nnn, \@@_quantity:nnn}
735 % The work here is pretty trivial: only conversion between forms makes
736 % things a bit more intricate.
737 % \begin{macrocode}
738 \cs_new_protected:Npn \siunitx_complex_number:n #1
739 {
740 \bool_if:NTF \l_siunitx_number_parse_bool
741 {
742 \group_begin:
743 \@@_suppress_negative:
744 \@@_expand:n {#1}
745 \bool_if:NTF \l_@@_force_polar_bool
746 {
747 \use:e
748 {
749 \siunitx_complex_number:nn
750 \@@_convert_polar:V \l_@@_arg_tl
751 }
752 }
753 {
754 \@@_parse:V \l_@@_arg_tl
755 \@@_format_cartesian:n { }
756 }
757 \group_end:
758 }
759 {
760 \siunitx_number_format:nN {#1} \l_@@_tmp_tl
761 \siunitx_print_number:V \l_@@_tmp_tl
762 }
763 }
764 \cs_generate_variant:Nn \siunitx_complex_number:n { e, x }
765 \cs_new_protected:Npn \siunitx_complex_number:nn #1#2
766 {
767 \bool_lazy_or:nnTF
768 { \tl_if_blank_p:n {#1} }
769 { \tl_if_blank_p:n {#2} }
770 {
771 \msg_error:nnnn { siunitx } { invalid-polar-form }
772 {#1} {#2}
773 }
774 { \@@_number:nn {#1} {#2} }
775 }
776 \cs_new_protected:Npn \@@_number:nn #1#2
777 {
778 \bool_lazy_and:nnTF
779 { \l_@@_force_cartesian_bool }
780 { \l_siunitx_number_parse_bool }
781 {
782 \siunitx_complex_number:e
783 { \@@_convert_cartesian:nn {#1} {#2} }
784 }
785 {
786 \group_begin:
787 \@@_suppress_negative:
788 \@@_parse_polar:nn {#1} {#2}
789 \@@_format_polar:n { }
790 \group_end:
791 }
792 }
793 \cs_new_protected:Npn \siunitx_complex_quantity:nn #1#2
794 {
795 \bool_if:NTF \l_siunitx_number_parse_bool
796 {
797 \group_begin:
798 \@@_suppress_negative:
799 \@@_expand:n {#1}
800 \bool_if:NTF \l_@@_force_polar_bool
801 {
802 \use:e
803 {
804 \siunitx_complex_quantity:nnn
805 \@@_convert_polar:V \l_@@_arg_tl
806 }
807 }
808 {
809 \@@_parse:V \l_@@_arg_tl
810 \@@_format_cartesian:n
811 }
812 {#2}
813 \group_end:
814 }
815 { \siunitx_quantity:nn {#1} {#2} }
816 }
817 \cs_generate_variant:Nn \siunitx_complex_quantity:nn { e , x }
818 \cs_new_protected:Npn \siunitx_complex_quantity:nnn #1#2#3
819 {
820 \bool_lazy_or:nnTF
821 { \tl_if_blank_p:n {#1} }
822 { \tl_if_blank_p:n {#2} }
823 {
824 \msg_error:nnnn { siunitx } { invalid-polar-form }
825 {#1} {#2}
826 }
827 { \@@_quantity:nnn {#1} {#2} {#3} }
828 }
829 \cs_new_protected:Npn \@@_quantity:nnn #1#2#3
830 {
831 \bool_lazy_and:nnTF
832 { \l_@@_force_cartesian_bool }
833 { \l_siunitx_number_parse_bool }
834 {
835 \siunitx_complex_quantity:en
836 { \@@_convert_cartesian:nn {#1} {#2} }
837 {#3}
838 }
839 {
840 \group_begin:
841 \@@_suppress_negative:
842 \@@_parse_polar:nn {#1} {#2}
843 \@@_format_polar:n {#3}
844 \group_end:
845 }
846 }
847 % \end{macrocode}
848 % \end{macro}
849 % \end{macro}
850 % \end{macro}
851 % \end{macro}
852 %
853 % \begin{macro}{\@@_format_cartesian:n}
854 % \begin{macro}{\@@_format_cartesian_auxi:n}
855 % \begin{macro}{\@@_format_cartesian_auxii:n}
856 % \begin{macro}{\@@_drop_exponent:nnnnnnn}
857 % \begin{macro}{\@@_format_sign:nnnnnnn}
858 % \begin{macro}{\@@_extract_exponent:nw}
859 % \begin{macro}{\@@_extract_exponent_aux:w}
860 % \begin{macro}[EXP]{\@@_format_bracket:n}
861 % We start here checking that there is something to do.
862 % \begin{macrocode}
863 \cs_new_protected:Npn \@@_format_cartesian:n #1
864 {
865 \bool_lazy_and:nnF
866 { \tl_if_empty_p:N \l_@@_real_tl }
867 { \tl_if_empty_p:N \l_@@_img_tl }
868 { \@@_format_cartesian_auxi:n {#1} }
869 }
870 % \end{macrocode}
871 % We split based on whether the number has a complex part at all,
872 % then print the result.
873 % \begin{macrocode}
874 \cs_new_protected:Npn \@@_format_cartesian_auxi:n #1
875 {
876 \tl_clear:N \l_@@_tmp_tl
877 \tl_if_empty:NTF \l_@@_img_tl
878 {
879 \siunitx_number_process:NN \l_@@_real_tl \l_@@_real_tl
880 \tl_set:Ne \l_@@_tmp_tl
881 { \siunitx_number_output:N \l_@@_real_tl }
882 }
883 { \@@_format_cartesian_auxii:n {#1} }
884 \tl_if_blank:nTF {#1}
885 { \siunitx_print_number:V \l_@@_tmp_tl }
886 { \siunitx_quantity_print:VV \l_@@_tmp_tl \l_@@_unit_tl }
887 }
888 % \end{macrocode}
889 % If we get to this stage we have both parts to a complex number. We
890 % need to process both and do some massaging, then it's just a question
891 % of reassembly with the right parts in the right places.
892 % \begin{macrocode}
893 \cs_new_protected:Npn \@@_format_cartesian_auxii:n #1
894 {
895 \@@_format_cartesian_units:n {#1}
896 \tl_if_empty:NF \l_@@_real_tl
897 { \exp_after:wN \@@_drop_exponent:nnnnnnn \l_@@_real_tl }
898 \exp_after:wN \@@_format_sign:nnnnnnn \l_@@_img_tl
899 \tl_set:Ne \l_@@_tmp_tl
900 { \siunitx_number_output:NN \l_@@_img_tl \q_nil }
901 \exp_after:wN \@@_extract_exponent:w \l_@@_tmp_tl \q_stop
902 \tl_set:Ne \l_@@_tmp_tl
903 {
904 \bool_lazy_or:nnTF
905 {
906 \bool_lazy_and_p:nn
907 { \l_siunitx_number_bracket_ambiguous_bool }
908 { ! \tl_if_empty_p:N \l_@@_exp_tl }
909 }
910 {
911 ! \bool_lazy_any_p:n
912 {
913 { \tl_if_blank_p:n {#1} }
914 { \tl_if_empty_p:N \l_@@_real_tl }
915 { \tl_if_empty_p:N \l_@@_img_tl }
916 }
917 }
918 { \@@_format_bracket:n }
919 { \use:n }
920 {
921 \siunitx_number_output:N \l_@@_real_tl
922 \exp_not:V \l_@@_sign_tl
923 \bool_if:NF \l_@@_root_after_bool
924 { \exp_not:V \l_@@_output_root_tl }
925 \exp_not:V \l_@@_tmp_tl
926 \bool_if:NT \l_@@_root_after_bool
927 { \exp_not:V \l_@@_output_root_tl }
928 }
929 \exp_not:V \l_@@_exp_tl
930 }
931 }
932 % \end{macrocode}
933 % No exponent for the real part.
934 % \begin{macrocode}
935 \cs_new_protected:Npn \@@_drop_exponent:nnnnnnn #1#2#3#4#5#6#7
936 { \tl_set:Nn \l_@@_real_tl { {#1} {#2} {#3} {#4} {#5} { } { 0 } } }
937 % \end{macrocode}
938 % Ensure the imaginary part has a sign.
939 % \begin{macrocode}
940 \cs_new_protected:Npn \@@_format_sign:nnnnnnn #1#2#3#4#5#6#7
941 {
942 \tl_set:Ne \l_@@_img_tl
943 {
944 { }
945 {
946 \tl_if_blank:nTF {#2}
947 { \tl_if_empty:NF \l_@@_real_tl { + } }
948 { \exp_not:n {#2} }
949 }
950 \exp_not:n { {#3} {#4} {#5} {#6} {#7} }
951 }
952 }
953 % \end{macrocode}
954 % Pull out the formatted exponent: we also need the sign.
955 % An imaginary part that is exactly $1$ is omitted, with only the complex
956 % root printed. That means checking and removing a lone $1$ here.
957 % \begin{macrocode}
958 \cs_new_protected:Npn \@@_extract_exponent:w
959 #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_nil #6 \q_nil #7 \q_nil #8
960 \q_nil #9 \q_stop
961 {
962 \tl_set:Nn \l_@@_sign_tl {#1#2}
963 \bool_lazy_all:nTF
964 {
965 { ! \l_@@_print_unity_bool }
966 { \str_if_eq_p:nn {#3} { 1 } }
967 { \tl_if_blank_p:n {#5} }
968 }
969 { \@@_extract_exponent_aux:nw {#6#7#8} }
970 { \@@_extract_exponent_aux:nw {#3#4#5#6#7#8} }
971 #9 \q_stop
972 }
973 \cs_new:Npn \@@_extract_exponent_aux:nw
974 #1#2 \q_nil #3 \q_nil #4 \q_stop
975 {
976 \tl_set:Nn \l_@@_tmp_tl {#1#2}
977 \tl_set:Nn \l_@@_exp_tl {#3#4}
978 }
979 \cs_new_protected:Npn \@@_format_bracket:n #1
980 {
981 \exp_not:V \l_@@_bracket_open_tl
982 #1
983 \exp_not:V \l_@@_bracket_close_tl
984 }
985 % \end{macrocode}
986 % \end{macro}
987 % \end{macro}
988 % \end{macro}
989 % \end{macro}
990 % \end{macro}
991 % \end{macro}
992 % \end{macro}
993 % \end{macro}
994 % \end{macro}
995 %
996 % \begin{macro}{\@@_format_cartesian_units:n}
997 % \begin{macro}
998 % {
999 % \@@_format_cartesian_combine-exponent:n ,
1000 % \@@_format_cartesian_extract-exponent:n ,
1001 % \@@_format_cartesian_input:n
1002 % }
1003 % \begin{macro}{\@@_format_combine-exponent:nn}
1004 % \begin{macro}{\@@_format_tidy_exp:}
1005 % \begin{macro}{\@@_format_tidy_exp:nn}
1006 % \begin{macro}{\@@_format_tidy_exp:nN}
1007 % \begin{macro}{\@@_format_tidy_exp:N}
1008 % \begin{macro}[EXP]{\@@_extract_exp:nnnnnnn}
1009 % \begin{macro}{\@@_drop_exp:N}
1010 % \begin{macro}[EXP]{\@@_drop_exp:nnnnnnnN}
1011 % Formatting units needs to know the settings from the main module, and
1012 % the flow is then much the same as in \pkg{siunitx-compound}. We only
1013 % have to watch the fact there are two numbers to format.
1014 % \begin{macrocode}
1015 \cs_new_protected:Npn \@@_format_cartesian_units:n #1
1016 {
1017 \tl_if_blank:nTF {#1}
1018 {
1019 \siunitx_number_process:NN \l_@@_real_tl \l_@@_real_tl
1020 \siunitx_number_process:NN \l_@@_img_tl \l_@@_img_tl
1021 \tl_if_empty:NF \l_@@_real_tl
1022 { \@@_format_cartesian_tidy_exp: }
1023 }
1024 {
1025 \use:c
1026 { @@_format_cartesian_ \l_siunitx_quantity_prefix_mode_str :n } {#1}
1027 }
1028 }
1029 \cs_new_protected:cpn { @@_format_cartesian_combine-exponent:n } #1
1030 {
1031 \siunitx_number_process:NN
1032 \l_@@_img_tl \l_@@_img_tl
1033 \fp_set:Nn \l_@@_tmp_fp
1034 {
1035 \exp_after:wN \@@_extract_exp:nnnnnnn
1036 \l_@@_img_tl
1037 }
1038 \tl_if_empty:NF \l_@@_real_tl
1039 {
1040 \siunitx_number_process:NN
1041 \l_@@_real_tl \l_@@_real_tl
1042 \@@_format_cartesian_tidy_exp:
1043 \@@_drop_exp:N \l_@@_real_tl
1044 }
1045 \@@_drop_exp:N \l_@@_img_tl
1046 \siunitx_unit_format_combine_exponent:nnN {#1}
1047 \l_@@_tmp_fp \l_@@_unit_tl
1048 }
1049 \cs_new_protected:cpe { @@_format_cartesian_extract-exponent:n } #1
1050 {
1051 \exp_not:N \siunitx_unit_format_extract_prefixes:nNN {#1}
1052 \exp_not:N \l_@@_unit_tl \exp_not:N \l_@@_tmp_fp
1053 \exp_not:c { @@_format_extract-exponent:N }
1054 \exp_not:N \l_@@_img_tl
1055 \exp_not:N \tl_if_empty:NF \exp_not:N \l_@@_real_tl
1056 {
1057 \exp_not:c { @@_format_extract-exponent:N }
1058 \exp_not:N \l_@@_real_tl
1059 }
1060 }
1061 \cs_new_protected:Npn \@@_format_cartesian_input:n #1
1062 {
1063 \siunitx_number_process:NN \l_@@_real_tl \l_@@_real_tl
1064 \siunitx_number_process:NN \l_@@_img_tl \l_@@_img_tl
1065 \tl_if_empty:NF \l_@@_real_tl
1066 { \@@_format_cartesian_tidy_exp: }
1067 \siunitx_unit_format:nN {#1} \l_@@_unit_tl
1068 }
1069 % \end{macrocode}
1070 % Here, as we know this is only going to work if the exponents are integers,
1071 % we use a faster string comparison for the fire-off.
1072 % \begin{macrocode}
1073 \cs_new_protected:Npn \@@_format_cartesian_tidy_exp:
1074 {
1075 \exp_args:Nee \@@_format_cartesian_tidy_exp:nn
1076 {
1077 \exp_after:wN \@@_extract_exp:nnnnnnn
1078 \l_@@_real_tl
1079 }
1080 {
1081 \exp_after:wN \@@_extract_exp:nnnnnnn
1082 \l_@@_img_tl
1083 }
1084 }
1085 \cs_new_protected:Npn \@@_format_cartesian_tidy_exp:nn #1#2
1086 {
1087 \str_if_eq:nnF {#1} {#2}
1088 {
1089 \fp_compare:nNnTF {#1} > {#2}
1090 {
1091 \fp_set:Nn \l_@@_tmp_fp {#1}
1092 \@@_format_cartesian_tidy_exp:nN
1093 {#1} \l_@@_img_tl
1094 }
1095 {
1096 \@@_format_cartesian_tidy_exp:nN
1097 {#2} \l_@@_real_tl
1098 }
1099 }
1100 }
1101 \cs_new_protected:Npn \@@_format_cartesian_tidy_exp:nN #1#2
1102 {
1103 \group_begin:
1104 \keys_set:nn { siunitx }
1105 {
1106 exponent-mode = fixed ,
1107 fixed-exponent = \fp_eval:n { round(#1,0) }
1108 }
1109 \siunitx_number_process:NN #2 #2
1110 \exp_args:NNNV \group_end:
1111 \tl_set:Nn #2 #2
1112 }
1113 \cs_new_protected:cpn { @@_format_extract-exponent:N } #1
1114 {
1115 \tl_set:Ne #1
1116 { \siunitx_number_adjust_exponent:Nn #1 \l_@@_tmp_fp }
1117 \siunitx_number_process:NN #1 #1
1118 }
1119 \cs_new:Npn \@@_extract_exp:nnnnnnn #1#2#3#4#5#6#7 { #6 0 #7 }
1120 \cs_new_protected:Npn \@@_drop_exp:N #1
1121 { \exp_after:wN \@@_drop_exp:nnnnnnnN #1 #1 }
1122 \cs_new_protected:Npn \@@_drop_exp:nnnnnnnN #1#2#3#4#5#6#7#8
1123 { \tl_set:Nn #8 { {#1} {#2} {#3} {#4} {#5} { } { 0 } } }
1124 % \end{macrocode}
1125 % \end{macro}
1126 % \end{macro}
1127 % \end{macro}
1128 % \end{macro}
1129 % \end{macro}
1130 % \end{macro}
1131 % \end{macro}
1132 % \end{macro}
1133 % \end{macro}
1134 % \end{macro}
1135 %
1136 % \begin{macro}
1137 % {
1138 % \@@_format_polar:n ,
1139 % \@@_format_combine-exponent:n ,
1140 % \@@_format_extract-exponent:n ,
1141 % \@@_format_polar_input:n
1142 % }
1143 % \begin{macro}{\@@_format_phase:}
1144 % We see similar ideas here to the Cartesian versions, but with only
1145 % the magnitude to adjust, things are rather simpler in the exponent
1146 % manipulations.
1147 % \begin{macrocode}
1148 \cs_new_protected:Npn \@@_format_polar:n #1
1149 {
1150 \tl_if_blank:nTF {#1}
1151 {
1152 \bool_if:NT \l_siunitx_number_parse_bool
1153 { \siunitx_number_process:NN \l_@@_mag_tl \l_@@_mag_tl }
1154 }
1155 {
1156 \bool_if:NTF \l_siunitx_number_parse_bool
1157 {
1158 \use:c
1159 { @@_format_polar_ \l_siunitx_quantity_prefix_mode_str :n } {#1}
1160 }
1161 { \@@_format_polar_input:n {#1} }
1162 }
1163 \tl_set:Ne \l_@@_tmp_tl
1164 {
1165 \bool_if:NTF \l_siunitx_number_parse_bool
1166 { \siunitx_number_output:N \l_@@_mag_tl }
1167 { \exp_not:V \l_@@_mag_tl }
1168 }
1169 \siunitx_print_number:V \l_@@_tmp_tl
1170 \l_@@_phase_tl { \@@_format_phase: }
1171 \siunitx_quantity_print:nV { } \l_@@_unit_tl
1172 }
1173 \cs_new_protected:cpn { @@_format_polar_combine-exponent:n } #1
1174 {
1175 \siunitx_number_process:NN \l_@@_mag_tl \l_@@_mag_tl
1176 \fp_set:Nn \l_@@_tmp_fp
1177 { \exp_after:wN \@@_extract_exp:nnnnnnn \l_@@_mag_tl }
1178 \@@_drop_exp:N \l_@@_mag_tl
1179 \siunitx_unit_format_combine_exponent:nnN {#1}
1180 \l_@@_tmp_fp \l_@@_unit_tl
1181 }
1182 \cs_new_protected:cpe { @@_format_polar_extract-exponent:n } #1
1183 {
1184 \exp_not:N \siunitx_unit_format_extract_prefixes:nNN {#1}
1185 \exp_not:N \l_@@_unit_tl \exp_not:N \l_@@_tmp_fp
1186 \exp_not:c { @@_format_extract-exponent:N }
1187 \exp_not:N \l_@@_mag_tl
1188 }
1189 \cs_new_protected:Npn \@@_format_polar_input:n #1
1190 {
1191 \bool_if:NT \l_siunitx_number_parse_bool
1192 { \siunitx_number_process:NN \l_@@_mag_tl \l_@@_mag_tl }
1193 \siunitx_unit_format:nN {#1} \l_@@_unit_tl
1194 }
1195 \cs_new_protected:Npn \@@_format_phase:
1196 {
1197 \group_begin:
1198 \bool_if:NTF \l_@@_polar_degree_bool
1199 {
1200 \exp_args:NV \siunitx_unit_options_apply:n \l_@@_symbol_degree_tl
1201 \siunitx_unit_format:VN \l_@@_symbol_degree_tl \l_@@_tmp_tl
1202 }
1203 { \tl_clear:N \l_@@_tmp_tl }
1204 \siunitx_quantity_print:VV \l_@@_angle_tl \l_@@_tmp_tl
1205 \group_end:
1206 }
1207 % \end{macrocode}
1208 % \end{macro}
1209 % \end{macro}
1210 %
1211 % \subsection{Conversion}
1212 %
1213 % \begin{macro}[EXP]
1214 % {
1215 % \@@_convert_cartesian:nn ,
1216 % \@@_convert_cartesian_aux:nn ,
1217 % \@@_convert_cartesian_aux:ee
1218 % }
1219 % \begin{macro}[EXP]{\@@_convert_cartesian_aux:w}
1220 % \begin{macro}[EXP]
1221 % {
1222 % \@@_convert_cartesian_aux:nnn ,
1223 % \@@_convert_cartesian_aux:nen ,
1224 % \@@_convert_cartesian_aux:enn
1225 % }
1226 % Conversion to Cartesian form is easy as we have two inputs and need to
1227 % do no parsing here at all. In most cases the exponents for the two
1228 % output parts will be the same, but that is not certain.
1229 % \begin{macrocode}
1230 \cs_new:Npn \@@_convert_cartesian:nn #1#2
1231 {
1232 \@@_convert_cartesian_aux:ee
1233 {
1234 \fp_to_tl:n
1235 { (#1) * cos \bool_if:NT \l_@@_polar_degree_bool { d } (#2) }
1236 }
1237 {
1238 \fp_to_tl:n
1239 { (#1) * sin \bool_if:NT \l_@@_polar_degree_bool { d } (#2) }
1240 }
1241 }
1242 \cs_new:Npn \@@_convert_cartesian_aux:nn #1#2
1243 {
1244 \@@_convert_cartesian_aux:w #1 e 0 e \q_mark #2 e 0 e \q_stop
1245 }
1246 \cs_generate_variant:Nn \@@_convert_cartesian_aux:nn { ee }
1247 \cs_new:Npn \@@_convert_cartesian_aux:w
1248 #1 e #2 e #3 \q_mark #4 e #5 e #6 \q_stop
1249 {
1250 \int_compare:nNnTF {#2} = {#5}
1251 { \@@_convert_cartesian_aux:nnn {#1} {#4} {#2} }
1252 {
1253 \int_compare:nNnTF {#2} > {#5}
1254 {
1255 \@@_convert_cartesian_aux:nen
1256 {#1}
1257 { \fp_to_decimal:n { #4 * 10 ** (#5 - #2) } }
1258 {#2}
1259 }
1260 {
1261 \@@_convert_cartesian_aux:enn
1262 { \fp_to_decimal:n { #1 * 10 ** (#2 - #5) } }
1263 {#4}
1264 {#5}
1265 }
1266 }
1267 }
1268 \cs_new:Npn \@@_convert_cartesian_aux:nnn #1#2#3
1269 {
1270 \fp_compare:nNnF {#1} = \c_zero_fp
1271 {#1}
1272 \fp_compare:nNnF {#2} = \c_zero_fp
1273 {
1274 \fp_compare:nNnF {#2} < \c_zero_fp { + }
1275 #2 i
1276 }
1277 \int_compare:nNnF {#3} = 0
1278 { e #3 }
1279 }
1280 \cs_generate_variant:Nn \@@_convert_cartesian_aux:nnn { e , ne }
1281 % \end{macrocode}
1282 % \end{macro}
1283 % \end{macro}
1284 % \end{macro}
1285 %
1286 % \begin{macro}[EXP]{\@@_convert_polar:n, \@@_convert_polar:V}
1287 % \begin{macro}[EXP]{\@@_convert_polar_auxi:w}
1288 % \begin{macro}[EXP]{\@@_convert_polar_auxii:nw}
1289 % \begin{macro}[EXP]{\@@_convert_polar_auxiii:nw}
1290 % \begin{macro}[EXP]{\@@_convert_polar_auxiv:nnw}
1291 % \begin{macro}[EXP]{\@@_convert_polar_auxv:nnw}
1292 % \begin{macro}[EXP]{\@@_convert_polar_auxvi:nnw}
1293 % \begin{macro}[EXP]{\@@_convert_polar_auxvii:nnn, \@@_convert_polar_auxvii:enn}
1294 % \begin{macro}[EXP]{\@@_convert_polar_auxviii:nnn}
1295 % \begin{macro}[EXP]
1296 % {\@@_convert_polar_auxix:nnn, \@@_convert_polar_auxix:eee}
1297 % \begin{macro}[EXP]
1298 % {\@@_convert_polar_auxix:nn, \@@_convert_polar_auxix:ee}
1299 % A simplified parser for complex numbers which works by expansion,
1300 % then converts to polar form.
1301 % \begin{macrocode}
1302 \cs_new:Npn \@@_convert_polar:n #1
1303 { \@@_convert_polar_auxi:w #1 e e \q_stop }
1304 \cs_generate_variant:Nn \@@_convert_polar:n { V }
1305 \cs_new:Npn \@@_convert_polar_auxi:w #1 e #2 e #3 \q_stop
1306 { \@@_convert_polar_auxii:nw {#2} #1 \q_stop }
1307 \cs_new:Npn \@@_convert_polar_auxii:nw #1#2#3 \q_stop
1308 {
1309 \bool_lazy_or:nnTF
1310 { \str_if_eq_p:nn {#2} { i } }
1311 { \str_if_eq_p:nn {#2#3} { +i } }
1312 { \@@_convert_polar_auxviii:nnn { } { 1 } {#1} }
1313 {
1314 \str_if_eq:nnTF {#2#3} { -i }
1315 { \@@_convert_polar_auxviii:nnn { } { -1 } {#1} }
1316 {
1317 \str_if_eq:nnTF {#2} { \pm }
1318 {
1319 \msg_expandable_error:nn { siunitx }
1320 { cannot-convert-to-polar }
1321 { 0 } { }
1322 }
1323 { \@@_convert_polar_auxiii:nnw {#1} {#2} #3 \pm \pm \q_stop }
1324 }
1325 }
1326 }
1327 \cs_new:Npn \@@_convert_polar_auxiii:nnw #1#2#3 \pm #4 \pm #5 \q_stop
1328 {
1329 \tl_if_blank:nTF {#5}
1330 { \@@_convert_polar_auxiv:nnw {#1} {#2} #3 + + \q_stop }
1331 {
1332 \msg_expandable_error:nn { siunitx } { cannot-convert-to-polar }
1333 { 0 } { }
1334 }
1335 }
1336 \cs_new:Npn \@@_convert_polar_auxiv:nnw #1#2#3 + #4 + #5 \q_stop
1337 {
1338 \tl_if_blank:nTF {#4}
1339 { \@@_convert_polar_auxv:nnw {#1} {#2} #3 - - \q_stop }
1340 {
1341 \str_if_eq:eeTF { \use:n #4 } { i }
1342 { \@@_convert_polar_auxviii:nnn {#2#3} { 1 } {#1} }
1343 { \@@_convert_polar_auxvi:nnw {#2#3} {#1} #4 i \q_nil i \q_stop }
1344 }
1345 }
1346 \cs_new:Npn \@@_convert_polar_auxv:nnw #1#2#3 - #4 - #5 \q_stop
1347 {
1348 \tl_if_blank:nTF {#4}
1349 { \@@_convert_polar_auxvi:nnw { } {#1} #2#3 i \q_nil i \q_stop }
1350 {
1351 \str_if_eq:eeTF { \use:n #4 } { i }
1352 { \@@_convert_polar_auxviii:nnn { } { -1 } {#1} }
1353 { \@@_convert_polar_auxvi:nnw {#2#3} {#1} -#4 i \q_nil i \q_stop }
1354 }
1355 }
1356 % \end{macrocode}
1357 % Negative real values are special-cased here for efficiency.
1358 % \begin{macrocode}
1359 \cs_new:Npn \@@_convert_polar_auxvi:nnw #1#2#3 i #4 i #5 \q_stop
1360 {
1361 \quark_if_nil:nTF {#4}
1362 {
1363 \tl_if_head_eq_meaning:nNTF {#3} -
1364 { \@@_convert_polar_auxvii:enn { \use_none:n #3 } { 180 } }
1365 { \@@_convert_polar_auxvii:nnn {#3} { 0 } }
1366 {#2}
1367 }
1368 { \@@_convert_polar_auxviii:nnn {#1} {#3} {#2} }
1369 }
1370 \cs_new:Npn \@@_convert_polar_auxvii:nnn #1#2#3
1371 {
1372 { #1 \tl_if_blank:nF {#3} { e#3 } } {#2}
1373 }
1374 \cs_generate_variant:Nn \@@_convert_polar_auxvii:nnn { e }
1375 \cs_new:Npn \@@_convert_polar_auxviii:nnn #1#2#3
1376 {
1377 \@@_convert_polar_auxix:eee
1378 { \tl_if_blank:nTF {#1} { 0 } {#1} }
1379 { \tl_if_blank:nTF {#2} { 0 } {#2} }
1380 { \tl_if_blank:nF {#3} { e#3 } }
1381 }
1382 \cs_new:Npn \@@_convert_polar_auxix:nnn #1#2#3
1383 {
1384 \@@_format_polar_auxx:ee
1385 { \fp_eval:n { sqrt ( (#1#3)^2 + (#2#3)^2 ) } }
1386 {
1387 \fp_eval:n
1388 { atan \bool_if:NT \l_@@_polar_degree_bool { d } (#2 , #1) }
1389 }
1390 }
1391 \cs_generate_variant:Nn \@@_convert_polar_auxix:nnn { eee }
1392 \cs_new:Npn \@@_format_polar_auxx:nn #1#2 { {#1} {#2} }
1393 \cs_generate_variant:Nn \@@_format_polar_auxx:nn { ee }
1394 % \end{macrocode}
1395 % \end{macro}
1396 % \end{macro}
1397 % \end{macro}
1398 % \end{macro}
1399 % \end{macro}
1400 % \end{macro}
1401 % \end{macro}
1402 % \end{macro}
1403 % \end{macro}
1404 % \end{macro}
1405 % \end{macro}
1406 %
1407 % \subsection{Messages}
1408 %
1409 % \begin{macrocode}
1410 \msg_new:nnn { siunitx } { cannot-convert-to-polar }
1411 { Cannot~convert~input~to~polar~form. }
1412 \msg_new:nnnn { siunitx } { duplicate-complex-root }
1413 { Duplicate~complex~root~in~"#1". }
1414 {
1415 Complex~numbers~may~have~only~exactly~one~root~symbol~present.
1416 }
1417 \msg_new:nnnn { siunitx } { invalid-polar-form }
1418 { Invalid~polar~form~"#1:#2". }
1419 {
1420 Complex~numbers~in~polar~form~must~have~both~a~magnitude~and~and~
1421 angle.
1422 }
1423 % \end{macrocode}
1424 %
1425 % \subsection{Deprecated options}
1426 %
1427 % \begin{macrocode}
1428 \keys_define:nn { siunitx }
1429 {
1430 complex-symbol-angle .code:n =
1431 {
1432 \msg_info:nnnn { siunitx } { option-deprecated }
1433 { number-angle-product }
1434 { complex-phase-command }
1435 complex-phase-command = {#1}
1436 }
1437 }
1438 % \end{macrocode}
1439 %
1440 % \subsection{Standard settings for module options}
1441 %
1442 % Some of these follow naturally from the point of definition
1443 % (\foreign{e.g.}~boolean variables are always |false| to begin with),
1444 % but for clarity everything is set here.
1445 % \begin{macrocode}
1446 \keys_set:nn { siunitx }
1447 {
1448 complex-angle-unit = degrees ,
1449 complex-mode = input ,
1450 complex-root-position = after-number ,
1451 complex-phase-command = \ensuremath { \angle } ,
1452 complex-symbol-degree = \degree ,
1453 input-complex-root = ij ,
1454 output-complex-root = \mathrm { i } ,
1455 print-complex-unity = false
1456 }
1457 % \end{macrocode}
1458 %
1459 % \begin{macrocode}
1460 %</package>
1461 % \end{macrocode}
1462 %
1463 % \end{implementation}
1464 %
1465 % \PrintIndex