mathjax / MathJax

Beautiful and accessible math in all browsers
http://www.mathjax.org/
Apache License 2.0
10.21k stars 1.16k forks source link

Different behaviour for $ and for $$ #2613

Closed superkuvar closed 3 years ago

superkuvar commented 3 years ago

Issue Summary

Github Jekyll and MathJax similar problem as here https://github.com/mathjax/MathJax/issues/830 Where I use multiple _ underscores.

The code below works when using $$ and will not work for $. This is inconsistent and I would like the code to work for $ as well. The current troubleshooting is to add math inside the <div> </div> . <span> will not work on my end interestingly.

Steps to Reproduce:

  1. Create the following inside the .md file
    $\begin{aligned} \mathbb{E}_{\mathrm{x} \sim P} [ f(x) ]= \sum_{x} P(x) f(x) \end{aligned}$
  2. This will work
    $$\begin{aligned} \mathbb{E}_{\mathrm{x} \sim P} [ f(x) ]= \sum_{x} P(x) f(x) \end{aligned}$$
  3. I made it work with
    
    <div>

$\begin{aligned} \mathbb{E}{\mathrm{x} \sim P} [ f(x) ]= \sum{x} P(x) f(x) \end{aligned}$

4. If I would use `<span>` tag this will not work, so I cannot make it inline.
5. What I would like for `$` to work just like `$$` works.

### Technical details:

* MathJax part inside

* Client OS: Windows 10
* Browser: Latest Firefox
superkuvar commented 3 years ago

Almost forgot to mention I don't like to use \_ instead _ because I use VS Code and if I use the \_ this breaks the formula preview.

dpvc commented 3 years ago

Almost forgot to mention I don't like to use \_ instead _

Unfortunately, you may not have much choice. Since Markdown runs in order to create the HTML page, and MathJax doesn't turn until the HTML page is delivered to the browser, MathJax can't prevent Markdown from interpreting the underscores and inserting <em> or whatever HTML tags Markdown is using. MathJax can't process math that contains HTML tags, so if Markdown inserts those tags, there is nothing MathJax can do about it after the fact.

I'm not sure why the display moth doesn't cause the same issue, but it may be that the Markdown processor knows about double dollar signs and doesn't process in between them. Because single dollar signs are off by default, that might be why those don't prevent Markdown processing. You might try \(...\) or \\(...\\) and see if those have been handled like double dollar signs.

Do you know what Markdown engine you are using (is there a "markdown: x" specification in your _config.yml file)? I suspect you are using Kramdown, since the double dollar block works. In that case, for in-line math, you should be able to use double dollar signs for that (the determination about whether double dollars are in-line or display is determined by whether they appear in a paragraph or as a separate Markdown block). See the Kramdown documentation for details.

If you are not willing to use \_ and are not willing to use double dollar signs for all math, then the only real solution to this modify the Markdown engine to realize that it should not process the math between dollar signs.

superkuvar commented 3 years ago

Hi and thanks for the great feedback.

You might try (...) or \(...\)

I asked VS Code stuff to add these, in particular:

At the moment this is not working from VS Code. I believe I should have VS Code preview helping me to see the formula.

is there a "markdown: x" specification in your _config.yml file

I commented these lines long time ago:

# Build settings
# markdown: kramdown
# mathjax: true
# math_engine: katex

What is by default on Github, or in this file https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full should be what am I using.

I may be wrong but do you actually propose that I use kramdown?

Do you know what Markdown engine you are using

At the moment I don't know. I just create .md files, edit them and I commit.

In that case, m for in-line math, you should be able to use double dollar signs for that (the determination about whether double dollars are in-line or display is determined by whether they appear in a paragraph or as a separate Markdown block). See the Kramdown documentation for details.

I would like to use $$ with m for in-line math. I am not sure what is this. The Kramdown looks like a big doc and I haven't found the usage for m.


What I have in VS Code, I will try to explain:

Using $$

image

Using $

image

When using $ the end markup on Github will inject <em> if there is _ ... _, which you perfectly outlined, HTML prevents MathJax to render.

dpvc commented 3 years ago

What is by default on Github

The default is Kramdown, so you are good (and the fact that double dollars worked but single dollars didn't suggested that you were using Kramdown).

I would like to use $$ with m for in-line math. I am not sure what is this.

Sorry, it was a typo. It should have read "In that case, for in-line math..." (no "m"). What I was saying was that in Kramdown, $$ is used for both in-line and display math. So for example

... of some function $$f(x)$$ with  respect to ...

is in-line math, while

... is drawn from $$x$$:

$$\begin{aligned}...\end{aligned}$$

...

would have the aligned environment in display style (while the x is in-line).

superkuvar commented 3 years ago

@dpvc, your feedback has perfect sense.

Looks like I should just use the $$ (double dollars) to get the correct output.

image

The problem is on VS Code side as I just tested.

Look:

image

The whole section highlighted in blue is missing in VS Code preview. Looks very strange. I am not sure, but this looks like VS Code bug then.

dpvc commented 3 years ago

I am not sure, but this looks like VS Code bug then.

Well, it looks like VS Code always interprets double dollars as display math (your $$\mathrm{x} \sim P$$ is centered), so it looks like you won't get consistent results between VS Code and GitHub Jekyll. But he loss of the rest of the paragraph after the closing dollar signs does seem to be a bug in the editor.

I'm afraid I don't have any more suggestions for you, as the two Markdown environments (VS Code and Jekyll) don't agree on how to treat the math delimiters. My own opinion is that Jekyll is the one that is at fault, since it is using what are normally display delimiters for its in-line math delimiters, and that is the real source of confusion. I don't know how extensible Kramdown (used in GitHub Jekyll) is, but perhaps there could be an extension that handles single dollar signs. But I don't know of one.

Nyte-BK201 commented 3 years ago

I am not sure, but this looks like VS Code bug then.

Well, it looks like VS Code always interprets double dollars as display math (your $$\mathrm{x} \sim P$$ is centered), so it looks like you won't get consistent results between VS Code and GitHub Jekyll. But he loss of the rest of the paragraph after the closing dollar signs does seem to be a bug in the editor.

I'm afraid I don't have any more suggestions for you, as the two Markdown environments (VS Code and Jekyll) don't agree on how to treat the math delimiters. My own opinion is that Jekyll is the one that is at fault, since it is using what are normally display delimiters for its in-line math delimiters, and that is the real source of confusion. I don't know how extensible Kramdown (used in GitHub Jekyll) is, but perhaps there could be an extension that handles single dollar signs. But I don't know of one.

This annoys me to no end since I am using Typora as markdown tool where it supports math equation naturally but behaves not any kind of Jekyll, VS Code. Haha, real nightmares through these years and there might not be a solution. How the hell things become like this, I can only get a consistent input experience in mathexchange and hope to have that interpreter in my blog.