summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/chemmacros/chemmacros5.sty
blob: e82088aa277898c92801917d92510f4208ab339a (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
% --------------------------------------------------------------------------
% the CHEMMACROS package
%
%   comprehensive support for typesetting chemistry documents
%
% --------------------------------------------------------------------------
% Clemens Niederberger
% --------------------------------------------------------------------------
% https://github.org/cgnieder/chemmacros/
% contact@mychemistry.eu
% --------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% --------------------------------------------------------------------------
% Copyright 2011--2020 Clemens Niederberger
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Clemens Niederberger.
% --------------------------------------------------------------------------
% The chemmacros package consists of the following files
%  - chemmacros.sty, chemmacros4.sty, chemmacros5.sty,
%  - chemmacros_en.tex, chemmacros_en.pdf,
%  - quite a number of module files following the naming scheme
%    chemmacros.module.*.code.tex
%  - README
 % --------------------------------------------------------------------------
\RequirePackage{expl3,xparse,l3keys2e}
\ExplSyntaxOn

% --------------------------------------------------------------------------
% package information:
\ProvidesExplPackage
  {chemmacros5}
  {\c_chemmacros_date_tl}
  {\c_chemmacros_version_tl}
  {\c_chemmacros_info_tl \c_space_tl (CN)}

% --------------------------------------------------------------------------
\msg_new:nnn {chemmacros} {package-option}
  {
    Unknown~ option~ `#1'!~ Since~ v5.0~ chemmacros~ has~ no~ package~
    options~ any~ more!
  }

\msg_new:nnn {chemmacros} {module-not-available}
  {
    The~ module~ `#1'~ is~ only~ available~ in~ compatibility~ mode~ #2~ or~
    higher.
  }

\cs_generate_variant:Nn \msg_warning:nnn { nnV }

\keys_define:nn {chemmacros/package-options}
  {
    compatibility .code:n = ,
    unknown       .code:n =
      \msg_warning:nnV {chemmacros} {package-option} \CurrentOption
  }

\ProcessKeysPackageOptions {chemmacros/package-options}

% --------------------------------------------------------------------------
% messages:
\msg_new:nnn {chemmacros} {module-missing}
  {
    You've~ requested~ the~ chemmacros~ module~ `#1'~ but~ it~ appears~ to~
    be~ missing~ on~ your~ system.~ Maybe~ you've~ misspelled~ the~ name?~
    Loading~ the~ module~ will~ abort~ \msg_line_context:
  }

\msg_new:nnn {chemmacros} {loading-module}
  {
    (chemmacros) \c_space_tl \c_space_tl \c_space_tl \c_space_tl
    Loading~ module~ `#1'...
  }

% --------------------------------------------------------------------------
% variants of kernel functions:
\cs_generate_variant:Nn \tl_if_eq:nnTF       {xx,V}
\cs_generate_variant:Nn \seq_set_split:Nnn   {Nnx}
\cs_generate_variant:Nn \prop_item:Nn        {NV}
\cs_generate_variant:Nn \seq_item:Nn         {cV}
\cs_generate_variant:Nn \int_to_arabic:n     {V}
\cs_generate_variant:Nn \tl_replace_once:Nnn {NnV}
\cs_generate_variant:Nn \prop_gput:Nnn       {cxx,NVf}
\cs_generate_variant:Nn \msg_error:nnn       {nnV}
\cs_generate_variant:Nn \msg_warning:nnn     {nnV}
\cs_generate_variant:Nn \msg_warning:nnnn    {nnV}
\cs_generate_variant:Nn \vbox_set:Nn         {NV}
\cs_generate_variant:Nn \keys_define:nn      {x} 
\cs_generate_variant:Nn \keys_set:nn         {x,nx}

% --------------------------------------------------------------------------
% temporary variables:
\tl_new:N    \l__chemmacros_tmpa_tl
\tl_new:N    \l__chemmacros_tmpb_tl
\tl_new:N    \l__chemmacros_tmpc_tl
\tl_new:N    \l__chemmacros_tmpd_tl

\bool_new:N  \l__chemmacros_tmpa_bool
\bool_new:N  \l__chemmacros_tmpb_bool
\bool_new:N  \l__chemmacros_tmpc_bool

\dim_new:N   \l__chemmacros_tmpa_dim
\dim_new:N   \l__chemmacros_tmpb_dim
\dim_new:N   \l__chemmacros_tmpc_dim

\seq_new:N   \l__chemmacros_tmpa_seq
\seq_new:N   \l__chemmacros_tmpb_seq
\seq_new:N   \l__chemmacros_tmpc_seq

\int_new:N   \l__chemmacros_tmpa_int
\int_new:N   \l__chemmacros_tmpb_int
\int_new:N   \l__chemmacros_tmpc_int

\box_new:N   \l__chemmacros_tmpa_box
\box_new:N   \l__chemmacros_tmpb_box
\box_new:N   \l__chemmacros_tmpc_box

\clist_new:N \l__chemmacros_tmpa_clist
\clist_new:N \l__chemmacros_tmpb_clist
\clist_new:N \l__chemmacros_tmpc_clist

\str_new:N   \l__chemmacros_tmpa_str
\str_new:N   \l__chemmacros_tmpb_str
\str_new:N   \l__chemmacros_tmpc_str

% --------------------------------------------------------------------------
% module mechanism:
\tl_const:Nn \c__chemmacros_module_extension_tl {code.tex}
\tl_const:Nn \c__chemmacros_module_prefix_tl    {chemmacros.module}

\seq_new:N \g__chemmacros_loaded_modules_seq
\prop_new:N \g__chemmacros_loaded_modules_prop

\cs_new_protected:Npn \chemmacros_load_modules:n #1
  { \clist_map_inline:nn {#1} { \chemmacros_load_module:n {##1} } }

\cs_new_protected:Npn \chemmacros_load_module:n #1
  {
    \tl_set:Nx \l__chemmacros_tmpa_tl { \tl_trim_spaces:n {#1} }
    \__chemmacros_load_module:V \l__chemmacros_tmpa_tl
  }

\cs_new_protected:Npn \__chemmacros_load_module:n #1
  {
    \chemmacros_if_module_loaded:nF {#1}
      {
        \chemmacros_if_module_exist:nTF {#1}
          {
            \__chemmacros_use_module_hook:nn {#1} {before}
            \msg_log:nnn {chemmacros} {loading-module} {#1}
            \@onefilewithoptions
              {\c__chemmacros_module_prefix_tl.#1}[][]
              \c__chemmacros_module_extension_tl
            \__chemmacros_use_module_hook:nn {#1} {after}
          }
          { \msg_error:nnn {chemmacros} {module-missing} {#1} }
      }
  }
\cs_generate_variant:Nn \__chemmacros_load_module:n { V }

\prg_new_conditional:Npnn \chemmacros_if_module_loaded:n #1 {p,T,F,TF}
  {
    \seq_if_in:NnTF \g__chemmacros_loaded_modules_seq {#1}
      { \prg_return_true: }
      { \prg_return_false: }
  }

\prg_new_conditional:Npnn \chemmacros_if_module_exist:n #1 {p,T,F,TF}
  {
    \file_if_exist:nTF
      {\c__chemmacros_module_prefix_tl.#1.\c__chemmacros_module_extension_tl}
      { \prg_return_true: }
      { \prg_return_false: }
  }

% --------------------------------------------------------------------------
\ChemCompatibilityFrom{5.1}
% module hooks:
% #1: module
% #2: code
\cs_new_protected:Npn \chemmacros_after_module:nn #1#2
  {
    \chemmacros_if_module_loaded:nTF {#1}
      {#2}
      { \__chemmacros_add_to_module_hook:nnn {#1} {after} {#2} }
  }

% #1: module
% #2: code
\cs_new_protected:Npn \chemmacros_before_module:nn #1#2
  {
    \chemmacros_if_module_loaded:nF {#1}
      { \__chemmacros_add_to_module_hook:nnn {#1} {before} {#2} }
  }
\EndChemCompatibility

% #1: module
% #2: hook name
% #3: code
\cs_new_protected:Npn \__chemmacros_add_to_module_hook:nnn #1#2#3
  {
    \chemmacros_if_module_loaded:nF {#1}
      {
        \tl_if_exist:cF {g__chemmacros_module_#1_#2_hook_tl}
          { \tl_new:c {g__chemmacros_module_#1_#2_hook_tl} }
        \tl_gput_right:cn {g__chemmacros_module_#1_#2_hook_tl} {#3}
      }
  }

% #1: module
% #2: hook name
\cs_new_protected:Npn \__chemmacros_use_module_hook:nn #1#2
  {
    \tl_if_exist:cT {g__chemmacros_module_#1_#2_hook_tl}
      {
        \tl_use:c {g__chemmacros_module_#1_#2_hook_tl}
        \tl_gclear:c {g__chemmacros_module_#1_#2_hook_tl}
      }
  }

% --------------------------------------------------------------------------
\cs_new_protected:Npn \__chemmacros_module:nnnn #1#2#3#4
  {
    \chemmacros_if_compatibility:nnF {>=} {#4}
      {
        \msg_warning:nnnn {chemmacros} {module-not-available} {#2} {#4}
        \file_input_stop:
      }
    \seq_gput_right:Nn \g__chemmacros_loaded_modules_seq {#2}
    \prop_gput:Nnn \g__chemmacros_loaded_modules_prop {#2} {#3}
    \ProvidesFile
      {\c__chemmacros_module_prefix_tl.#2.\c__chemmacros_module_extension_tl}
      [
        \c_chemmacros_date_tl
        \c_space_tl
        v \c_chemmacros_version_tl
        \c_space_tl
        chemmacros~ module~ `#2'~ #3
      ]
    \bool_if:nF {#1} { \ExplSyntaxOn }
  }
  
% define a chemmacros module:
% standard modules are written in the expl3 programming environment, the
% starred version only uses a 2e programming environment, i.e., with @ as a
% letter
% #1: boolean
% #2: name
% #3: description
\NewDocumentCommand \ChemModule {smmO{5.0}}
  { \__chemmacros_module:nnnn {#1} {#2} {#3} {#4} }

% --------------------------------------------------------------------------
% command for loading modules:
\NewDocumentCommand \usechemmodule {m} { \chemmacros_load_modules:n {#1} }

% --------------------------------------------------------------------------
% load default modules:
\chemmacros_load_modules:n
  {
    % required modules:
    base,
    errorcheck,
    lang, greek,
    chemformula,
    % preloaded modules:
    acid-base,
    symbols,
    charges,
    particles,
    phases ,
    nomenclature
  }

% --------------------------------------------------------------------------
\file_input_stop:

% --------------------------------------------------------------------------
Modules: (* = preloaded)
 - internal modules:
   - base *
   - errorcheck *
   - lang *
   - greek *
   - chemformula *
   - tikz
 - user modules:
   - acid-base *
   - all
   - charges *
   - isotopes
   - mechanisms
   - newman
   - nomenclature *
   - orbital
   - particles *
   - phases *
   - reactions
   - redox
   - scheme
   - spectroscopy
   - symbols *
   - thermodynamics
   - units
   - xfrac

% --------------------------------------------------------------------------
Version history
2015/08/29 - initial release v5.0
2015/09/23 - add module hooks `before' and `after'
           - allow to give minimal compatibility version to a module: this
             should be the first version when a module is published and
             shouldn't be changed any more; loading the corresponding module
             will abort when compatibility is too low
           - store loaded modules in a seq and in a prop
2015/10/14 - bug fix in the module hook mechanism
           - bug fix in \chemmacros_if_module_loaded:n
           - new `errorcheck' module
2020/02/03 - add temporary string variables