summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/chemmacros/chemmacros.module.base.code.tex
blob: 1f20afb1e25efa5254677caba0a67e66f7edbfb1 (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
% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `base'
%
% --------------------------------------------------------------------------
% 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-2016 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.
% --------------------------------------------------------------------------
\ChemModule{base}{2016/02/09 basic chemmacros module}

% for the document hooks
\ChemCompatibilityFrom{5.1}
\RequirePackage {etoolbox}
\EndChemCompatibility

% --------------------------------------------------------------------------
\msg_new:nnn {chemmacros} {command-deprecated}
  {
    The~ command~ \token_to_str:N #1 \c_space_tl is~ deprecated.~ Use~
    \token_to_str:N #2 \c_space_tl instead.
  }

% define a deprecated command with hints to the corresponding new one:
\cs_new_protected:Npn \chemmacros_define_deprecated:NN #1#2
  {
    \cs_set_protected:Npn #1
      {
        \msg_warning:nnnn {chemmacros} {command-deprecated} {#1} {#2}
        #2
      }
  }

\NewDocumentCommand \DeclareChemDeprecated {mm}
  { \chemmacros_define_deprecated:NN #1 #2 }

% --------------------------------------------------------------------------
% define a macro set:
% \NewChem... \RenewChem... \DeclareChem... \ProvideChem...
% #1: macro names
% #2: arg spec
% #3: internal command call
\cs_new_protected:Npn \chemmacros_new_macroset:nnn #1#2#3
  {
    \exp_args:Nc \NewDocumentCommand
      {
        NewChem
        \str_upper_case:f { \tl_head:n {#1} }
        \tl_tail:n {#1}
      }
      {#2}
      {
        \cs_if_free:NTF ##1
          { #3 }
          { \msg_error:nnn {chemmacros} {new-#1} {##1} }
      }
    \exp_args:Nc \NewDocumentCommand
      {
        RenewChem
        \str_upper_case:f { \tl_head:n {#1} }
        \tl_tail:n {#1}
      }
      {#2}
      {
        \cs_if_free:NTF ##1
          { \msg_error:nnn {chemmacros} {renew-#1} {##1} }
          { #3 }
      }
    \exp_args:Nc \NewDocumentCommand
      {
        DeclareChem
        \str_upper_case:f { \tl_head:n {#1} }
        \tl_tail:n {#1}
      }
      {#2}
      {#3}
    \exp_args:Nc \NewDocumentCommand
      {
        ProvideChem
        \str_upper_case:f { \tl_head:n {#1} }
        \tl_tail:n {#1}
      }
      {#2}
      { \cs_if_free:NT ##1 { #3 } }
    \msg_new:nnn {chemmacros} {new-#1}
      {
        You've~ tried~ to~ define~ a~ #1~ with~ \token_to_str:N
        \NewChem #1 ,~ but~ the~ command~ sequence~ \token_to_str:N ##1
        \c_space_tl already~ exists.~ Please~ choose~ another~ name.
      }
    \msg_new:nnn {chemmacros} {renew-#1}
      {
        You've~ tried~ to~ renew~ the~ #1~ \token_to_str:N ##1 ,~ but~
        it~ doesn't~ exist.
      }
  }

\cs_new_protected:Npn \chemmacros_new_environment_macroset:nnn #1#2#3
  {
    \exp_args:Nc \NewDocumentCommand {NewChem#1} {#2}
      {
        \bool_if:nTF { \cs_if_free_p:c {##1} && \cs_if_free_p:c {end##1} }
          { #3 }
          { \msg_error:nnn {chemmacros} {new-#1} {##1} }
      }
    \exp_args:Nc \NewDocumentCommand {RenewChem#1} {#2}
      {
        \bool_if:nTF { \cs_if_free_p:c {##1} || \cs_if_free_p:c {end##1} }
          { \msg_error:nnn {chemmacros} {renew-#1} {##1} }
          { #3 }
      }
    \exp_args:Nc \NewDocumentCommand {DeclareChem#1} {#2} {#3}
    \exp_args:Nc \NewDocumentCommand {ProvideChem#1} {#2}
      {
        \bool_if:nT { \cs_if_free_p:c {##1} && \cs_if_free_p:c {end##1} }
          { #3 }
      }
    \msg_new:nnn {chemmacros} {new-#1}
      {
        You've~ tried~ to~ define~ a~ #1~ with~ \token_to_str:N
        \NewChem #1 ,~ but~ the~ command~ sequence~ \exp_not:c {##1}
        \c_space_tl already~ exists.~ Please~ choose~ another~ name.
      }
    \msg_new:nnn {chemmacros} {renew-#1}
      { You've~ tried~ to~ renew~ the~ #1~ ##1,~ but~ it~ doesn't~ exist. }
  }

\NewDocumentCommand \NewChemMacroset {smmm}
  {
    \IfBooleanTF {#1}
      { \chemmacros_new_environment_macroset:nnn {#2} {#3} {#4} }
      { \chemmacros_new_macroset:nnn {#2} {#3} {#4} }
  }
  
% --------------------------------------------------------------------------
% basic tools:
\cs_new:Npn \chemmacros_remove_backslash:N #1
  { \exp_after:wN \use_none:n \token_to_str:N #1 }

\cs_new_eq:NN \chemmacros_ignore_spaces: \tex_ignorespaces:D

\cs_new_protected:Npn \chemmacros_leave_vmode: { \hbox_unpack:N \c_empty_box }
\cs_new_protected:Npn \chemmacros_nobreak:     { \tex_penalty:D \c_ten_thousand }
\cs_new_protected:Npn \chemmacros_allow_break: { \tex_penalty:D \c_zero }
\cs_new_protected:Npn \chemmacros_skip_nobreak:N #1
  {
    \chemmacros_nobreak:
    \skip_horizontal:N #1
    \chemmacros_nobreak:
  }

% do I use those at all?
\prg_new_conditional:Npnn \chemmacros_tex_if:nn #1#2 {p,T,F,TF}
  { \use:c { if #1 } #2 \prg_return_true: \else: \prg_return_false: \fi: }

\prg_new_conditional:Npnn \chemmacros_latex_if:n #1 {p,T,F,TF}
  { #1 { \prg_return_true: } { \prg_return_false: } }

% this is used at least in the `isotope' module
\prg_new_conditional:Npnn \chemmacros_if_is_int:n #1 {p,T,F,TF}
  {
    \if_charcode:w ! \if_int_compare:w 9 < 1#1 ! \else: _ \fi:
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }

\prg_new_conditional:Npnn \chemmacros_if_loaded:nn #1#2 {p,T,F,TF}
  {
    \use:c {chemmacros_if_#1_loaded:nTF} {#2}
      { \prg_return_true: }
      { \prg_return_false: }
  }
  
\prg_new_conditional:Npnn \chemmacros_if_package_loaded:n #1 {p,T,F,TF}
  {
    \cs_if_exist:cTF {ver@#1.sty}
      { \prg_return_true: }
      { \prg_return_false: }
  }

\prg_new_conditional:Npnn \chemmacros_if_class_loaded:n #1 {p,T,F,TF}
  {
    \cs_if_exist:cTF {ver@#1.cls}
      { \prg_return_true: }
      { \prg_return_false: }
  }

\ChemCompatibilityFrom{5.2}
% this is still true \AtBeginDocument:
\prg_new_conditional:Npnn \chemmacros_if_preamble: {p,T,F,TF}
  {
    \cs_if_eq:NNTF \@onlypreamble \@notprerr
      { \prg_return_false: }
      { \prg_return_true: }
  }

% this is still false \AtBeginDocument:
\prg_new_conditional:Npnn \chemmacros_if_document: {p,T,F,TF}
  {
    \bool_if:NTF \l__chemmacros_in_document_bool
      { \prg_return_true: }
      { \prg_return_false: }
  }
\EndChemCompatibility

% --------------------------------------------------------------------------
% some checks at begin document:
\bool_new:N \l__chemmacros_in_document_bool
\bool_new:N \l__chemmacros_chemstyle_bool

\chemmacros_if_compatiblity:nnTF {>=} {5.2}
  { \AfterEndPreamble }
  { \AtBeginDocument }
  { \bool_set_true:N \l__chemmacros_in_document_bool }

\AtBeginDocument
  {
    \@ifpackageloaded { chemstyle }
      { \bool_set_true:N \l__chemmacros_chemstyle_bool }
      { \bool_set_false:N \l__chemmacros_chemstyle_bool }
  }

% --------------------------------------------------------------------------
% detection and handling of bold face:
\RequirePackage{bm}

\prg_new_protected_conditional:Npnn \chemmacros_if_bold: {T,F,TF}
  {
    \seq_if_in:NVTF \l__chemmacros_if_bf_seq \f@series
      { \prg_return_true: }
      { \prg_return_false: }
  }

\seq_new:N  \l__chemmacros_if_bf_seq

\seq_set_split:Nnn \l__chemmacros_if_bf_seq { , }
  {
    b , bc , bm , bx , bux ,
    eb , ebc , ebx , mb ,
    sb , sbc , sbx ,
    ub , ubc , ubx
  }

\cs_new_protected:Npn \chemmacros_bold:n #1
  {
    \chemmacros_if_bold:TF
      {
        \mode_if_math:TF
          { \bm {#1} }
          { \textbf {#1} }
      }
      {#1}
  }

\cs_new_protected:Npn \chemmacros_detect_bold:n
  { \error \chemmacros_bold:n }

% --------------------------------------------------------------------------
\RequirePackage{amstext}

\cs_new_protected:Npn \chemmacros_text:n #1
  { \mode_if_math:TF { \text {#1} } {#1} }
\cs_generate_variant:Nn \chemmacros_text:n { V }

\cs_new_protected:Npn \chemmacros_math:n #1
  { \chemmacros_text:n { \c_math_toggle_token #1 \c_math_toggle_token } }
\cs_generate_variant:Nn \chemmacros_math:n { V }

% --------------------------------------------------------------------------
% a setup command:
\NewDocumentCommand \chemsetup {om}
  {
    \IfNoValueTF {#1}
      { \keys_set:nn {chemmacros}    {#2} }
      { \keys_set:nn {chemmacros/#1} {#2} }
  }

\keys_define:nn {chemmacros}
  { modules .code:n = \chemmacros_load_modules:n {#1} }

% --------------------------------------------------------------------------

% load package #1 and add its options to chemmacros
%   this requires that package #1 defines its options with l3keys using its
%   name as module
\cs_new_protected:Npn \chemmacros_integrate_package:nnn #1#2#3
  {
    \tl_if_blank:nTF {#3}
      {
        \tl_if_blank:nTF {#1}
          { \RequirePackage {#2} }
          { \RequirePackage [#1] {#2} }
      }
      {
        \tl_if_blank:nTF {#1}
          { \RequirePackage {#2} [#3] }
          { \RequirePackage [#1] {#2} [#3] }
      }
    \keys_define:nn {chemmacros}
      {
        #2 .choice: ,
        #2 / unknown .code:n =
          \keys_set:no {#2} { \l_keys_key_tl = ##1 }
      }
  }

% --------------------------------------------------------------------------

\chemmacros_if_compatiblity:nnT {>=} {5.2}
  { \chemmacros_load_module:n {errorcheck} }

% --------------------------------------------------------------------------

\tex_endinput:D

2015/09/23 - new conditionals \chemmacros_if_preamble:TF and
             \chemmacros_if_document:TF
2015/10/14 - load `errorcheck' module
2016/02/09 - starred version of \NewChemMacroset