jgm / pandoc

Universal markup converter
https://pandoc.org
Other
34.6k stars 3.38k forks source link

Empty line inside math mode trigger exit math mode #8293

Open yasirroni opened 2 years ago

yasirroni commented 2 years ago

Describe your proposed improvement and the problem it solves.

Support \begin{aligned} and &= inside $$ ... $$ in markdown.

Currently GitHub markdown (and VSCode markdown viewer support LaTeX inside markdown using KaTeX. But, it seems that I can't use pandoc to convert my markdown.

Describe alternatives you've considered.

None.

yasirroni commented 2 years ago

I've found out that the problem is not that pandoc did not support \begin{aligned}, rather empty line inside $$ ... $$. But, is not correctly return the error message, since it return:

! Package amsmath Error: \begin{aligned} allowed only in math mode.

It seems that empty line wrongly translated as exit math mode environment.

jgm commented 2 years ago

This is as documented in the manual under tex_math_dollars extension:

For display math, use $$ delimiters. (In this case, the delimiters may be separated from the formula by whitespace. However, there can be no blank lines between the opening and closing $$ delimiters.)

The intent of the restriction is to prevent $$ which aren't math from being interpreted as beginning a math section, and also to avoid potentially reading the entire file before finding that there is no closing $$ delimiter.

Solution is pretty easy: blank lines in LaTeX math are semantically irrelevant, so remove them?

yasirroni commented 2 years ago

Yeah, removing them solve the problem. But, GitHub and VSCode markdown renderer can work with it flawlessly.

Maybe follow their approach?

Another solution is to preserve a line that only contain $$ (yeah, only this in that line, no other character except whit space and change line symbol) as begin / end math environment. _ I hope blank lines is supported because it can add readability when working with \begin{aligned}. Thank you.

jgm commented 2 years ago

Looks like GitHub's support for math in Markdown is a bit different from pandoc's. Unfortunately, I can't find a detailed spec for it. There is this but it's sketchy and not entirely right.

Some things I had to learn from experimenting:

So, this works $x=6$
But not this $x=
5$

So, this works $x=6$ But not this $x= 5$

math $$x=y$$

math $$x=y$$

not math
$$x=
y$$

math
$$x=y$$

$$x=
y$$

not math $$x= y$$

math $$x=y$$

$$x= y$$

$$
x=

y$$

$$
\begin{aligned}
x &=y\\

z &=y^2\\
\end{aligned}
$$

$$ x=

y$$

$$ \begin{aligned} x &=y\

z &=y^2\ \end{aligned} $$

```math
\sqrt{3}

```math
\sqrt{3}

@martinwoodward are these things expected? Is there a fuller spec for GH's math support somewhere?

@yasirroni perhaps you could post a minimal sample of the Markdown that is working in GH but not with pandoc.

yasirroni commented 2 years ago

Whoops, my bad. After further testing, it is the VSCode that able to render the markdown, not the GitHub! Pandoc even has a better render than GitHub!

So, the render capability, ranked, will be: VSCode>Pandoc>GitHub.

In my VSCode, this is working:

$$
\begin{aligned}

x &=y
\\
z &=y^2

\end{aligned}
$$
jgm commented 2 years ago

"Better" depends on whether it's really desirable to allow blank lines here. What if someone has an unclosed $$? Does VSCode treat the entire rest of the document as math, or does the parser backtrack?

yasirroni commented 2 years ago

GitHub markdown seems has lots of issues, that is why I said that Pandoc works better. See this discussion about GitHub can't render ($...$) yet: https://github.com/orgs/community/discussions/30606#discussioncomment-3531432


Regarding VSCode, I've tested it that using $$ in the start of the line only, it will open a math environment (even if there is no other $$ to close it. If it is not in the start of the line, it will not open.

BUT, to close math mode, $$ can be used in the middle of the line, but it will remove all text in that line.

IMHO, that is the best.

So,

$$ hello

world

foo $$ bar

Rendered into:

$hello world$