JamesxX / typst-chem-par

Typst show rules for parsing IUPAC names and chemical formulae
MIT License
16 stars 4 forks source link

Charges not behaving as expected #9

Open wweatherall opened 1 week ago

wweatherall commented 1 week ago

There is an issue when one tries to add charges. A single charge works fine, but when you try to add multiple you get issues.

image image image

JamesxX commented 1 week ago

Regex doesn't have a concept of valence electrons so you'll probably want to either format it manually or use brackets

wweatherall commented 1 week ago

How do you use brackets with it? Or do you mean bracket with #super[]

Having looked at it, your package knows what elements are. Would a regex expression that knew if you had a charge and it would know it would be, be possible?

Like metals are all fairly simple because they aren't diatomic. If there is metal, a number, and then a charge, it would always be an ion.

For the nonmetal ions, they would need to be disambiguated from their polyatomic forms but if the pattern is single nonmetal, a number, then a charge then it would be a charge on an ion.

For polyatomic ions, I wonder if a mcchem style of using carets would do the trick. Beyond what they do, If there is any number between the caret and the charge, I would think to would only possibly be a charge and so that pattern could safely be lifted.

Here is a bit of code that explains what I mean about the carets:


#let ion-fm = regex("(\p{upper}\p{lower}?)(\d+)?(\^(\d*))?([-+])?")

#show ion-fm: it=> {

    let (sm, atom-n, _, ..chg) = it.text.match(ion-fm).captures

    sm

    sub(atom-n)

    super(chg.join())

}

CO2

CO3^2-

Cl3-

H2O

H3O+

2SO4^2- + H2O - 

CO2

CO3^2-

Cl-

H2O

H3O+

Ca^2+

2SO4^2- 

CO3^2-

NH4+

NO2^-

NO3^-

SO3^2-

SO4^2-

HSO4^-

S2O3^2-

C2O4^2-

OH-

PO3^3-

PO4^3-

HPO4^2-

H2PO4^-

ClO4-

ClO3-

ClO2-

ClO-

BrO3^-

IO3^-

CH3COO^-

S2O3^2-

CH3COO^-

That said wouldn't it be cool if it had like a list in your repository that people could add ions to with the correct formatting that is triggered by typing them in the laziest way possible as I did in my example from earlier.

Anyway, thanks for the response. I'm really satisfied with your package so far.

One final thing, I think

#show "<=>": sym.harpoons.rtlb

//to make possible 

#"<=>"

would be a nice little perk in your package.