summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/siunitx/siunitx-command.dtx
blob: 20574f725238d4ffba1f724e26b3c0495b149dd9 (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
% \iffalse meta-comment
%
% File: siunitx-command.dtx Copyright (C) 2019,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}
\ProvideDocumentCommand\foreign{m}{\textit{#1}}
% The next line is needed so that \GetFileInfo will be able to pick up
% version data
\usepackage{siunitx}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{siunitx.sty}
%
% \title{^^A
%   \pkg{siunitx-command} -- Units as document command^^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}
%
% This submodule provides support for creating free-standing document commands
% for unit macros.
%
% \section{Creating units as document commands}
%
% \begin{function}{\siunitx_command_create:}
%   \begin{syntax}
%     \cs{siunitx_command_create:}
%   \end{syntax}
%   Maps over the list of know unit commands and creates the appropriate
%   document command to support them, as controlled by the options below.
% \end{function}
%
% \subsection{Key--value options}
%
% The options defined by this submodule are available within the \pkg{l3keys}
% |siunitx| tree.
%
% These options are all preamble-only.
%
% \begin{function}{free-standing-units}
%   \begin{syntax}
%     |free-standing-units| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch to determine whether free standing document commands are created
%   for symbolic units. This will include not only units themselves but also
%   prefixes, \foreign{etc.} The standard setting is |false|.
% \end{function}
%
% \begin{function}{overwrite-commands}
%   \begin{syntax}
%     |overwrite-commands| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch to determine whether when creating free standing document commands,
%   any existing document commands are overwritten. The standard setting is
%   |false|.
% \end{function}
%
% \begin{function}{space-before-unit}
%   \begin{syntax}
%     |space-before-unit| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch to determine whether a space is inserted before free standing
%   document commands. The standard setting is |false|.
% \end{function}
%
% \begin{function}{unit-optional-argument}
%   \begin{syntax}
%     |unit-optional-argument| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch to determine whether free standing document commands take an
%   optional argument (a number). The standard setting is |false|.
% \end{function}
%
% \begin{function}{use-xspace}
%   \begin{syntax}
%     |use-xspace| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch to determine whether free standing document commands use the
%   \pkg{xparse} package to insert space after the command names. The standard
%   setting is |false|. When set |true|, the \pkg{xparse} package will be
%   loaded at the start of the document if not already available.
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{siunitx-command} 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_command>
%    \end{macrocode}
%
% \begin{macro}{\l_@@_tmp_tl}
%    \begin{macrocode}
\tl_new:N \l_@@_tmp_tl
%    \end{macrocode}
% \end{macro}
%
% \subsection{Options}
%
% \begin{variable}
%   {
%     \l_@@_create_bool    ,
%     \l_@@_overwrite_bool ,
%     \l_@@_prespace_bool  ,
%     \l_@@_optarg_bool    ,
%     \l_@@_xspace_bool
%   }
%    \begin{macrocode}
\keys_define:nn { siunitx }
  {
    free-standing-units .bool_set:N =
      \l_@@_create_bool ,
    overwrite-commands .bool_set:N =
      \l_@@_overwrite_bool ,
    space-before-unit .bool_set:N =
      \l_@@_prespace_bool ,
    unit-optional-argument .bool_set:N =
      \l_@@_optarg_bool ,
    use-xspace .bool_set:N =
      \l_@@_xspace_bool
  }
%    \end{macrocode}
% \end{variable}
%
% These preamble-only options are all disabled at the start of the document.
%    \begin{macrocode}
\AtBeginDocument
  {
    \clist_map_inline:nn
      {
        free-standing-units    ,
        overwrite-commands     ,
        space-before-unit      ,
        unit-optional-argument ,
        use-xspace
      }
      {
        \keys_define:nn { siunitx }
          {
            #1 .code:n =
              { \msg_warning:nnn { siunitx } { option-preamble-only } {#1} }
          }
      }
  }
\msg_new:nnn { siunitx } { option-preamble-only }
  { Option~'#1'~only~available~in~the~preamble. }
%    \end{macrocode}
%
% \subsection{Creation of unit document commands}
%
% \begin{macro}{\siunitx_command_create:, \@@_create:}
% \begin{macro}{\@@_create:N}
%   Creating document commands is all done by a single function which is
%   set up using expansion: that way the tests are only run once. Other than
%   that, this is all just a question of picking up all the various routes.
%   Where the \pkg{soulpos} package is loaded \emph{after} \pkg{siunitx}, the
%   commands \cs{hl} and \cs{ul} will be created only after the hook is used.
%   The \pkg{soul} package creates those using \tn{newcommand}, so we have to
%   avoid an issue.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_command_create:
  {
    \bool_if:NT \l_@@_create_bool
      {
        \@@_create:
        \@ifpackageloaded { soulpos }
          {
            \@ifpackageloaded { soul }
              { }
              {
                \cs_undefine:N \hl
                \cs_undefine:N \ul
              }
          }
          { }
      }
%    \end{macrocode}
%   At the beginning of table cells and inside \texttt{x}-type expansion,
%   all symbolic units need to have \emph{some} definition. 
%    \begin{macrocode}
    \seq_map_inline:Nn \l_siunitx_unit_symbolic_seq
      {
        \cs_if_free:NT ##1
          { \cs_set_protected:Npn ##1 { \ERROR } }
      }
  }
\AtBeginDocument { \siunitx_command_create: }
\cs_new_protected:Npn \@@_create:
  {
    \bool_if:NT \l_@@_xspace_bool
      { \RequirePackage { xspace } }
    \bool_if:NT \l_@@_overwrite_bool
      {
        \seq_map_inline:Nn \l_siunitx_unit_symbolic_seq
          { \cs_undefine:N ##1 }
      }
    \cs_set_protected:Npx \@@_create:N ##1
      {
        \ProvideDocumentCommand ##1 { \bool_if:NT \l_@@_optarg_bool { o } }
          {
            \mode_leave_vertical:
            \group_begin:
              \bool_if:NTF \l_@@_optarg_bool
                { \exp_not:N \IfNoValueTF {####1} }
                { \use_i:nn }
                {
                  \siunitx_unit_options_apply:n {##1}
                  \bool_if:NT \l_@@_prespace_bool { \exp_not:N \  }
                  \siunitx_unit_format:nN {##1}
                    \exp_not:N \l_@@_tmp_tl
                  \siunitx_print_unit:V
                    \exp_not:N \l_@@_tmp_tl
                }
                { \siunitx_quantity:nn {####1} {##1} }
            \group_end:
            \bool_if:NT \l_@@_xspace_bool { \exp_not:N \xspace }
          }
      }
    \seq_map_function:NN \l_siunitx_unit_seq \@@_create:N
  }
\cs_new_protected:Npn \@@_create:N #1 { }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Standard settings for module options}
%
% Some of these follow naturally from the point of definition
% (\foreign{e.g.}~boolean variables are always |false| to begin with),
% but for clarity everything is set here.
%    \begin{macrocode}
\keys_set:nn { siunitx }
  {
    free-standing-units    = false ,
    overwrite-commands     = false ,
    space-before-unit      = false ,
    unit-optional-argument = false ,
    use-xspace             = false
  }
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex