Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline latex doesn't allow punctuation following the ending $ #193

Closed
cventus opened this issue Aug 4, 2023 · 4 comments · Fixed by #196
Closed

Inline latex doesn't allow punctuation following the ending $ #193

cventus opened this issue Aug 4, 2023 · 4 comments · Fixed by #196
Labels
enhancement New feature or request

Comments

@cventus
Copy link
Contributor

cventus commented Aug 4, 2023

Hi

When I try to include an inline equation I can't seem to put punctuation after the ending $. The following examples don't work unless I put a space before the period or comma:

The algorithm has a time complexity of $O(log(n))$.

Let's consider $x$, $y$, and $z$.

Putting a space in any of those locations really doesn't look good, and I think it's incorrect to include the punctuation in the equation itself.

I took a quick look at the regex and I wonder if it's possible to change the final look-ahead from (=?\s|$) to (=?\W|$). The non-word class \W includes spaces, but also common punctuation, which would resolve my issue.

const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])+?)(?<!\$)\1(?=\s|$)/;

@UziTech
Copy link
Owner

UziTech commented Aug 4, 2023

Ya I think that could work. Can you create a PR and add a couple tests?

@UziTech UziTech added the enhancement New feature or request label Aug 6, 2023
@UziTech
Copy link
Owner

UziTech commented Aug 6, 2023

It looks like we would need to update the look ahead to (=?[^\w\}\$]|$)

This will prevent a match if one of the following are true:

  • A word character is found \w since we don't want to match $300 $400
  • A squiggly brace is found } since we want to be able to match nested katex $ \hbox{$x^2$} $
  • A dollar sign $ since we want the same number of delimiters so it won't match $ not katex $$

I think this might be too lenient and allow other types of markdown that is not katex or prevent matching other valid katex.

The other alternative could be to list specific punctuation that we do allow (=?[\s.,]|$). This is how other parts of markdown work specifically adding valid punctuation.

I am also not an expert in katex so I don't know if there is a valid case of nested katex that would have punctuation after $

For example: (im not sure if this is valid katex)
$ some nested katex $x$, $

If we do decide to list valid punctuation, what punctuation should we allow?

@cventus
Copy link
Contributor Author

cventus commented Aug 7, 2023

Thanks for looking into this 👍 Good find about the autolink, I didn't consider that.

An explicit list of punctuation that's allowed, just like the autolink example, sounds like a better idea than the broad \W. I don't see why anybody would want to emphasize, bold, or strike-through an inline equation, and it probably wouldn't do what you want, so maybe keep ?, !, ., ,, and : and drop *, _ and ~? I.e. (=?[\s?!\.,:]|$)?

If this list is fine then I can create a PR with tests maybe today or tomorrow. The punctuation list can always be revised if there are any further issues.

@UziTech
Copy link
Owner

UziTech commented Aug 7, 2023

Ya that list works for me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants