@@ -52,8 +52,15 @@ export const MhchemUtils = {
5252/**
5353 * Replace these constructs in mhchem output now that we have stretchy versions
5454 * of the needed arrows
55+ *
56+ * @param {string } match The matching macro name
57+ * @param {string } arrow The arrow name (without the backslash).
58+ * @returns {string } The mhchem arrow name, if there is one.
5559 */
56- export const MhchemReplacements = new Map < string , RegExp > ( [
60+ export const MhchemReplacements = new Map <
61+ string | ( ( match : string , arrow : string ) => string ) ,
62+ RegExp
63+ > ( [
5764 [
5865 '\\mhchemx$3[$1]{$2}' ,
5966 / \\ u n d e r s e t { \\ l o w e r 2 m u { ( .* ?) } } { \\ o v e r s e t { ( .* ?) } { \\ l o n g ( .* ?) } } / g,
@@ -72,7 +79,12 @@ export const MhchemReplacements = new Map<string, RegExp>([
7279 / \\ r l a p \{ \\ l o w e r \. 2 e m \{ - \} \} \\ r l a p \{ \\ r a i s e .2 e m \{ - \} \} \\ t r i p l e d a s h / g,
7380 ] ,
7481 [
75- '\\mhchem$1' ,
82+ ( match : string , arrow : string ) => {
83+ const mharrow = `mhchem${ arrow } ` ;
84+ return mhchemChars . lookup ( mharrow ) || mhchemMacros . lookup ( mharrow )
85+ ? `\\${ mharrow } `
86+ : match ;
87+ } ,
7688 / \\ ( x ? (?: l o n g ) ? (?: l e f t | r i g h t | [ L l ] e f t r i g h t | [ R r ] i g h t l e f t ) (?: a r r o w | h a r p o o n s ) ) / g,
7789 ] ,
7890] ) ;
@@ -90,7 +102,7 @@ export const MhchemMethods: { [key: string]: ParseMethod } = {
90102 try {
91103 tex = mhchemParser . toTex ( arg , machine ) ;
92104 for ( const [ name , pattern ] of MhchemReplacements . entries ( ) ) {
93- tex = tex . replace ( pattern , name ) ;
105+ tex = tex . replace ( pattern , name as string ) ;
94106 }
95107 } catch ( err ) {
96108 throw new TexError ( err [ 0 ] , err [ 1 ] ) ;
@@ -106,7 +118,7 @@ export const MhchemMethods: { [key: string]: ParseMethod } = {
106118/**
107119 * The command macros
108120 */
109- new CommandMap ( 'mhchem' , {
121+ const mhchemMacros = new CommandMap ( 'mhchem' , {
110122 ce : [ MhchemMethods . Machine , 'ce' ] ,
111123 pu : [ MhchemMethods . Machine , 'pu' ] ,
112124 mhchemxrightarrow : [ MhchemMethods . xArrow , 0xe429 , 5 , 9 ] ,
@@ -121,7 +133,7 @@ new CommandMap('mhchem', {
121133/**
122134 * The character macros
123135 */
124- new CharacterMap ( 'mhchem-chars' , MhchemUtils . relmo , {
136+ const mhchemChars = new CharacterMap ( 'mhchem-chars' , MhchemUtils . relmo , {
125137 tripledash : [ '\uE410' , { stretchy : false } ] ,
126138 mhchemBondTD : [ '\uE411' , { stretchy : false } ] ,
127139 mhchemBondTDD : [ '\uE412' , { stretchy : false } ] ,
0 commit comments