summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/chemmacros/chemmacros.module.chemformula.code.tex
blob: d785ce6c7950ef96e69cf8d901e011cd499181af (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
% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `chemformula'
%
% --------------------------------------------------------------------------
% 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-2015 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{chemformula}{2015/09/19 integration of chemical formulas}

\RequirePackage{amstext}
\chemmacros_load_module:n {charges}

\msg_new:nnn {chemmacros} {unknown-formula-method}
  {
    You~ chose~ the~ formula~ method~ `#1'~ which~ is~ not~ available.~ Valid~
    methods~ are~ (the~ recommended~ choice)~ `chemformula'~ and~ `mhchem'.~
    I'm~ falling~ back~ to~ `chemformula'~ now.
  }

\bool_new:N \l__chemmacros_formula_method_set_bool

\tl_new:N \g_chemmacros_formula_method_tl
\tl_gset:Nn \g_chemmacros_formula_method_tl {chemformula}

\cs_new_protected:Npn \chemmacros_set_formula_method:n #1
  {
    \str_case:nnTF {#1}
      {
        {chemformula} {
          \chemmacros_integrate_package:nnn {} {chemformula} {2015/09/08}
          \tl_gset:Nn \g_chemmacros_formula_method_tl {chemformula}
        }
        {mhchem} {
          \chemmacros_integrate_package:nnn {version=4} {mhchem} {}
          \cs_set_protected:Npn \chemmacros_chemformula:n ##1 { \ce {##1} }
          \cs_set_protected:Npn \chemmacros_reaction:n ##1 { \ce {##1} }
          \chemmacros_after_module:nn {charges}
            {
              \cs_set_protected:Npn \chemmacros_elpair:n ##1 { \ce{##1} : }
              \cs_set_protected:Npn \chemmacros_plus:  { + }
              \cs_set_protected:Npn \chemmacros_minus: { - }
              \cs_set_protected:Npn \chemmacros_formal_plus: { \ensuremath{\oplus} }
              \cs_set_protected:Npn \chemmacros_formal_minus: { \ensuremath{\ominus} }
              \cs_set_protected:Npn \fplus  { \ensuremath{\oplus} }
              \cs_set_protected:Npn \fminus { \ensuremath{\ominus} }
            }
          \tl_gset:Nn \g_chemmacros_formula_method_tl {mhchem}
        }
      }
      { \bool_set_true:N \l__chemmacros_formula_method_set_bool }
      { \bool_set_false:N \l__chemmacros_formula_method_set_bool }
  }
\cs_generate_variant:Nn \chemmacros_set_formula_method:n {V}

% --------------------------------------------------------------------------
% let's make use of `chemformula' inside some of `chemmacros' commands:
\cs_new_protected:Npn \chemmacros_chemformula:n #1
  { \chemformula_chcpd:nn {} {#1} }
\cs_generate_variant:Nn \chemmacros_chemformula:n { x,V }

\cs_new_protected:Npn \chemmacros_reaction:n #1
  { \chemformula_ch:nn {} {#1} }

% for module writes prefering traditional 2e programming:
\cs_new_protected:Npn \chemmacros@formula #1 { \chemmacros_chemformula:n {#1} }
\cs_new_protected:Npn \chemmacros@reaction #1 { \chemmacros_reaction:n {#1} }

% --------------------------------------------------------------------------
\ChemCompatibilityFrom{5.1}
\keys_define:nn {chemmacros}
  {
    formula          .choices:nn =
      { chemformula , mhchem }
      { \chemmacros_set_formula_method:V \l_keys_choice_tl } ,
    formula / unknown .code:n  =
      \msg_warning:nnn {chemmacros} {unknown-formula-method} {#1}
      \tl_gset:Nn \g_chemmacros_formula_method_tl {chemformula}
  }
\EndChemCompatibility

\AtEndPreamble
  {
    \bool_if:NF \l__chemmacros_formula_method_set_bool
      { \chemmacros_set_formula_method:V \g_chemmacros_formula_method_tl }
  }

% --------------------------------------------------------------------------
\tex_endinput:D

2015/09/22 - new general option `method' which allows a choice between
             `chemformula' and `mhchem'