Skip to content

fix: preserve math expressions in comment shortcodes (LaTeX + HTML)#3

Merged
vgreg merged 3 commits intovgreg:mainfrom
zinc75:fix/latex-math-in-comments
May 9, 2026
Merged

fix: preserve math expressions in comment shortcodes (LaTeX + HTML)#3
vgreg merged 3 commits intovgreg:mainfrom
zinc75:fix/latex-math-in-comments

Conversation

@zinc75
Copy link
Copy Markdown
Contributor

@zinc75 zinc75 commented Apr 24, 2026

Problem

When a shortcode contains inline math, e.g.:

{{< question "$\hat{h}_*$" inline=true >}}

Two rendering failures occur depending on the output format.

LaTeX: pandoc.utils.stringify returns the content as a plain string with
literal dollar signs. escape_latex then escapes $\$ and \
\textbackslash{}, producing broken output in the todo:

\todo...{\$\textbackslash{}hat\{h\}\_*\$}

HTML: pandoc.Str(comment_text) inserts the text as an opaque string node.
MathJax/KaTeX never sees a pandoc.Math node and renders the dollar signs as
literal characters.

A secondary HTML issue: build_html_inline used display: inline-flex to lay
out the span contents. With a single Str node this was harmless, but once
parse_inlines splits the text into multiple nodes (Str, Math, Space…), each
node becomes a flex item and the comment renders as a column layout.

Fix

LaTeX (already in this PR): add escape_latex_with_math which splits on
$...$ regions — math is copied verbatim, surrounding text goes through
escape_latex as before.

HTML (new): add parse_inlines(text) which calls
pandoc.read(text, "markdown") and returns the inlines of the first block.
This converts $...$ into proper pandoc.Math nodes that MathJax/KaTeX
processes correctly. Use it in build_html_inline (via :extend()) and
build_html_block.

Also replace display: inline-flex / align-items / gap with
display: inline-block / vertical-align: baseline in build_html_inline to
prevent the column-layout artifact.

Result

{{< question "Define $\hat{h}_*$ and $\hat{h}$." inline=true >}}

LaTeX: renders correctly as:

\todo...{Define $\hat{h}_*$ and $\hat{h}$.}

HTML: math is rendered by MathJax/KaTeX; inline comment box has correct
rectangular border with no column artifacts.

zinc75 added 2 commits April 24, 2026 09:34
$...$ regions were escaped by escape_latex, producing broken LaTeX
like \$\textbackslash{}hat\{h\}\_*\$.

Add escape_latex_with_math which copies $...$ content verbatim and
only escapes surrounding text.
Parse comment text as Markdown so that $...$ regions become proper
pandoc.Math nodes, rendered by MathJax/KaTeX in HTML output.
Also fix inline comment display: replace inline-flex (which created
column artifacts with multiple Pandoc nodes) with inline-block.
Companion to the LaTeX math fix (previous commit)
@zinc75 zinc75 changed the title fix: preserve LaTeX math expressions inside comment shortcodes fix: preserve math expressions in comment shortcodes (LaTeX + HTML) Apr 24, 2026
@zinc75
Copy link
Copy Markdown
Contributor Author

zinc75 commented May 8, 2026

@vgreg : Gently reminder : did you have time to look at my pull request ? This allows the use of maths in comments which is useful for many use cases 🙂

Comment thread _extensions/comments/comment_core.lua Outdated
@vgreg
Copy link
Copy Markdown
Owner

vgreg commented May 8, 2026

@vgreg : Gently reminder : did you have time to look at my pull request ? This allows the use of maths in comments which is useful for many use cases 🙂

Thank you for this contribution.
Can you make the fix in my comment above? It looks all good otherwise.

@zinc75
Copy link
Copy Markdown
Contributor Author

zinc75 commented May 9, 2026

@vgreg : Gently reminder : did you have time to look at my pull request ? This allows the use of maths in comments which is useful for many use cases 🙂

Thank you for this contribution. Can you make the fix in my comment above? It looks all good otherwise.

Fix done in f368b83 💯

@vgreg vgreg merged commit ef819d2 into vgreg:main May 9, 2026
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.

2 participants