MathJax & \rule

75 views
Skip to first unread message

Matias Piipari

unread,
Dec 5, 2016, 7:07:17 AM12/5/16
to MathJax Development
Hi there

Whilst tweaking the MathML to TeX conversions done by my app Manuscripts (http://manuscriptsapp.com) as part of document importing, I encountered what looks to be a limitation in MathJax with handling the LaTeX macro \rule. Here's an example:

6\text{.}\text{53} \times {\text{10}}^{4}\phantom{\rule{0.25em}{1ex}}\text{N/m}

(I realise the markup is ugly – it's the direct output from MathML => TeX conversion)


This renders as following in pdflatex (which LaTeXiT below is using):
Inline image 1



Inline image 2

I understood from earlier discussion between Peter and Davide that \rule is not presently implemented but may be possible to implement. Is this indeed the case? Meanwhile I was wondering, is the big horizontal space caused by MathJax encountering some unexpected input and is there perhaps some way to configure MathJax to be graceful about it, or some smaller change that could be made to suppress it, in case implementing \rule is nontrivial?

Matias

Davide Cervone

unread,
Dec 19, 2016, 7:12:36 PM12/19/16
to MathJax Development
It looks like the images are missing.  Can you repost them?

Davide Cervone

unread,
Dec 20, 2016, 9:54:47 AM12/20/16
to mathj...@googlegroups.com
Whilst tweaking the MathML to TeX conversions done by my app Manuscripts (http://manuscriptsapp.com) as part of document importing, I encountered what looks to be a limitation in MathJax with handling the LaTeX macro \rule. Here's an example:

6\text{.}\text{53} \times {\text{10}}^{4}\phantom{\rule{0.25em}{1ex}}\text{N/m}

(I realise the markup is ugly – it's the direct output from MathML => TeX conversion)

I suspect that MathML is ugly to start with, and that is the source of the problem

The \phantom{\rule{...}{...}} is a pretty strange construction.  It seems that its purpose is to introduce a space, which would be better handled as \kern .25em or \hskip .25em or \hspace{.25em} (depending on your preferences).  The original MathML may have been <mphantom><mpadded width=".25em" height="1ex" mathbackground="black" /></mphantom> or something similarly ugly.

In any case, this is the source of the problem.  Because the combined configuration files (that handle TeX input) all include the noUndefined extension, unknown macros are displayed as their macro name in red.  So since \rule is not defined in MathJax, when \rule{.25em}{1ex} is processed, it produces output equivalent to "\rule.25em1ex" with "\rule" in red.  Then this is wrapped in \phantom, so it is not displayed, but takes up the same space, and in particular, is as white as these characters.  That is why you are getting large blank spaces.  So this is "correct" behavior for the given situation.

You could include a definition for \rule that would resolve the problem.  For example, the equivalent of

    \def\rule#1#2{\Rule{#1}{#2}{0pt}}

provided that your uses of \rule never include the optional offset (e.g., \rule[3pt]{.25em}{1ex}).  To handle the offset would take some extra work.

Davide

Davide Cervone

unread,
Dec 20, 2016, 10:04:33 AM12/20/16
to mathj...@googlegroups.com
Here is an implementation of \rule that handles the optional offset parameter.  Add the following to your page BEFORE the script that loads MathJax.js itself.

<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  var MML = MathJax.ElementJax.mml,
      TEX = MathJax.InputJax.TeX;

  TEX.Definitions.macros.rule = "myRule";

  TEX.Parse.Augment({
    myRule: function (name) {
      var v = this.GetBrackets(name),
          w = this.GetDimen(name),
          h = this.GetDimen(name);
      var mml = MML.mspace().With({
        width: w, height:h,
        mathbackground: (this.stack.env.color || "black")
      });
      if (v) {
        mml = MML.mpadded(mml).With({voffset: v});
        if (v.match(/^\-/)) {
          mml.height = v;
          mml.depth = '+' + v.substr(1);
        } else {
          mml.height = '+' + v;
        }
      }
      this.Push(mml);
    }
  });
});
</script>

The contents of the script could also be put into a local configuration file that you load along with the main configuration file.

Hope that fixes the issue for you.

Davide

Matias Piipari

unread,
Dec 20, 2016, 10:29:22 AM12/20/16
to mathj...@googlegroups.com
Thanks a lot Davide!! I'll give this a go as soon as I can. Indeed the presentation MathML in question is pretty ugly markup to begin with in these cases. I should be able to tweak the transform from it to that simpler form you suggested.

Re: the missing images, sorry my bad – I've attached them below for future reference (first one showing how LaTeX renders the offending TeX markup):

Inline image 1

Inline image 2


Inline image 3

Matias

--
You received this message because you are subscribed to a topic in the Google Groups "MathJax Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathjax-dev/qlj6dGKt-Eg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mathjax-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
- - - - -
Matias Piipari, PhD

Manuscripts - A writing tool like nothing you have seen before
Reply all
Reply to author
Forward
0 new messages