pandoc / lua-filters

A collection of lua filters for pandoc
MIT License
610 stars 166 forks source link

mhchem: Charge not displayed properly after numerical element subscript #223

Open twoframesperminute opened 2 years ago

twoframesperminute commented 2 years ago

In mhchem, the charge is propertly displayed when it follows an element with a numerical subscript. E.g. in \ce{HCO3- + H+ <-> H2CO3} the negative charge of the HCO3 molecule is rendered as a minus sign, not a negative charge, while the positive charge after the H molecule is rendered properly.

In the mhchem filter, a Charge can only follow a letter (AZ or az) or a bracket/brace/parenthesis, not a number. I think changing line 108 and 109 to the line below (adding R"09") fixes the issue, but I'm not well-versed enough in Lua/Lpeg to see whether this adds problems down the line.

Charge = B(R"AZ" + R"az" + R"09" + S")]}") * (S"+-") * #-R"AZ" /
    render.superscript ;

Screenshot 2022-04-08 at 16 55 19

jgm commented 2 years ago

That seems reasonable to me. Maybe you could modify your filter along these lines, use it for a while, and inspect the results to make sure nothing unexpected happens. It could also be useful to add a new test case for this to test.txt, and to run the modified filter on test.txt to make sure nothing changes for the other formulas.

If you can test it out in this way, I'd be happy to incorporate the change.

twoframesperminute commented 2 years ago

I will try it out in my current project. I will also add test cases (copied from the mhchem LaTeX package documentation) and check the current test cases. If it seems OK, I will create a pull request.