summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/siunitx/siunitx-symbol.dtx
blob: 1307e2ee0bf508b99477e6fa31aec7bd5f1a2028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
% \iffalse meta-comment
%
% File: siunitx-symbol.dtx Copyright (C) 2018-2021 Joseph Wright
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "siunitx bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% The released version of this bundle is available from CTAN.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/josephwright/siunitx
%
% for those people who are interested.
%
% -----------------------------------------------------------------------
%
%<*driver>
\documentclass{l3doc}
% The next line is needed so that \GetFileInfo will be able to pick up
% version data
\usepackage{siunitx}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{siunitx.sty}
%
% \title{^^A
%   \pkg{siunitx-symbol} -- Symbol-related settings^^A
%   \thanks{This file describes \fileversion,
%     last revised \filedate.}^^A
% }
%
% \author{^^A
%  Joseph Wright^^A
%  \thanks{^^A
%    E-mail:
%    \href{mailto:joseph.wright@morningstar2.co.uk}
%      {joseph.wright@morningstar2.co.uk}^^A
%   }^^A
% }
%
% \date{Released \filedate}
%
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{siunitx-symbol} implementation}
%
% Start the \pkg{DocStrip} guards.
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% Identify the internal prefix (\LaTeX3 \pkg{DocStrip} convention): only
% internal material in this \emph{submodule} should be used directly.
%    \begin{macrocode}
%<@@=siunitx_symbol>
%    \end{macrocode}
%
% \begin{variable}{\l_@@_tmpa_tl, \l_@@_tmpb_tl}
%   Scratch space.
%    \begin{macrocode}
\tl_new:N \l_@@_tmpa_tl
\tl_new:N \l_@@_tmpb_tl
%    \end{macrocode}
% \end{variable}
%
% A small number of commands are needed from the companion fonts when working
% with $8$-bit engines. These are loaded by modern \LaTeXe{} kernel, so for
% older ones, force loading them using \pkg{textcomp}.
%    \begin{macrocode}
\AtBeginDocument
  {
    \cs_if_free:cT { T@TS1 }
      { \RequirePackage { textcomp } }
  }
%    \end{macrocode}
%
% \begin{macro}{\@@_non_latin:n}
% \begin{macro}{\@@_non_latin:nnnn}
%   As in \pkg{siunitx-unit}, but internal in both cases as it's rather
%   specialised.
%    \begin{macrocode}
\bool_lazy_or:nnTF
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:Npn \@@_non_latin:n #1
      { \char_generate:nn {#1} { \char_value_catcode:n {#1} } }
  }
  {
    \cs_new:Npn \@@_non_latin:n #1
      {
        \exp_last_unbraced:Nf \@@_non_latin:nnnn
          { \char_to_utfviii_bytes:n {#1} }
      }
    \cs_new:Npn \@@_non_latin:nnnn #1#2#3#4
      {
        \exp_after:wN \exp_after:wN \exp_after:wN
          \exp_not:N \char_generate:nn {#1} { 13 }
        \exp_after:wN \exp_after:wN \exp_after:wN
          \exp_not:N \char_generate:nn {#2} { 13 }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_if_replace:NnT}
%   A test to see if the unit definition which applies is still one we expect:
%   here that means it is just using a (Unicode) codepoint. The comparison
%   is string-based as \pkg{unicode-math} (at least) can alter some of them.
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_if_replace:Nn #1#2 { T , TF }
  {
    \group_begin:
      \tl_set:Nx \l_@@_tmpa_tl { \@@_non_latin:n {#2} }
      \protected@edef \l_@@_tmpa_tl
        { \exp_not:N \mathrm { \l_@@_tmpa_tl } }
      \keys_set:nn { siunitx } { parse-units = false }
      \siunitx_unit_format:nN {#1} \l_@@_tmpb_tl
      \str_if_eq:VVTF \l_@@_tmpa_tl \l_@@_tmpb_tl
        {
          \group_end:
          \prg_return_true:
        }
        {
          \group_end:
          \prg_return_false:
        }
  }
%    \end{macrocode}
% \end{macro}
%
% At the start of the document, fonts are fixed and the user may have
% altered unit set up. If things are unchanged, we can alter the settings
% such that they use something \enquote{more sensible}.
%    \begin{macrocode}
\AtBeginDocument
  {
    \@@_if_replace:NnT \arcminute { "02B9 }
      {
        \siunitx_declare_unit:Nn \arcminute
          { \exp_not:N \ensuremath { { } ' } }
      }
    \@@_if_replace:NnT \arcsecond { "02BA }
      {
        \siunitx_declare_unit:Nn \arcsecond
          { \exp_not:N \ensuremath { { } '' } }
      }
%    \end{macrocode}
%   For |\degree|, direct input works in text mode so there is
%   only a need to tidy up for math mode. If \pkg{fontspec} is loaded then that
%   problem goes away, so nothing needs to be done.
%    \begin{macrocode}
    \@@_if_replace:NnT \degree { "00B0 }
      {
        \@ifpackageloaded { fontspec }
          { }
          {
            \siunitx_declare_unit:Nxn \degree
              {
                \siunitx_print_text:n
                  { \@@_non_latin:n { "00B0 } }
              }
              { quantity-product = { } }
         }
      }
%    \end{macrocode}
%   For |\degreeCelsius|, much the same to think about but the comparison
%   must be done by hand.
%    \begin{macrocode}
    \group_begin:
      \tl_set:Nx \l_@@_tmpa_tl { \@@_non_latin:n { "00B0 } C }
      \protected@edef \l_@@_tmpa_tl
        { \exp_not:N \mathrm { \l_@@_tmpa_tl } }
      \keys_set:nn { siunitx } { parse-units = false }
      \siunitx_unit_format:nN { \degreeCelsius } \l_@@_tmpb_tl
      \str_if_eq:VVTF \l_@@_tmpa_tl \l_@@_tmpb_tl
        {
          \group_end:
          \@ifpackageloaded { fontspec }
            { }
            {
              \siunitx_declare_unit:Nx \degreeCelsius
                {
                  \siunitx_print_text:n
                    { \@@_non_latin:n { "00B0 } } C
                }
           }
        }
        { \group_end: }
%    \end{macrocode}
%   For |\ohm|, there is a math mode symbol we can use, so there has to be
%   a mode-dependent definition.
%    \begin{macrocode}
    \@@_if_replace:NnT \ohm { "03A9 }
      {
        \siunitx_declare_unit:Nx \ohm
          {
            \exp_not:N \ifmmode
              \cs_if_exist:NTF \upOmega
                { \exp_not:N \upOmega }
                { \exp_not:N \Omega }
            \exp_not:N \else
              \siunitx_print_text:n
                {
                  \bool_lazy_or:nnTF
                    { \sys_if_engine_luatex_p: }
                    { \sys_if_engine_xetex_p: }
                    { \@@_non_latin:n { "03A9 } }
                    { \exp_not:N \textohm }
                }
            \exp_not:N \fi
          }
      }
%    \end{macrocode}
%   Only a text mode command is available for |\micro| in the standard
%   set up.
%    \begin{macrocode}
    \@@_if_replace:NnT \micro { "03BC }
      {
        \siunitx_declare_prefix:Nnx \micro { -6 }
          {
            \siunitx_print_text:n
              {
                \bool_lazy_or:nnTF
                  { \sys_if_engine_luatex_p: }
                  { \sys_if_engine_xetex_p: }
                  { \@@_non_latin:n { "00B5 } }
                  { \exp_not:N \textmu }
              }
          }
      }
  }
%    \end{macrocode}
%
%   For the times symbol, only \LuaTeX{} allows us to use the math mode symbol
%   directly. However, that likely won't follow the surrounding font appearance,
%   so in all cases we use the |TS1| version for text. Otherwise much the
%   same as |\textmu| support. It's hard to check for the product symbol, so
%   we just go with it an hope for the best!
%    \begin{macrocode}
\keys_set:nn { siunitx }
  { product-symbol  = \ifmmode \times \else \texttimes \fi }
\AtBeginDocument
  {
    \group_begin:
        \tl_set:Nn \l_@@_tmpa_tl
          { { } { } { 2 } { } { } { } { 1 } }
        \tl_set:Nx \l_@@_tmpa_tl
          { \siunitx_number_output:N \l_@@_tmpa_tl }
        \tl_set:Nn \l_@@_tmpb_tl { 2 \times 10 ^ { 1 } }
        \tl_if_eq:NNTF \l_@@_tmpa_tl \l_@@_tmpb_tl
          {
            \group_end:
            \keys_set:nn { siunitx }
              {
                exponent-product =
                  \ifmmode \times \else \texttimes \fi
              }
          }
          {
            \tl_set:Nn \l_@@_tmpb_tl { 2 \cdot 10 ^ { 1 } }
            \tl_if_eq:NNTF \l_@@_tmpa_tl \l_@@_tmpb_tl
              {
                \group_end:
                \keys_set:nn { siunitx }
                  {
                    exponent-product =
                      \ifmmode \cdot \else \textperiodcentered \fi
                  }
              }
              { \group_end: }
          }
    \group_begin:
      \tl_set:Nn \l_@@_tmpa_tl { \mathrm { m } \cdot \mathrm { s } }
      \tl_set:Nn \l_siunitx_unit_font_tl { \mathrm }
      \siunitx_unit_format:nN { m . s } \l_@@_tmpb_tl
      \tl_if_eq:NNTF \l_@@_tmpa_tl \l_@@_tmpb_tl
        {
          \group_end:
          \keys_set:nn { siunitx }
            {
              inter-unit-product =
                \ifmmode \cdot \else \textperiodcentered \fi
            }
        }
        { \group_end: }
  }
%    \end{macrocode}
%
% \subsection{Bookmark definitions}
%
% Inside PDF strings we disable the text printing function. The definition of
% |\ohm| is also reset as otherwise engine-dependent strings are generated
% (\XeTeX{} and \LuaTeX{} give different outcomes using for example
% |\textohm|).
%    \begin{macrocode}
\AtBeginDocument
  {
    \@ifpackageloaded { hyperref }
      {
        \exp_args:Nx \pdfstringdefDisableCommands
          {
            \cs_set_eq:NN \siunitx_print_text:n \exp_not:N \use:n
            \siunitx_declare_unit:Nn \exp_not:N \ohm
              { \@@_non_latin:n { "03A9 } }
          }
      }
      { }
  }
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex