martenlienen/pelican-katex

Inline math following open bracket not possible

Closed this issue · 4 comments

Hi, currently it is impossible to write something like some text ($x + y$) to get "some text ($x + y$)" (note that the parentheses are not in math mode) in pelican-katex due to the rule that inline math must be preceded by whitespace if it doesn't start at the beginning of a block.

Unlike false-positive cases where you don't want inline math but pelican-katex recognizes your markdown code as such—which you can fix by escaping the dollar sign—, this is an instance of a false-negative case that as far as I know has no possible workaround apart from inserting a space after the open bracket which you might not want.

A possible solution to the specific example above would be to also allow inline math to be preceded by ( (GitHub seems to handle this case similarly), but then there might be other false-negatives if someone wants to their inline math to follow a different character. I think that the only possible ways to always leave a choice to the user are to (1) assume math mode by default which the user has to escape if they don't want it or (2) do the reverse and require "inverted escaping" to enable math mode (which probably would be quite unintuitive).

Would it work to accept dollar signs that are preceded by non-alphanumeric characters? At the same time it should be possible to adapt the regex to reject dollar signs preceded by backslashes to allow escaping.

Would it work to accept dollar signs that are preceded by non-alphanumeric characters?

I think so. My specific case would definitely be fixed by this, and probably also most other cases where someone wants to start inline math that doesn't follow a whitespace. Technically it's still not completely bullet-proof (e.g. what if someone wants hello$a+b$ to be recognized as math?), but I believe that these cases are very rare. GitHub also doesn't recognize this example, btw.

At the same time it should be possible to adapt the regex to reject dollar signs preceded by backslashes to allow escaping.

With explicit escaping one could make sure that the user always has a choice, but this will of course break backwards compatibility in cases like 5$ and 10$ which then would require manual escaping (5\$ and 10\$). You are the project owner, choose the approach you like :)

I have released version 1.8.2 on PyPI, which should render your inline math in parentheses :) To my own surprise, backslash escaping was already implemented from a previous issue.

Can confirm that it works now, thanks a lot!