Search code examples
mathjax

How to correctly render double negatives in MathJax?


I have some formulas generated on the fly. Mathjax does not turn double negatives to positives. For example, I want "1 - - 1" to become "1 + 1". Same for "1 + - 1 = 1 - 1".

Is there a way to force MathJax to do this?

Ofcourse I can do this step in the code but it makes my formula templates uglier.

Thanks


Solution

  • MathJax is not a computer algebra system, it is just a mathematical typesetting system. It has no understanding of the mathematics it typesets, and so can not perform transformations on the equations. You will have to do that yourself.

    It is possible to register pre-filters for the TeX input processor, so you could use that approach to modify the TeX code before MathJax evaluates it. For example, place

    <script type="text/x-mathjax-config"> 
      MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { 
        MathJax.InputJax.TeX.prefilterHooks.Add(function (data) { 
          data.math = data.math.replace(/- *-/g,"+")
                               .replace(/- *+|+ *-/g,"-"); 
        }); 
      }); 
    </script>
    

    before the script that loads MathJax.js itself. That might do what you need.