mathjax / MathJax

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

Subscript notation issue #329

Closed fonnesbeck closed 11 years ago

fonnesbeck commented 11 years ago

When I try to use the grouped subscript notation ("_{}"), Mathjax does render it. For example:

$\mathbf{X}_i = (X_{1i}, \ldots, X_{ki})$

does not work because of the "_{1i}" subscripts. If I just use a single subscript, like "X_i", it works fine.

fonnesbeck commented 11 years ago

I also notice a failure in a related instance. Here, it is just a single subscript, but the following:

$$Pr(h(\mathbf{X}) \le b) \approx \frac{1}{N} \sum_{i=1}^N I(h(\mathbf{X}) \le b)$$

renders fine, while adding a subscript i (_i) to the last part of the equation causes it to fail:

$$Pr(h(\mathbf{X}) \le b) \approx \frac{1}{N} \sum_{i=1}^N I(h(\mathbf{X}_i) \le b)$$

Using the very latest MathJax from GitHub, as well as the CDN service.

pkra commented 11 years ago

This renders fine for me, e.g., by copy&pasting it into http://cdn.mathjax.org/mathjax/latest/test/sample-dynamic-2.html. (fyi: the first example is a missing _ in X{1i})

When you say "does not work", what are you seeing? (math-processing error, not rendered as expected etc)

Also, tell us you browser+version+OS, please. And, if you can, a link to an example page where the error occurs.

fonnesbeck commented 11 years ago

So, here is a screen shot of the "not working" case:

broken

Here is the code that produces the output:

Input: $\mathbf{X}_i = (X_{1i}, \ldots, X_{ki})$

Output: $\mathbf{Y}_i = h(\mathbf{X}_i)$

Analysis: 

$$Pr(h(\mathbf{X}) \le b) \approx \frac{1}{N} \sum_{i=1}^N I(h(\mathbf{X}_i) \le b)$$
$$E(h(\mathbf{X})) \approx \frac{1}{N} \sum_{i=1}^N h(\mathbf{X}_i)$$

And here, when I eliminate the grouped subscript in the first case, and the last "_i" in the second, it renders:

working

Here is the code that produces this output:

Input: $\mathbf{X}_i = (X_i, \ldots, X_i)$

Output: $\mathbf{Y}_i = h(\mathbf{X}_i)$

Analysis: 

$$Pr(h(\mathbf{X}) \le b) \approx \frac{1}{N} \sum_{i=1}^N I(h(\mathbf{X}) \le b)$$
$$E(h(\mathbf{X})) \approx \frac{1}{N} \sum_{i=1}^N h(\mathbf{X}_i)$$

Using Safari 6 or Chrome 23 in OS X 10.8.2

Here is the MathJax configuration I have set up on the page:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
      processEscapes: true
    },
    "HTML-CSS": { availableFonts: ["TeX"] }
  });
</script>
<script type="text/javascript"
    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
fonnesbeck commented 11 years ago

PS, the underscore isn't missing in the first example, I just did not use a verbatim block, so it disappeared. I will fix this.

dpvc commented 11 years ago

It looks like this is an interaction between your content management system and MathJax. If you are using a Markdown engine, it probably uses underscores to denote italics (notice that material between the underscores is italicized). Since the markdown engine is running before the page is shipped (and MathJax runs after it arrives at your browser), the underscores are already removed and <i> tags inserted before MathJax has a chance to see them. MathJax doesn't process math if it has HTML tags in the middle of it, so the <i> in the middle prevents MathJax from processing that equation.

You don't mention what content management system you are using, but this is most likely something that needs to be dealt with at that end of things. In some cases, you can use "verbatim" mode to prevent the system from altering the mathematics. E.g., for Markdown, you might use backticks around the math. That might require altering the MathJax configuration to allow MathJax to process <code> blocks, however, if backticks produce those.

In any case, this doesn't look like a MathJax issue but rather a Markdown interaction with the TeX code.

fonnesbeck commented 11 years ago

Thanks. I'm using Landslide to convert Markdown to HTML5 presentation slides. I will consult Landslide's author.

fonnesbeck commented 11 years ago

PS backslashing the underscore seems to do the trick.

jay3sh commented 7 years ago

I ran into another variant of the issue. Sometimes the underscores work without escaping them. But then they stop working when there are multiple underscores in the expression. The solution is to escape all occurences of underscores. In case of markdown add \ before underscore.

bergercookie commented 7 years ago

FYIW the backslashing solution provided by @dpvc is relevant to other Markdown presentation projects such as Gitpitch

vman049 commented 5 years ago

Escaping the underscore with backslash works well for inlineMath but doesn't work for displayMath (text doesn't get subscripted). Is there a solution that is consistent across both inlineMath and displayMath? Related links: 1, 2, 3, 4.

dpvc commented 5 years ago

@vman049, you don't say which content management system (CMS) you are using, and since this is really a question about the CMS more than it is about MathJax, that is probably going to make a difference. You have referenced Jekyll and Hugo posts, so it is not clear which you are using. Your third reference is to the official recommendation of how to handle MathJax in Hugo, so if you are using Hugo, that shows how to be able to use the same notation within inline and display math.

You also don't say what the actual result is in displayed equations, e.g., whether the underscore is displayed as a literal underscore, for example. If so, it is probably also possible to define \_ as _ in your Mathjax configuration via

MathJax.Hub.Config({
  TeX: {
    Macros: {
      '\\_': '_'
    }
  }
});

so that \_ and _ both do the same thing, and you will be able to use \_ in in-line mode (where Markdown will remove the\) and also \_ in display mode (where it will be a macro equivalent to _). So that would mean you could use \_ for subscripts in both situations.

lukesalamone commented 3 years ago

Just wanted to mention for anyone coming here from Google, I was able to solve this issue in hugo by wrapping my mathjax in <p> tags.

HinnyTsang commented 1 year ago

In my case, the \_ is work for your reference.

prasanth-ntu commented 1 year ago

Inline math expression: $\sum_{i=1}^{m}$ $\sum{i=1}^{m}$ Summation superscript rendering is bad! Another e.g., `$\frac{1}{2m}\sum{i=1}^{m}{(\hat{y} {i} - y {i})}^{2}$` $\frac{1}{2m}\sum{i=1}^{m}{(\hat{y} {i} - y _{i})}^{2}$

Block math expression: Summation superscript is okay! $$\sum_{i=1}^{m}$$ $$\sum{i=1}^{m}$$ Another e.g., $$\frac{1}{2m}\sum{i=1}^{m}{(\hat{y} {i} - y {i})}^{2}$$

Any workarounds?

dpvc commented 1 year ago

@prasanth-ntu, if you are workin in GitHub, their implementation of MathJax has a number of serious issues, and if I recall correctly, this is one of them. If you are not using Mathjax on GitHub, then can you point us to a page that exhibits the problem?

prasanth-ntu commented 1 year ago

@dpvc I am relatively new to writing/rendering math expressions in GitHub Markdown, as so far I have been either using Jupyter Markdowns or Latex directly to render them. To answer your question, I believe I am using the GitHubs default implementation (MathJax). Are there any other implementations that we can use to render inline math expressiosn in GitHub markdown that can overcome the issues with MathJax?

dpvc commented 1 year ago

Are there any other implementations that we can use to render inline math expressiosn in GitHub markdown that can overcome the issues with MathJax?

It is not an issue with MathJax, it is an issue with how GitHub connects their markdown to MathJax. In this case, they are filtering attributes from the MathJax output, and have removed ones that are critical to the layout. That is not MathJax's fault, and is out of our control. Unfortunately, users don't have the ability to change how GitHub Markdown handles that. The only option I know of is to petition GitHub to correct their implementation. There is a discussion board where issues are listed, and a bug report system that you can use. This is something that GitHub will have to address, not MathJax.