Using asciimath for both displayed and inline output with different delimiters

650 views
Skip to first unread message

Hameer Abbasi

unread,
Oct 5, 2012, 9:16:09 AM10/5/12
to mathja...@googlegroups.com
Is it possible to use asciimath for both inline/displayed mathematics on the same page/with the same configuration with different delimiters? If not, I'd like to propose this as a feature.

Davide P. Cervone

unread,
Oct 6, 2012, 6:29:44 PM10/6/12
to mathja...@googlegroups.com
> Is it possible to use asciimath for both inline/displayed
> mathematics on the same page/with the same configuration with
> different delimiters? If not, I'd like to propose this as a feature.

AsciiMath notation doesn't have a way to indicate display math as
opposed to in-line math. The assumption is, I assume, that you will
use regular HTML formatting to separate and center displayed
equations. There is a variable (displaystyle) that controls how
AsciiMath handles things like limits on sums and so on, but it is a
global value, not something that you can readily change on an equation-
by-equation basis. MathJax has a configuration option that controls
the displaystyle variable, but again, that is global.

On the other hand, it is not too hard to write an extension that does
what I think you are asking for. Here is an example file that does it:

<!DOCTYPE html>
<html>
<head>
<title>Add Inline/Display Control to AsciiMath</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
asciimath2jax: {delimiters: [['`','`'],['``','``']]},
AsciiMath: {displaystyle: false}
});
MathJax.Hub.Register.LoadHook("[MathJax]/extensions/
asciimath2jax.js",function () {
var AM = MathJax.Extension.asciimath2jax,
CREATEPATTERNS = AM.createPatterns;
AM.createPatterns = function () {
var result = CREATEPATTERNS.call(this);
this.match['``'].mode = ";mode=display";
return result;
};
});
MathJax.Hub.Register.StartupHook("AsciiMath Jax Ready",function () {
var AM = MathJax.InputJax.AsciiMath;
AM.postfilterHooks.Add(function (data) {
if (data.script.type.match(/;mode=display/))
{data.math.root.display = "block"}
return data;
});
});
</script>
<script type="text/javascript" src="../MathJax/MathJax.js?
config=AM_HTMLorMML"></script>

</head>
<body>
<INPUT TYPE="BUTTON" onclick="debug();" VALUE="Debug">

<p>Inline AsciiMath: `sum_{n=1}^10 n^2 = 55`</p>

<p>Display AsciiMath: ``sum_{n=1}^10 n^2 = 55``</p>

</body>
</html>

This makes `...` do inline math and ``...`` do displayed math. The
asciimath2jax extension has to be configured to include the double-
tick delimiters, and it createPatterns routine is modified to add a
parameter to the double-tick that tells asciimath2jax to mark the math
with double-ticks as display math. Then you add a postfilter to the
AsciiMath input jax that checks for display math elements and adds the
display="block" attribute to the root math node of the math that
AsciiMath generates. That's pretty much it.

Hope that does what you need.

Davide

Hameer Abbasi

unread,
Oct 8, 2012, 9:45:22 AM10/8/12
to mathja...@googlegroups.com
Hey Davide,

Thanks a lot for compiling that code for me, I appreciate it, but it seems not to work. It breaks MathJax on my page. My config file (iolympiads-mathjax.js, that I modified to include your lines of code, unmodified but for different delimiters and semantic spacing:

MathJax.Hub.Config({
  jax: ["input/TeX", "input/AsciiMath", "output/HTML-CSS", "output/SVG"],

  extensions: ["tex2jax.js", "asciimath2jax.js", "MathMenu.js", "MathZoom.js"],

  menuSettings: {
    zoom: "Double-Click",
    zscale: "200%"
  },

  TeX: {
    extensions: ["AMSmath.js", "AMSsymbols.js", "noErrors.js", "noUndefined.js"]
  },

  tex2jax: {
    inlineMath: [['$ ', ' $'], ["\\(", "\\)"]],
    displayMath: [['$$', '$$'], ["\\[", "\\]"]],
    processEscapes: true,
    processEnvironments: true
  },


  asciimath2jax: {
    delimiters: [['` ', ' `'], ['``', '``']]
  },

  AsciiMath: {
    displaystyle: false;
  }
});

MathJax.Hub.Register.LoadHook("[MathJax]/extensions/asciimath2jax.js", function () {
  var AM = MathJax.Extension.asciimath2jax, CREATEPATTERNS = AM.createPatterns;
  AM.createPatterns = function () {
    var result = CREATEPATTERNS.call(this);
    this.match['``'].mode = ";mode=display";
    return result;
  };
});

MathJax.Hub.Register.StartupHook("AsciiMath Jax Ready", function () {

  var AM = MathJax.InputJax.AsciiMath;
  AM.postfilterHooks.Add(function (data) {
    if (data.script.type.match(/;mode=display/)) {
      data.math.root.display = "block"}
      return data;
    });
});

MathJax.Ajax.loadComplete("[MathJax]/config/local/iolympiads-mathjax.js");

(I modified it back now, but you get the point. MathJax is called via the following:
<script type="text/javascript" src="http://www.iolympiads.com/MathJax/MathJax.js?config=local/iolympiads-mathjax.js"></script>

It tells me something for a short while about the config file failing to load.

Hameer Abbasi

unread,
Oct 8, 2012, 9:57:34 AM10/8/12
to mathja...@googlegroups.com
It worked, probably a network timeout error, since my cPanel was also giving me issues, as well as my SSH access.

Davide P. Cervone

unread,
Oct 8, 2012, 10:42:13 AM10/8/12
to mathja...@googlegroups.com
That sounds plausible.

I also notice that there is are extra spaces in "[ MathJax] /extensions" and "[ MathJax] /config" that I don't think should be there.  (They should be "[MathJax]/extensions" and "[MathJax]/config") The latter would cause the "failed to load config file" issues, and the former would prevent the hook from ever running (since that file would never be loaded).  But perhaps these spaces are being added by mail software somewhere along the line and aren't in your originals?

Davide
Reply all
Reply to author
Forward
0 new messages