Skip to content

Fix LaTeXWriter not stripping $$ delimiters from text/latex MIME output#2929

Closed
singhharsh1708 wants to merge 3 commits intoJuliaDocs:masterfrom
singhharsh1708:fix/issue-2923-latex-mime-delimiters
Closed

Fix LaTeXWriter not stripping $$ delimiters from text/latex MIME output#2929
singhharsh1708 wants to merge 3 commits intoJuliaDocs:masterfrom
singhharsh1708:fix/issue-2923-latex-mime-delimiters

Conversation

@singhharsh1708
Copy link
Copy Markdown

When a type's show(io, ::MIME"text/latex", ...) wraps output in $$
or \[...\] around a display math environment (e.g. ModelingToolkit),
LaTeXWriter was writing the content verbatim. This caused a 'Bad math
environment delimiter' LaTeX error because both $$ and \begin{equation}
try to open math mode.
Add _strip_latex_math_delimiters which strips outer $$/\[...\] delimiters
only when the inner content already contains a \begin{...} environment.
Plain math content like $$ \left[...\right] $$ is left unchanged.
Fixes #2923

@odow
Copy link
Copy Markdown
Collaborator

odow commented Apr 27, 2026

I maintain that this is a bug in ModelingToolkit:

$$ \begin{equation}
\frac{dv(t)}{dt} = g
\end{equation}
$$

That's not valid LaTeX.

@singhharsh1708
Copy link
Copy Markdown
Author

You're right that $$\begin{equation}...\end{equation}$$ is invalid LaTeX — the bug belongs in the package generating it.
I've updated the approach: Documenter now emits a @warn when it detects this pattern, explicitly telling users to fix the show(io, MIME"text/latex"(), ...) method of the relevant type. The outer delimiters are still stripped so the PDF build doesn't crash with an opaque "Bad math environment delimiter" error, but the warning makes the real problem visible rather than silently papering over it.
Happy to close this if the preference is to let it fail loudly instead.

@odow
Copy link
Copy Markdown
Collaborator

odow commented Apr 27, 2026

I don't think there needs to be any change in Documenter. We shouldn't try to correct other people's incorrect LaTeX.

@odow
Copy link
Copy Markdown
Collaborator

odow commented Apr 27, 2026

@asinghvi17
Copy link
Copy Markdown
Collaborator

asinghvi17 commented Apr 27, 2026

It's probably because latexify emits \begin{equation}...\end{equation} by default - https://github.com/korsbo/Latexify.jl/blob/8b633ca3bbadf5e85b27b57555a76574cc15e786/src/latexequation.jl#L4

Ideally there would be some way to tell it not to - which SciML/MTK could use. But at a brief glance I could not find one.

@singhharsh1708
Copy link
Copy Markdown
Author

Understood, closing this. The fix belongs upstream — either in Latexify.jl to provide a way to suppress the \begin{equation} wrapper, or in ModelingToolkit's MTKLatexifyExt.jl to use that option once available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LaTeXWriter does not strip $$ delimiters from text/latex MIME output

3 participants