Description
Applies To
- Notebooks (.ipynb files)
- Interactive Window and/or Cell Scripts (.py files with #%% markers)
What happened?
Problem
When using IPython.display.display
to render raw LaTeX output in a VS Code Jupyter notebook, a string like:
display({'text/latex': '$$A$$ $$B$$'}, raw=True)
fails with:
ParseError: KaTeX parse error: Can't use function '$' in math mode at position 2: A$̲$ $$B
I think this is happening because the VS Code KaTeX renderer strips the outermost $$
and then sends the rest (A$$ $$B
) into KaTeX’s parser in display mode — which interprets this as a single expression and throws a parse error when it encounters one of the inner $
.
In contrast, the parser works if we prepend the previous example with preliminary text, like this:
display({'text/latex': 'some preliminary text... $$A$$ $$B$$'}, raw=True)
renders correctly, now with two separate math display blocks. Somehow the preliminary text is enough to get KaTeX to parse more carefully and find the additional display block delimiters.
It appears that this inconsistency does not occur in classic Jupyter Notebook or JupyterLab, perhaps because it is using MathJax. There, multiple $$…$$
display blocks in a single text/latex
MIME type string seem to always get parsed correctly.
Reproduction
In a Jupyter Notebook in VS Code, run the following in a code cell:
from IPython.display import display
display({'text/latex': '$$A$$ $$B$$'}, raw=True) # Fails in VS Code
display({'text/latex': 'Initial text... $$A$$ $$B$$'}, raw=True) # Works in VS Code
Expected behavior
Both calls should succeed and render multiple display math blocks, as they do in Jupyter Notebook and JupyterLab.
VS Code Version
1.101.1
Jupyter Extension Version
2025.5.0
Jupyter logs
Coding Language and Runtime Version
Python v3.13.4
Language Extension Version (if applicable)
No response
Anaconda Version (if applicable)
No response
Running Jupyter locally or remotely?
Local