Search code examples
javascripthtmlmathjaxhugo

MathJax equation containing multiple summations with index


Background:

Here is how I am including the MathJax library in my page:

<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML' async></script>

Problem:

The MathJax string (that I believe) will display what I want:

$$\sum_{s=1}^{1000} p_s  \sum_{c=1}^{4} x_c$$

wanted_broken


The closest MathJax string that I can get to work (display correctly):

$$\sum_{s=1}^{1000} p_s  \sum_c^{4} x_c$$

notwanted_works

As part of debugging, I have simplified the second summation down to 'x_c' but it still does not work. This leads me to believe that the problem is caused by the second summation index definition. When I try to add the 'c=1' bit to the second summation notation, it seems MathJax will no longer render the equation at all. This behavior seems strange since the first summation can have the defined index (e.g., 'i=1'). Any thoughts appreciated at this point.


Solution

  • As @Peter_Krautzberger has noted it looks like the Markdown parser has converted some text to italics. Which could be causing the issue.

    The below snippet is to verify that the issue is not with MathJax.

    <script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML-full&latest"></script>
    
    $$\sum_{s=1}^{1000} p_s  \sum_{c=1}^{4} x_c$$

    According to https://divadnojnarg.github.io/blog/mathjax/ MathJax \sum_ does not work correctly in markdown and you have to use \sum\_.

    Latex rendering errors There are some differences with classical Latex expressions and the syntax to use in a markdown document. For example, \sum_ does not render with Hugo and you should use \sum_ instead (notice the second backslash before the underscore).

    Try escaping the underscores.

    Either:

    $$\sum\_{s=1}^{1000} p\_s \sum\_{c=1}^{4} x\_c$$

    or

    $$\sum\_{s=1}^{1000} p_s \sum\_{c=1}^{4} x_c$$

    Might do the trick.

    There is also some additional config mentioned in the article that may be required:

    <script type="text/javascript" async
      src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
      MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        displayMath: [['$$','$$']],
        processEscapes: true,
        processEnvironments: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
        TeX: { equationNumbers: { autoNumber: "AMS" },
             extensions: ["AMSmath.js", "AMSsymbols.js"] }
      }
      });
      MathJax.Hub.Queue(function() {
        // https://github.com/mojombo/jekyll/issues/199
        var all = MathJax.Hub.getAllJax(), i;
        for(i = 0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    
      MathJax.Hub.Config({
      // Autonumbering by mathjax
      TeX: { equationNumbers: { autoNumber: "AMS" } }
      });
    </script>