schang412 / typst-whalogen

A port of LaTeX mhchem to Typst
Apache License 2.0
20 stars 2 forks source link

Complex compounds aren't displayed correctly #11

Open BackThePortal opened 3 months ago

BackThePortal commented 3 months ago

Iron (III) carbonate is not displayed properly:

Captura de pantalla 2024-07-15 a les 15 03 33

This is how it should look like: Captura de pantalla 2024-07-15 a les 15 03 51

The issue appears when there is a number behind the parenthesis, since cadmium cyanide is shown properly:

Captura de pantalla 2024-07-15 a les 15 05 58

BackThePortal commented 3 months ago

Just tried to see what's causing this bug. It's because of the way Typst parses parenthesis in a math environment. One way to solve it would be to add a space whenever a number and a parenthesis are together. It's not a perfect fix because it's not the way it should look.

This will be tricky to fix.

BackThePortal commented 3 months ago

Another possibility is to close and open the math environment, like this: $"Fe"_2$$("SO"_3)_3$. But I'm not sure how that would interfere with the rest of the code.

schang412 commented 2 months ago

Hi, i tried fixing this by adding the space as suggested, but it will affect the behavior for state of aggregation. For compounds like NaOH, it will no longer be possible to put the state in the subscript. This needs some more work.

  // on opening brackets...
   if _char_in.contains(regex("[\[({]")) {
     (_state, _out, _buffer) = (
+      "underscore": ("leading_punctuation", _flush_ce_buffer(_state, _buffer), " "),
       "_": ("", "", "")
     ).at(_state, default: ("leading_punctuation", _flush_ce_buffer(_state, _buffer), ""))
   }
BackThePortal commented 2 months ago

Hi, i tried fixing this by adding the space as suggested, but it will affect the behavior for state of aggregation. For compounds like NaOH, it will no longer be possible to put the state in the subscript. This needs some more work.

Perhaps this can be solved requiring a space to be added between the compound and the state? I think it makes sense from a user perspective to type ce("NaOH (aq)") instead of ce("NaOH(aq)").