summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/chemmacros/chemformula.sty
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/latex/chemmacros/chemformula.sty')
-rw-r--r--Master/texmf-dist/tex/latex/chemmacros/chemformula.sty397
1 files changed, 236 insertions, 161 deletions
diff --git a/Master/texmf-dist/tex/latex/chemmacros/chemformula.sty b/Master/texmf-dist/tex/latex/chemmacros/chemformula.sty
index 630992621cd..c8c81b62add 100644
--- a/Master/texmf-dist/tex/latex/chemmacros/chemformula.sty
+++ b/Master/texmf-dist/tex/latex/chemmacros/chemformula.sty
@@ -155,6 +155,12 @@
\tl_new:N \l__chemformula_radical_style_tl
\dim_new:N \l__chemformula_radical_radius_dim
\dim_set:Nn \l__chemformula_radical_radius_dim { .2ex }
+\dim_new:N \l__chemformula_radical_hshift_dim
+\dim_set:Nn \l__chemformula_radical_hshift_dim { .3ex }
+\dim_new:N \l__chemformula_radical_vshift_dim
+\dim_set:Nn \l__chemformula_radical_vshift_dim { .7ex }
+\dim_new:N \l__chemformula_radical_space_dim
+\dim_set:Nn \l__chemformula_radical_space_dim { .3ex }
\cs_new:Npn \chemformula_radical:
{
@@ -166,13 +172,24 @@
inner~sep=0pt,
outer~sep=0pt
}
- { \fill (0,0)++(.3ex,.7ex) circle (\l__chemformula_radical_radius_dim) ; }
+ {
+ \fill (0,0)
+ ++(\l__chemformula_radical_hshift_dim,\l__chemformula_radical_vshift_dim)
+ circle (\l__chemformula_radical_radius_dim) ;
+ }
+ \skip_horizontal:N \l__chemformula_radical_space_dim
}
-\keys_define:nn { chemformula }
+\keys_define:nn { chemmacros / chemformula }
{
+ % TikZ style:
radical-style .tl_set:N = \l__chemformula_radical_style_tl ,
- radical-radius .dim_set:N = \l__chemformula_radical_radius_dim
+ radical-radius .dim_set:N = \l__chemformula_radical_radius_dim ,
+ % shift before:
+ radical-hshift .dim_set:N = \l__chemformula_radical_hshift_dim ,
+ radical-vshift .dim_set:N = \l__chemformula_radical_vshift_dim ,
+ % space after:
+ radical-space .dim_set:N = \l__chemformula_radical_space_dim
}
\cs_new:Npn \chemformula_adduct:
@@ -281,27 +298,23 @@
\let\pgf@arrows@check@already\savedef}
%-----------------------------------------------------------------------------%
-% dimension variables
-\dim_new:N \l__chemformula_arrow_length_dim
-
-\dim_new:N \l__chemformula_arrow_label_offset_dim
+% variables
+\dim_new:N \l__chemformula_arrow_length_dim
+\dim_new:N \l__chemformula_arrow_label_offset_dim
\dim_set:Nn \l__chemformula_arrow_label_offset_dim { 2pt }
-
-\dim_new:N \l__chemformula_arrow_shortage_dim
-\tl_new:N \l__chemformula_arrow_ratio_tl
-\tl_set:Nn \l__chemformula_arrow_ratio_tl { .6 }
-
-\dim_new:N \l__chemformula_arrow_offset_dim
+\dim_new:N \l__chemformula_arrow_shortage_dim
+\dim_new:N \l__chemformula_arrow_offset_dim
\dim_set:Nn \l__chemformula_arrow_offset_dim { .75em }
-
-\dim_new:N \l__chemformula_compound_sep_dim
+\dim_new:N \l__chemformula_compound_sep_dim
\dim_set:Nn \l__chemformula_compound_sep_dim { .5em }
-
-\dim_new:N \l__chemformula_arrow_yshift_dim
+\dim_new:N \l__chemformula_arrow_yshift_dim
\dim_set:Nn \l__chemformula_arrow_yshift_dim { 0pt }
+\tl_new:N \l__chemformula_arrow_ratio_tl
+\tl_set:Nn \l__chemformula_arrow_ratio_tl { .6 }
+
% determine length in dependency of labels
-\cs_new:Npn \chemformula_determine_arrow_length:NN #1#2
+\cs_new_protected:Npn \chemformula_determine_arrow_length:NN #1#2
{
\box_set_eq:NN \l__chemformula_tmpa_box #1
\dim_set:Nn \l__chemformula_tmpa_dim { \box_wd:N \l__chemformula_tmpa_box }
@@ -346,14 +359,14 @@
\box_new:N \l__chemformula_arrow_arg_ii_box
% read optional arguments
-\cs_new_nopar:Npn \l__chemformula_arrow_read_args:w
+\cs_new:Npn \l__chemformula_arrow_read_args:w
{
\peek_meaning:NTF [
{ \l__chemformula_arrow_read_args_aux_i:w }
{ \l__chemformula_arrow_read_args_aux_iii: }
}
-\cs_new_nopar:Npn \l__chemformula_arrow_read_args_aux_i:w [#1]
+\cs_new_protected:Npn \l__chemformula_arrow_read_args_aux_i:w [#1]
{
\hbox_set:Nn \l__chemformula_arrow_arg_i_box
{ \tl_use:N \l__chemformula_arrow_label_style_tl #1 }
@@ -362,14 +375,14 @@
{ \l__chemformula_arrow_read_args_aux_iii: }
}
-\cs_new_nopar:Npn \l__chemformula_arrow_read_args_aux_ii:w [#1]
+\cs_new_protected:Npn \l__chemformula_arrow_read_args_aux_ii:w [#1]
{
\hbox_set:Nn \l__chemformula_arrow_arg_ii_box
{ \tl_use:N \l__chemformula_arrow_label_style_tl #1 }
\l__chemformula_arrow_read_args_aux_iii:
}
-\cs_new_nopar:Npn \l__chemformula_arrow_read_args_aux_iii:
+\cs_new:Npn \l__chemformula_arrow_read_args_aux_iii:
{
% now we have both arguments do the actual drawing:
\prop_get:NVN \l__chemformula_arrows_code_prop
@@ -380,7 +393,7 @@
% get arrows (for the use in \ch to replace the symbol)
% #1: symbol
-\cs_new_nopar:Npn \chemformula_arrows_get:n #1
+\cs_new_protected:Npn \chemformula_arrows_get:n #1
{
% first clear the arguments
\box_clear:N \l__chemformula_arrow_arg_i_box
@@ -391,11 +404,10 @@
}
% draw the arrows
-\cs_new_nopar:Npn \chemformula_arrow_draw:n #1
+\cs_new:Npn \chemformula_arrow_draw:n #1
{
\chemmacros_allow_break:
% prepare arrow code for drawing:
- % TODO: flexible Pfeilspitzen, TikZ-Keys ermöglichen
\tl_set_rescan:Nnn \l__chemformula_tmpc_tl
{ \char_set_catcode_letter:N \_ } { #1 }
% determine length of the arrow
@@ -536,7 +548,7 @@
%-----------------------------------------------------------------------------%
\seq_new:N \l__chemformula_input_seq
% MAIN COMMAND
-\cs_new:Npn \chemformula_ch_main:n #1
+\cs_new_protected:Npn \chemformula_ch_main:n #1
{
\group_begin:
\seq_set_split:Nnn \l__chemformula_input_seq { ~ } { #1 }
@@ -561,7 +573,7 @@
\group_end:
}
-\cs_new:Npn \chemformula_ch:nn #1#2
+\cs_new_protected:Npn \chemformula_ch:nn #1#2
{
\group_align_safe_begin:
\chemmacros_leave_vmode:
@@ -576,7 +588,7 @@
% Trickery to use \ch inside align environment:
% bypass \\:
-\cs_new:Npn \chemformula_ch_aux_i:w #1 \\ #2 \q_stop
+\cs_new_protected:Npn \chemformula_ch_aux_i:w #1 \\ #2 \q_stop
{
\chemformula_ch_aux_ii:w #1 & \q_no_value \q_stop
\quark_if_no_value:nF { #2 }
@@ -587,7 +599,7 @@
}
% bypass &:
-\cs_new:Npn \chemformula_ch_aux_ii:w #1 & #2 \q_stop
+\cs_new_protected:Npn \chemformula_ch_aux_ii:w #1 & #2 \q_stop
{
\chemformula_ch_aux_iii:w #1 \label{} \q_no_value \q_stop
\quark_if_no_value:nF { #2 }
@@ -598,7 +610,7 @@
}
% bypass \label:
-\cs_new:Npn \chemformula_ch_aux_iii:w #1 \label#2 #3 \q_stop
+\cs_new_protected:Npn \chemformula_ch_aux_iii:w #1 \label#2 #3 \q_stop
{
\chemformula_ch_aux_iv:w #1 \tag{} \q_no_value \q_stop
\quark_if_no_value:nF { #3 }
@@ -609,7 +621,7 @@
}
% bypass \tag:
-\cs_new:Npn \chemformula_ch_aux_iv:w #1 \tag#2 #3 \q_stop
+\cs_new_protected:Npn \chemformula_ch_aux_iv:w #1 \tag#2 #3 \q_stop
{
\chemformula_ch_aux_v:w #1 \intertext{} \q_no_value \q_stop
\quark_if_no_value:nF { #3 }
@@ -620,7 +632,7 @@
}
% bypass \intertext:
-\cs_new:Npn \chemformula_ch_aux_v:w #1 \intertext#2 #3 \q_stop
+\cs_new_protected:Npn \chemformula_ch_aux_v:w #1 \intertext#2 #3 \q_stop
{
\tl_gput_right:Nn \g__chemformula_output_tl
{ \chemformula_ch_main:n { #1 } }
@@ -651,9 +663,9 @@
%-----------------------------------------------------------------------------%
% arrow generation & detection
-\cs_set:Nn \chemformula_generate_arrows:V
+\cs_new:Npn \chemformula_generate_arrows:n #1
{
- \tl_set:No \l__chemformula_tmpa_tl { #1 }
+ \tl_set:Nn \l__chemformula_tmpa_tl { #1 }
\seq_map_inline:Nn \l__chemformula_arrows_type_seq
{
\tl_replace_all:Nnn \l__chemformula_tmpa_tl
@@ -661,8 +673,9 @@
}
\tl_set_eq:NN \l__chemformula_output_tl \l__chemformula_tmpa_tl
}
+\cs_generate_variant:Nn \chemformula_generate_arrows:n { V }
-\cs_new:Npn \chemformula_detect_arrows:n #1
+\cs_new_protected:Npn \chemformula_detect_arrows:n #1
{
\bool_set_false:N \l__chemformula_is_arrow_bool
\prop_map_inline:Nn \l__chemformula_arrows_code_prop
@@ -700,7 +713,7 @@
\)
}
-\cs_new:Npn \chemformula_generate_name:n #1
+\cs_new_protected:Npn \chemformula_generate_name:n #1
{
\tl_set:Nn \l__chemformula_tmpa_tl { #1 }
\tl_if_in:VnT \l__chemformula_tmpa_tl { ! }
@@ -709,7 +722,7 @@
}
\cs_generate_variant:Nn \chemformula_generate_name:n { V }
-\cs_new:Npn \chemformula_detect_name:n #1
+\cs_new_protected:Npn \chemformula_detect_name:n #1
{
\bool_set_false:N \l__chemformula_is_name_bool
\tl_set:Nn \l__chemformula_tmpa_tl { #1 }
@@ -729,10 +742,13 @@
\bool_new:N \l__chemformula_is_down_bool
\bool_new:N \l__chemformula_is_arrow_bool
\bool_new:N \l__chemformula_is_name_bool
+\bool_new:N \l__chemformula_stoich_parse_iupac_bool
\bool_new:N \l__chemformula_first_last_text_bool
\bool_new:N \l__chemformula_first_last_double_bool
\bool_new:N \l__chemformula_first_last_single_bool
\bool_new:N \l__chemformula_first_last_math_bool
+\bool_new:N \l__chemformula_first_last_dollar_bool
+\bool_new:N \l__chemformula_first_last_mathbraces_bool
\bool_new:N \l__chemformula_number_style_math_bool
\bool_new:N \l__chemformula_charge_style_math_bool
\bool_new:N \l__chemformula_sub_bool
@@ -913,7 +929,7 @@
\chemmacros_skip_nobreak:N \l__chemformula_bond_space_dim
}
-\cs_new:Npn \chemformula_bond_draw:n #1
+\cs_new_protected:Npn \chemformula_bond_draw:n #1
{
\tl_set_rescan:Nnn \l__chemformula_tmpa_tl {} { #1 }
\exp_args:NNo \prop_get:Nn \l__chemformula_bonds_prop
@@ -1046,11 +1062,11 @@
\cs_generate_variant:Nn \chemformula_charge_style:n { V }
% subscripts
-\tl_new:N \l__chemformula_subscript_tl
+\tl_new:N \l__chemformula_subscript_tl
\box_new:N \l__chemformula_subscript_box
% remove double or nested sub- and superscripts
-\cs_new:Npn \chemformula_subscript:n #1
+\cs_new_protected:Npn \chemformula_subscript:n #1
{
\tl_if_eq:nnTF { #1 }{ \chemformula_superscript:n }
{ \chemformula_subscript_aux:n }
@@ -1069,7 +1085,7 @@
}
% handle subscripts
-\cs_new:Npn \chemformula_subscript_aux:n #1
+\cs_new_protected:Npn \chemformula_subscript_aux:n #1
{
\tl_put_right:Nn \l__chemformula_subscript_tl { #1 }
\chemformula_clean_subscript:
@@ -1088,7 +1104,7 @@
}
\cs_generate_variant:Nn \chemformula_subscript_aux:n { o,V }
-\cs_new:Npn \chemformula_clean_subscript:
+\cs_new_protected:Npn \chemformula_clean_subscript:
{
\tl_replace_all:Nnn \l__chemformula_subscript_tl
{ \chemformula_single_bond: } { \chemformula_minus: }
@@ -1128,7 +1144,7 @@
}
\cs_generate_variant:Nn \chemformula_subscript_write:n { V }
-\cs_new:Npn \chemformula_measure_subscript:n #1
+\cs_new_protected:Npn \chemformula_measure_subscript:n #1
{
\hbox_set:Nn \l__chemformula_tmpa_box
{ \chemformula_subscript_write:n { #1 } }
@@ -1139,11 +1155,11 @@
\cs_generate_variant:Nn \chemformula_measure_subscript:n { V }
% superscripts
-\tl_new:N \l__chemformula_superscript_tl
+\tl_new:N \l__chemformula_superscript_tl
\box_new:N \l__chemformula_superscript_box
% remove double or nested sub- and superscripts
-\cs_new:Npn \chemformula_superscript:n #1
+\cs_new_protected:Npn \chemformula_superscript:n #1
{
\tl_if_eq:nnTF { #1 } { \chemformula_superscript:n }
{ \chemformula_superscript_aux:n }
@@ -1162,7 +1178,7 @@
}
% handle superscripts
-\cs_new:Npn \chemformula_superscript_aux:n #1
+\cs_new_protected:Npn \chemformula_superscript_aux:n #1
{
\tl_put_right:Nn \l__chemformula_superscript_tl { #1 }
\chemformula_clean_superscript:
@@ -1178,7 +1194,7 @@
}
\cs_generate_variant:Nn \chemformula_superscript_aux:n { o,V }
-\cs_new:Npn \chemformula_clean_superscript:
+\cs_new_protected:Npn \chemformula_clean_superscript:
{
\tl_replace_all:Nnn \l__chemformula_superscript_tl
{ \chemformula_single_bond: } { \chemformula_minus: }
@@ -1224,7 +1240,7 @@
}
\cs_generate_variant:Nn \chemformula_superscript_write:n { V }
-\cs_new:Npn \chemformula_measure_superscript:n #1
+\cs_new_protected:Npn \chemformula_measure_superscript:n #1
{
\hbox_set:Nn \l__chemformula_tmpa_box
{ \chemformula_superscript_write:n { #1 } }
@@ -1235,7 +1251,7 @@
\cs_generate_variant:Nn \chemformula_measure_superscript:n { V }
% typeset both sub- and superscripts
-\cs_new:Npn \chemformula_use_sub_and_superscript:
+\cs_new_protected:Npn \chemformula_use_sub_and_superscript:
{
\group_begin:
% No shift for excited state
@@ -1326,7 +1342,7 @@
%-----------------------------------------------------------------------------%
% read the input
-\cs_new:Npn \chemformula_input:n #1
+\cs_new_protected:Npn \chemformula_input:n #1
{
\tl_set_rescan:Nnn \l__chemformula_chemformula_tmpa_tl
{
@@ -1334,6 +1350,7 @@
\char_set_catcode_letter:N \}
\char_set_catcode_letter:N \\
\char_set_catcode_other:N \"
+ \char_set_catcode_math_superscript:N \^
}
{ #1 }
\chemformula_input_escape_text:V \l__chemformula_chemformula_tmpa_tl
@@ -1351,10 +1368,12 @@
%-----------------------------------------------------------------------------%
% set options for next compound:
-\cs_new:Npn \chemformula_input_options:n #1
+\cs_new_protected:Npn \chemformula_input_options:n #1
{
\makeatother
- \tl_set_rescan:Nnn \l__chemformula_tmpa_tl { \makeatletter } { #1 }
+ \tl_set_rescan:Nnn \l__chemformula_tmpa_tl
+ { \char_set_catcode_letter:N \@ }
+ { #1 }
\tl_if_in:VnTF \l__chemformula_tmpa_tl { @ }
{
\bool_set_true:N \l__chemformula_options_bool
@@ -1368,7 +1387,7 @@
%-----------------------------------------------------------------------------%
% input stoichiometric factors
-\cs_new:Npn \chemformula_input_stoich:n #1
+\cs_new_protected:Npn \chemformula_input_stoich:n #1
{
\bool_set_true:N \l__chemformula_stoich_bool
\tl_put_right:Nn \l__chemformula_stoich_tl { \chemformula_font_inner: }
@@ -1388,8 +1407,8 @@
\tl_set_rescan:Nno \l__chemformula_stoich_tl
{ \ExplSyntaxOn }
{ \l__chemformula_stoich_tl }
- \exp_args:No \chemformula_parse_stoich:n
- { \l__chemformula_stoich_tl }
+ \chemformula_parse_stoich:V
+ \l__chemformula_stoich_tl
}
}
{ \tl_clear:N \l__chemformula_stoich_tl }
@@ -1397,20 +1416,16 @@
\cs_new:Npn \chemformula_bm:n #1 { #1 }
-\cs_new:Npn \chemformula_parse_stoich:n #1
+\cs_new_protected:Npn \chemformula_parse_stoich:n #1
+ { \__chemformula_parse_stoich:w #1 \q_nil }
+\cs_generate_variant:Nn \chemformula_parse_stoich:n { V }
+
+\cs_new:Npn \__chemformula_parse_stoich:w \chemformula_font_inner: #1 \q_nil
{
- % TODO add support for missing leading 0
+ \tl_put_right:Nn \l__chemformula_input_tl { \chemformula_font_inner: }
\tl_if_in:nnTF { #1 } { ( }
- { \tl_put_right:Nn \l__chemformula_input_tl { #1 } }
- {
- \tl_if_in:nnTF { #1 } { _ }
- { \chemformula_parse_stoich_misc_frac:w #1 \q_nil }
- {
- \tl_if_in:nnTF { #1 } { / }
- { \chemformula_parse_stoich_frac:w #1 \q_nil }
- { \tl_put_right:Nn \l__chemformula_input_tl { #1 } }
- }
- }
+ { \__chemformula_parse_stoich_iupac:n { #1 } }
+ { \__chemformula_parse_stoich_regular:n { #1 } }
}
\DeclareInstance { xfrac } { chemformula-text-frac } { text }
@@ -1419,18 +1434,57 @@
slash-right-kern = -.15em
}
-\cs_new:Npn \chemformula_parse_stoich_frac:w #1/#2 \q_nil
+\cs_new:Npn \__chemformula_parse_stoich_regular:n #1
+ {
+ \tl_if_in:nnTF { #1 } { / }
+ { \__chemformula_parse_stoich_frac:n { #1 } }
+ { \__chemformula_parse_stoich_decimal:n { #1 } }
+ }
+
+\cs_new_protected:Npn \__chemformula_parse_stoich_decimal:n #1
+ {
+ % get head:
+ \tl_set:Nx \l__chemformula_tmpa_tl { \tl_head:n { #1 } }
+ \tl_set:Nx \l__chemformula_tmpb_tl { \l__chemformula_decimal_output_tl }
+ % check if stoich starts with decimal-marker and add leading 0 if so:
+ \tl_if_eq:NNTF \l__chemformula_tmpa_tl \l__chemformula_tmpb_tl
+ { \tl_put_right:Nn \l__chemformula_input_tl { 0#1 } }
+ { \tl_put_right:Nn \l__chemformula_input_tl { #1 } }
+ }
+
+\cs_new:Npn \__chemformula_parse_stoich_frac:n #1
+ {
+ \tl_if_in:nnTF { #1 } { _ }
+ { \__chemformula_parse_stoich_misc_frac:w #1 \q_nil }
+ { \__chemformula_parse_stoich_frac:w #1 \q_nil }
+ }
+
+\cs_new_protected:Npn \__chemformula_parse_stoich_frac:w #1/#2 \q_nil
{
\tl_put_right:Nn \l__chemformula_input_tl
{ \chemformula_frac:nn { #1 } { #2 } }
}
-\cs_new:Npn \chemformula_parse_stoich_misc_frac:w #1_#2/#3 \q_nil
+\cs_new_protected:Npn \__chemformula_parse_stoich_misc_frac:w #1_#2/#3 \q_nil
{
\tl_put_right:Nn \l__chemformula_input_tl
{ \chemformula_misc_frac:nnn { #1 } { #2 } { #3 } }
}
+\cs_new_protected:Npn \__chemformula_parse_stoich_iupac:n #1
+ {
+ \bool_if:NTF \l__chemformula_stoich_parse_iupac_bool
+ { \__chemformula_parse_stoich_iupac:w #1 \q_nil }
+ { \tl_put_right:Nn \l__chemformula_input_tl { #1 } }
+ }
+
+\cs_new:Npn \__chemformula_parse_stoich_iupac:w (#1) \q_nil
+ {
+ \tl_put_right:Nn \l__chemformula_input_tl { ( }
+ \__chemformula_parse_stoich_regular:n { #1 }
+ \tl_put_right:Nn \l__chemformula_input_tl { ) }
+ }
+
\cs_new:Npn \chemformula_frac:nn #1#2
{
\bool_if:NT \l__chemformula_xfrac_bool
@@ -1453,7 +1507,7 @@
%-----------------------------------------------------------------------------%
% input compounds
-\cs_new:Npn \chemformula_input_cmpd:nN #1#2
+\cs_new_protected:Npn \chemformula_input_cmpd:nN #1#2
{
\tl_if_blank:VTF \l__chemformula_stoich_tl
{
@@ -1509,7 +1563,7 @@
}
\cs_generate_variant:Nn \chemformula_input_cmpd:nN { VN }
-\cs_new:Npn \chemformula_font_inner:
+\cs_new_protected:Npn \chemformula_font_inner:
{
\bool_if:NT \l__chemformula_fss_bool
{
@@ -1540,11 +1594,12 @@
\cs_new:Npn \chemformula_fontspec_aux:w [#1]#2 \q_stop
{ \newfontfamily \chemformula_font: [ #1 ] { #2 } }
-\cs_new:Npn \chemformula_sanitize:Nn #1#2
+\cs_new_protected:Npn \chemformula_sanitize:Nn #1#2
{
\tl_set_rescan:Nnn #1
{
\char_set_catcode_letter:N \_
+ \char_set_catcode_math_superscript:N \^
\char_set_catcode_letter:N \{
\char_set_catcode_letter:N \}
\char_set_catcode_letter:N \\
@@ -1553,16 +1608,17 @@
{ #2 }
}
-\cs_new:Npn \chemformula_handle_sub_and_superscripts:nN #1#2
+\cs_generate_variant:Nn \tl_if_in:nnT { x }
+\cs_new_protected:Npn \chemformula_handle_sub_and_superscripts:nN #1#2
{
\chemformula_sanitize:Nn \l__chemformula_tmpa_tl { #1 }
% let's see if the compound starts with a sub- or superscript:
- \exp_args:Nx \tl_if_in:nnT { \tl_head:V \l__chemformula_tmpa_tl } { ^ }
+ \tl_if_in:xnT { \tl_head:V \l__chemformula_tmpa_tl } { ^ }
{
\tl_put_left:Nn \l__chemformula_tmpa_tl
{ \bool_set_true:N \l__chemformula_is_isotope_bool }
}
- \exp_args:Nx \tl_if_in:nnT { \tl_head:V \l__chemformula_tmpa_tl } { _ }
+ \tl_if_in:xnT { \tl_head:V \l__chemformula_tmpa_tl } { _ }
{
\tl_put_left:Nn \l__chemformula_tmpa_tl
{ \bool_set_true:N \l__chemformula_is_isotope_bool }
@@ -1618,7 +1674,7 @@
%-----------------------------------------------------------------------------%
% clean up chemmacros commands:
-\cs_new:Npn \chemformula_clean_chem_macros:n #1
+\cs_new_protected:Npn \chemformula_clean_chem_macros:n #1
{
\cs_set:Nn \chemmacros_text:n
{
@@ -1632,7 +1688,7 @@
%-----------------------------------------------------------------------------%
% input "and" sign
-\cs_new:Npn \chemformula_input_plus:n #1
+\cs_new_protected:Npn \chemformula_input_plus:n #1
{
\tl_if_eq:nnT { #1 } { + }
{
@@ -1648,16 +1704,31 @@
%-----------------------------------------------------------------------------%
% input escaping gas
-\cs_new:Npn \chemformula_input_up:n #1
+\tl_new:N \g__chemformula_input_up_tl
+\cs_new_protected:Npn \chemformula_input_up:n #1
{
- \tl_if_eq:nnT { #1 } { ^ }
- { \tl_put_right:Nn \l__chemformula_input_tl { $\uparrow$ } }
+ \group_begin:
+ % ensure ^ has catcode 7
+ % (else chemmacros wouldn't work with e.g. `breqn')
+ \chemformula_sanitize:Nn \g__chemformula_input_up_tl { #1 }
+ \char_set_catcode_math_superscript:N \^
+ \__chemformula_input_up:V \g__chemformula_input_up_tl
+ }
+
+\cs_new_protected:Npn \__chemformula_input_up:n #1
+ {
+ \tl_gclear:N \g__chemformula_input_up_tl
+ \tl_if_eq:nnT { #1 } { ^ }
+ { \tl_gset:Nn \g__chemformula_input_up_tl { $\uparrow$ } }
+ \group_end:
+ \tl_put_right:NV \l__chemformula_input_tl \g__chemformula_input_up_tl
\bool_set_false:N \l__chemformula_is_up_bool
}
+\cs_generate_variant:Nn \__chemformula_input_up:n { V }
%-----------------------------------------------------------------------------%
% input precipitate
-\cs_new:Npn \chemformula_input_down:n #1
+\cs_new_protected:Npn \chemformula_input_down:n #1
{
\tl_if_eq:nnT { #1 } { v }
{ \tl_put_right:Nn \l__chemformula_input_tl { $\downarrow$ } }
@@ -1666,7 +1737,7 @@
%-----------------------------------------------------------------------------%
% input arrow
-\cs_new:Npn \chemformula_input_arrow:n #1
+\cs_new_protected:Npn \chemformula_input_arrow:n #1
{
\bool_if:nT
{
@@ -1681,7 +1752,7 @@
%-----------------------------------------------------------------------------%
% input compound name
-\cs_new:Npn \chemformula_input_name:n #1
+\cs_new_protected:Npn \chemformula_input_name:n #1
{
\bool_if:nT
{
@@ -1695,7 +1766,29 @@
%-----------------------------------------------------------------------------%
% input escaped text
-\cs_new:Npn \chemformula_input_escape_text:n #1
+% #1: <bool> #2: <tl> #3: <first> #4: <last>
+\cs_new_protected:Npn \__chemformula_bool_set_if_first_last:Nnnn #1#2#3#4
+ {
+ \int_zero:N \l__chemformula_tmpa_int
+ \int_zero:N \l__chemformula_tmpb_int
+ \int_set:Nn \l__chemformula_tmpa_int { \tl_count:n { #2 } }
+ \tl_map_inline:nn { #2 }
+ {
+ \int_incr:N \l__chemformula_tmpb_int
+ \int_compare:nT { \l__chemformula_tmpb_int = 1 }
+ {
+ \tl_if_eq:nnT { ##1 } { #3 }
+ { \bool_set_true:N #1 }
+ }
+ \int_compare:nT { \l__chemformula_tmpb_int = \l__chemformula_tmpa_int }
+ {
+ \tl_if_eq:nnF { ##1 } { #4 }
+ { \bool_set_false:N #1 }
+ }
+ }
+ }
+
+\cs_new_protected:Npn \chemformula_input_escape_text:n #1
{
\chemformula_first_last_text:n { #1 }
\bool_if:NT \l__chemformula_first_last_double_bool
@@ -1711,129 +1804,110 @@
}
\cs_generate_variant:Nn \chemformula_input_escape_text:n { V }
-\cs_new:Npn \chemformula_read_escape_double:w "#1" \q_nil
+\cs_new_protected:Npn \__chemformula_read_escape_text:n #1
{
\tl_set_rescan:Nnn \l__chemformula_tmpa_tl
- { \ExplSyntaxNamesOn \char_set_catcode_other:N \: }
- { { #1 } }
+ {
+ \char_set_catcode_letter:N \_
+ \char_set_catcode_other:N \:
+ }
+ { #1 }
\tl_replace_all:Nnn \l__chemformula_tmpa_tl
{ \_ } { chemformulaplaceholder }
\tl_replace_all:Nnn \l__chemformula_tmpa_tl
- { _ } { \sb } % TODO use \c_math_subscript_token
+ { _ } { \sb }
\tl_replace_all:Nnn \l__chemformula_tmpa_tl
{ chemformulaplaceholder } { \_ }
\tl_set_rescan:Nno \l__chemformula_tmpa_tl
- { \ExplSyntaxOff } { \l__chemformula_tmpa_tl }
+ { }% TODO da war \ExplSyntaxOff drin: brauchts das?
+ { \l__chemformula_tmpa_tl }
\tl_put_right:NV \l__chemformula_input_tl \l__chemformula_tmpa_tl
}
+\cs_new:Npn \chemformula_read_escape_double:w "#1" \q_nil
+ { \__chemformula_read_escape_text:n { #1 } }
+
\cs_new:Npn \chemformula_read_escape_single:w '#1' \q_nil
- {
- \tl_set_rescan:Nnn \l__chemformula_tmpa_tl
- { \ExplSyntaxNamesOn \char_set_catcode_other:N \: }
- { { #1 } }
- \tl_replace_all:Nnn \l__chemformula_tmpa_tl
- { \_ } { chemformulaplaceholder }
- \tl_replace_all:Nnn \l__chemformula_tmpa_tl
- { _ } { \sb } % TODO use \c_math_subscript_token?
- \tl_replace_all:Nnn \l__chemformula_tmpa_tl
- { chemformulaplaceholder } { \_ }
- \tl_set_rescan:Nno \l__chemformula_tmpa_tl
- { \ExplSyntaxOff } { \l__chemformula_tmpa_tl }
- \tl_put_right:NV \l__chemformula_input_tl \l__chemformula_tmpa_tl
- }
+ { \__chemformula_read_escape_text:n { #1 } }
-\cs_new:Npn \chemformula_first_last_text:n #1
+\cs_new_protected:Npn \chemformula_first_last_text:n #1
{
\bool_set_false:N \l__chemformula_first_last_double_bool
\bool_set_false:N \l__chemformula_first_last_single_bool
\bool_set_false:N \l__chemformula_first_last_text_bool
- \int_zero:N \l__chemformula_tmpa_int
- \int_zero:N \l__chemformula_tmpb_int
- \tl_map_inline:nn { #1 } { \int_incr:N \l__chemformula_tmpa_int }
- \tl_map_inline:nn { #1 }
- {
- \int_incr:N \l__chemformula_tmpb_int
- \int_compare:nT { \l__chemformula_tmpb_int = 1 }
- {
- \tl_if_eq:nnT { ##1 } { " }
- { \bool_set_true:N \l__chemformula_first_last_double_bool }
- }
- \int_compare:nT { \l__chemformula_tmpb_int = \l__chemformula_tmpa_int }
- {
- \tl_if_eq:nnF { ##1 } { " }
- { \bool_set_false:N \l__chemformula_first_last_double_bool }
- }
- }
+ \__chemformula_bool_set_if_first_last:Nnnn
+ \l__chemformula_first_last_double_bool
+ { #1 }
+ { " } { " }
\bool_if:NF \l__chemformula_first_last_double_bool
{
- \int_zero:N \l__chemformula_tmpb_int
- \tl_map_inline:nn { #1 }
- {
- \int_incr:N \l__chemformula_tmpb_int
- \int_compare:nT { \l__chemformula_tmpb_int = 1 }
- {
- \tl_if_eq:nnT { ##1 } { ' }
- { \bool_set_true:N \l__chemformula_first_last_single_bool }
- }
- \int_compare:nT { \l__chemformula_tmpb_int = \l__chemformula_tmpa_int }
- {
- \tl_if_eq:nnF { ##1 } { ' }
- { \bool_set_false:N \l__chemformula_first_last_single_bool }
- }
- }
+ \__chemformula_bool_set_if_first_last:Nnnn
+ \l__chemformula_first_last_single_bool
+ { #1 }
+ { ' } { ' }
}
}
%-----------------------------------------------------------------------------%
% input escaped math
-\cs_new:Npn \chemformula_input_escape_math:n #1
+\cs_new_protected:Npn \chemformula_input_escape_math:n #1
{
\chemformula_first_last_math:n { #1 }
- \bool_if:NT \l__chemformula_first_last_math_bool
- { \chemformula_read_escape_math:w #1 \q_nil }
+ \bool_if:NT \l__chemformula_first_last_dollar_bool
+ {
+ \bool_set_true:N \l__chemformula_first_last_math_bool
+ \chemformula_read_escape_dollar:w #1 \q_nil
+ }
+ \bool_if:NT \l__chemformula_first_last_mathbraces_bool
+ {
+ \bool_set_true:N \l__chemformula_first_last_math_bool
+ \chemformula_read_escape_mathbraces:w #1 \q_nil
+ }
}
-\cs_new:Npn \chemformula_read_escape_math:w $#1$ \q_nil
+\cs_new_protected:Npn \__chemformula_read_escape_math:n #1
{
\tl_set_rescan:Nnn \l__chemformula_tmpa_tl
{
- \ExplSyntaxNamesOn \char_set_catcode_letter:N \{
- \char_set_catcode_letter:N \}
+ \char_set_catcode_letter:N \_
+ \char_set_catcode_letter:N \:
}
- { \mbox { \( #1 \) } }
+ { \( #1 \) }
\tl_replace_all:Nnn \l__chemformula_tmpa_tl
{ \_ } { chemformulaplaceholder }
\tl_replace_all:Nnn \l__chemformula_tmpa_tl
- { _ } { \sb } % TODO use \c_math_subscript_token
+ { _ } { \sb }
\tl_replace_all:Nnn \l__chemformula_tmpa_tl
{ chemformulaplaceholder } { \_ }
\tl_set_rescan:Nno \l__chemformula_tmpa_tl
- { \ExplSyntaxOff } { \l__chemformula_tmpa_tl }
+ { }% TODO da war \ExplSyntaxOff drin: brauchts das?
+ { \l__chemformula_tmpa_tl }
\tl_put_right:NV \l__chemformula_input_tl \l__chemformula_tmpa_tl
\tl_put_right:Nn \l__chemformula_input_tl
{ \skip_horizontal:N \l__chemformula_math_space_skip }
}
-\cs_new:Npn \chemformula_first_last_math:n #1
+\cs_new:Npn \chemformula_read_escape_dollar:w $#1$ \q_nil
+ { \__chemformula_read_escape_math:n { #1 } }
+
+\cs_new:Npn \chemformula_read_escape_mathbraces:w \(#1\) \q_nil
+ { \__chemformula_read_escape_math:n { #1 } }
+
+\cs_new_protected:Npn \chemformula_first_last_math:n #1
{
\bool_set_false:N \l__chemformula_first_last_math_bool
- \int_zero:N \l__chemformula_tmpa_int
- \int_zero:N \l__chemformula_tmpb_int
- \tl_map_inline:nn { #1 } { \int_incr:N \l__chemformula_tmpa_int }
- \tl_map_inline:nn { #1 }
+ \bool_set_false:N \l__chemformula_first_last_dollar_bool
+ \bool_set_false:N \l__chemformula_first_last_mathbraces_bool
+ \__chemformula_bool_set_if_first_last:Nnnn
+ \l__chemformula_first_last_dollar_bool
+ { #1 }
+ { $ } { $ }
+ \bool_if:NF \l__chemformula_first_last_dollar_bool
{
- \int_incr:N \l__chemformula_tmpb_int
- \int_compare:nT { \l__chemformula_tmpb_int = 1 }
- {
- \tl_if_eq:nnT { ##1 } { $ }
- { \bool_set_true:N \l__chemformula_first_last_math_bool }
- }
- \int_compare:nT { \l__chemformula_tmpb_int = \l__chemformula_tmpa_int }
- {
- \tl_if_eq:nnF { ##1 } { $ }
- { \bool_set_false:N \l__chemformula_first_last_math_bool }
- }
+ \__chemformula_bool_set_if_first_last:Nnnn
+ \l__chemformula_first_last_mathbraces_bool
+ { #1 }
+ { \( } { \) }
}
}
@@ -1878,6 +1952,7 @@
\bool_set_false:N \l__chemformula_xfrac_bool
\bool_set_true:N \l__chemformula_nicefrac_bool
\bool_set_false:N \l__chemformula_mathfrac_bool ,
+ stoich-iupac-parse .bool_set:N = \l__chemformula_stoich_parse_iupac_bool ,
stoich-space .skip_set:N = \l__chemformula_stoich_space_skip ,
adduct-space .dim_set:N = \l__chemformula_cdot_space_dim ,
plus-space .skip_set:N = \l__chemformula_plus_space_skip ,