yihui / litedown

A lightweight version of R Markdown
https://yihui.org/litedown/
Other
181 stars 4 forks source link

Equations muddled with list markup #33

Closed hturner closed 1 month ago

hturner commented 1 month ago

Equations with line breaks before operators that are valid markup for list bullets are not rendered correctly.

An example:

---
title: "Equation Bug"
output: 
  litedown::html_format:
    options:
      js_math: "katex"
knit: litedown:::knit
---

## Example

Equation with various operators that are also valid markup for list bullets

$$a * b - c + d$$

$$a * b - c + d$$ 

## Bad line breaks

Break before `*`, no preceding space

$$a

Break before -, one preceding space

$$a * b
 - c + d$$ 

$$a * b

Break before +, two preceding spaces

$$a * b - c
  + d$$ 

$$a * b - c

Maybe bad line break

Break before +, four preceding spaces (not sure if junk output is hangover from bad examples)

$$a * b - c
    + d$$ 

$$a * b - c

Good line break

Break after -, two preceding spaces

$$a * b - 
  c + d$$

$$a * b - c + d$$



Renders as 

![image](https://github.com/user-attachments/assets/608c881c-14f1-4dee-a260-f863e0c97ecd)

Notes:
* I get the same results with KaTeX and MathJax.
* All equations render correctly with rmarkdown::html_document.
yihui commented 1 month ago

This is indeed a bug, and due to one of the two aspects that I dislike most about the commonmark spec (i.e., no requiring empty lines around block-level elements). The author of Pandoc also hates it: https://github.com/jgm/djot?tab=readme-ov-file#rationale (point 7). I think this is absolutely a design mistake, but I can't change the spec. I'll see if I can figure out a workaround. Thanks for the report!

yihui commented 1 month ago

Fixed now. However, please note that the current syntax for $$ $$ expressions requires that there are no trailing spaces after the closing $$. Your examples have spaces after d$$, which must be removed.

image
hturner commented 1 month ago

Brilliant, thanks!