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

Latex not parsed correctly by markdown renderer in VS Code #147043

Closed
DonJayamanne opened this issue Apr 7, 2022 · 8 comments · Fixed by microsoft/vscode-markdown-it-katex#2 or #148845
Closed
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug good first issue Issues identified as good for first-time contributors help wanted Issues identified as good community contribution opportunities markdown Markdown support issues notebook-markdown verified Verification succeeded
Milestone

Comments

@DonJayamanne
Copy link
Contributor

Discussed in microsoft/vscode-jupyter#9630

Originally posted by jhconning April 7, 2022
The Quarto notebook project builds part of its documentation around how to use vs-code with the jupyter extension to create beautifully rendered HTML and pdf documents https://quarto.org/docs/get-started/hello/vscode.html

However, I've noticed a problem with using Quarto syntax that results in a katex parsing error in a markdown cell when using the juptyer extension. The issue does not arise using JupyterLab.

Quarto allows one to label and cross-reference equations, as described here:
https://quarto.org/docs/get-started/authoring/vscode.html#cross-references

In the example there, a {#eq-stddev} label is added immediately after the $$ delimiter (does not work if the label is on the next line). The rendered quarto document then produces a numbered equation and the @eq-stddev label can be used elsewhere in the document to create clickable cross-reference links.

## Equation 
THis is the standard deviation formula:
$$
\sqrt{\frac{1}{N-1} \sum_{i=1}^N (x_i - \overline{x})^2}
$$ {#eq-stddev}

The problem is that the juptyer extension in vs-code chokes on this syntax, and presents a Katex parser error instead of the rendered cell:
image

This problem does not arise in Jupyterlab, where the same is rendered like this:
image

Would it be possible to change the parsing to allow this syntax? One could then develop notebooks using the jupyter extension and also have the option of using the powerful extra directives and syntax from quarto to produce beautiful websites, books, blog posts, and other documents.

@DonJayamanne DonJayamanne transferred this issue from microsoft/vscode-jupyter Apr 7, 2022
@mjbvz
Copy link
Contributor

mjbvz commented Apr 8, 2022

Looks like the parser doesn't like the trailing {#eq-stddev}. Based on the screen shot it looks like Jupyter just ignores this text

Here's the relevant code to look into fixing this: https://github.com/mjbvz/markdown-it-katex

@mjbvz mjbvz added bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities markdown Markdown support issues good first issue Issues identified as good for first-time contributors notebook-markdown labels Apr 8, 2022
@mjbvz mjbvz added this to the Backlog milestone Apr 8, 2022
@Lemmingh
Copy link
Contributor

Lemmingh commented Apr 9, 2022

Jupyter just ignores this text

Jupyter can easily make it because its Markdown parsing is powered by Marked, as I noted in #141905 (comment) and yzhang-gh/vscode-markdown#1030 (comment).

Marked is not CommonMark-compliant. Indeed, they disagree at the very first step of parsing (block-level): markedjs/marked#1202


{#eq-stddev}

This seems a derivative of "pandoc-eqnos". To allow it in markdown-it-katex, the end condition of math_block perhaps needs to be changed to something much more complex.


Well, I'd recommend just replicating Jupyter's Marked for processing Markdown in a Jupyter notebook. This could potentially solve half of the problems occurring in rendering Jupyter Markdown by markdown-it.

@Lemmingh
Copy link
Contributor

Since Jupyter does not allow blank line inside an equation, there is still a chance of mimicking its logic in a markdown-it plugin:

Only contribute inline rules. In other words, all math areas must be treated as inline structures at Markdown level.

This is not a rare choice, but against the design of markdown-it-katex.


From this perspective, I recommend building a separate dedicated markdown-it plugin. Or you can choose to make your fork mostly incompatible with the original one.

markdown-it-katex now defines several inline and leaf block structures.

So, the first thing is to merge them into one inline rule. What about simply naming it "math", or verbosely "math_span"? Anyway, this step is complicated.

The remaining work is just adjusting rendering calls (renderer rule). We can distinguish structures by Token.markup as long as it's properly set during parsing.

@JerryWJi
Copy link

I'd like to take a stab at this - not completely sure where to start though, would appreciate some hints or leads!

@mjbvz
Copy link
Contributor

mjbvz commented Apr 12, 2022

@JerryWJi The relevant plugin is here: https://github.com/mjbvz/markdown-it-katex

Once you fix the issue in the repo — which includes tests you can use to verify the fix — we can then pick up the fix in VS Code

@JerryWJi
Copy link

@mjbvz Thanks for the help. The approach I'm currently taking involves changing this line here: https://github.com/JerryWJi/markdown-it-katex/blob/master/index.js#L176

Am I going down the right path, or is there something else I should try?

@JerryWJi
Copy link

@mjbvz pushed a code fix to my fork - the code no longer only checks the last two characters for the terminating double dollar sign. https://github.com/JerryWJi/markdown-it-katex/blob/master/index.js#L181

Let me know if this is looking good so far.

@mjbvz
Copy link
Contributor

mjbvz commented Apr 30, 2022

@JerryWJi Can yon please try creating a PR for this so I can more easily review it? Also be sure to add some tests to confirm the new behavior

@mjbvz mjbvz modified the milestones: Backlog, May 2022 May 5, 2022
mjbvz added a commit to mjbvz/vscode that referenced this issue May 5, 2022
mjbvz added a commit that referenced this issue May 5, 2022
@joyceerhl joyceerhl added the verified Verification succeeded label Jun 3, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug good first issue Issues identified as good for first-time contributors help wanted Issues identified as good community contribution opportunities markdown Markdown support issues notebook-markdown verified Verification succeeded
Projects
None yet
8 participants
@rebornix @DonJayamanne @greazer @mjbvz @joyceerhl @JerryWJi @Lemmingh and others