lua-filters icon indicating copy to clipboard operation
lua-filters copied to clipboard

mhchem: Charge not displayed properly after numerical element subscript

Open twoframesperminute opened this issue 2 years ago • 2 comments

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

twoframesperminute avatar Apr 08 '22 14:04 twoframesperminute

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.

jgm avatar Apr 08 '22 15:04 jgm

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.

twoframesperminute avatar Apr 08 '22 17:04 twoframesperminute