summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/chemmacros/chemmacros.module.acid-base.code.tex
blob: 341a60fc585e0b641139c65187e81f125c39f6f0 (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
% --------------------------------------------------------------------------
%
% the CHEMMACROS package -- module: `acid-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--2020 Clemens Niederberger
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% 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 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Clemens Niederberger.
% --------------------------------------------------------------------------
\ChemModule{acid-base}{2018/02/22 acid/base}

\chemmacros_load_modules:n {charges}

% --------------------------------------------------------------------------
% equilibrium constants:

\tl_new:N  \l__chemmacros_equilibrium_constant_tl
\tl_set:Nn \l__chemmacros_equilibrium_constant_tl {K}
\prop_new:N \l__chemmacros_eq_constants_prop

\prg_new_conditional:Npnn \chemmacros_if_eq_constant:Nn #1#2 {T,F,TF}
  {
    \cs_if_exist:NTF #1
      {
        \tl_if_eq:xnTF
          { \prop_item:Nn \l__chemmacros_eq_constants_prop {#1} }
          {#2}
          { \prg_return_true: }
          { \prg_return_false: }
      }
      { \prg_return_false: }
  }
\cs_generate_variant:Nn \tl_if_eq:nnTF {x}

% #1: cs
% #2: translation id and option name
% #3: default translation
\cs_new_protected:Npn \chemmacros_define_eq_constant:Nnn #1#2#3
  {
    \chemmacros_if_eq_constant:NnF {#1} {#2}
      {
        \cs_new_protected:Npn #1 {}
        \prop_put:Nnn \l__chemmacros_eq_constants_prop {#1} {#2}
        \tl_new:c {l__chemmacros_#2_tl}
        \chemmacros_define_keys:nn {acid-base}
          { #2 .tl_set:c = l__chemmacros_#2_tl }
      }
    \tl_set:cn
      {l__chemmacros_#2_tl}
      { \chemmacros_translate:n {#2} }
    \chemmacros_if_compatibility:nnTF {>=} {5.7}
      { \chemmacros_declare_translation:nnn {#2} {fallback} {#3} }
      { \DeclareTranslationFallback {#2} {#3} }
    \cs_set_protected:Npn #1
      {
        \ensuremath
          {
            \chemmacros_bold:n
              {
                \l__chemmacros_equilibrium_constant_tl
                \c_math_subscript_token
                  { \tl_use:c {l__chemmacros_#2_tl} }
              }
          }
      }
  }

\NewChemMacroset {EqConstant} {mmm}
  { \chemmacros_define_eq_constant:Nnn #1 {#2} {#3} }

\NewChemEqConstant \Ka {K-acid}  { \mathrm{a} }
\NewChemEqConstant \Kb {K-base}  { \mathrm{b} }
\NewChemEqConstant \Kw {K-water} { \mathrm{w} }

\chemmacros_if_compatibility:nnT {>=} {5.4}
  {
    \chemmacros_define_keys:nn {acid-base}
      { eq-constant .tl_set:N = \l__chemmacros_equilibrium_constant_tl }
  }

\chemmacros_define_keys:nn {acid-base}
  {
    p-style           .choice: ,
    p-style / slanted .code:n   = \cs_set_eq:NN \chemmacros_p_style:n \textsl ,
    p-style / italics .code:n   = \cs_set_eq:NN \chemmacros_p_style:n \textit ,
    p-style / upright .code:n   = \cs_set_eq:NN \chemmacros_p_style:n \textup ,
    p-style           .initial:n = upright
  }

\cs_new_protected:Npn \chemmacros_p:n #1
  {
    \group_begin:
      \chemmacros_if_compatibility:nnTF {<=} {5.8b}
        { \mbox }
        { \chemmacros_text:n }
        {
          \chemmacros_p_style:n {p}
          \ensuremath {#1}
        }
    \group_end:
  }

\NewDocumentCommand \p  {m} { \chemmacros_p:n {#1} }
\NewDocumentCommand \pH  {} { \chemmacros_p:n { \chemmacros_chemformula:n {H} } }
\NewDocumentCommand \pOH {} { \chemmacros_p:n { \chemmacros_chemformula:n {OH} } }

\NewDocumentCommand \pKa {o}
  {
    \chemmacros_p:n
      {
        \Ka \IfNoValueF {#1}
          { {} \c_math_subscript_token { \chemmacros_bold:n {#1} } }
      }
  }

\NewDocumentCommand \pKb {o}
  {
    \chemmacros_p:n
      {
        \Kb \IfNoValueF {#1}
          { {} \c_math_subscript_token { \chemmacros_bold:n {#1} } }
      }
  }

% --------------------------------------------------------------------------
\ChemCompatibilityTo{5.6}
\DeclareTranslation {English} {K-acid}  {\mathrm{a}}
\DeclareTranslation {German}  {K-acid}  {\mathrm{s}}
\DeclareTranslation {Dutch}   {K-acid}  {\mathrm{z}}

\DeclareTranslation {English} {K-base}  {\mathrm{b}}

\DeclareTranslation {English} {K-water} {\mathrm{w}}
\EndChemCompatibility

\ChemCompatibilityFrom{5.6}
\chemmacros_declare_translations:nn {K-acid}
  {
    German = \mathrm {s} ,
    Dutch  = \mathrm {z}
  }
\EndChemCompatibility
% --------------------------------------------------------------------------
\file_input_stop:

2016/02/04 - \NewChemEqConstant and family, new option `eq-constant'
2016/04/27 - moved translations from lang module to acid-base module
2016/05/31 - adapt to changes in `lang' module
2018/02/22 - fix bug in \chemmacros_p:n (https://tex.stackexchange.com/q/416227/)