Use unicode characters as macros

368 views
Skip to first unread message

jakub....@gmail.com

unread,
Nov 10, 2012, 2:34:05 PM11/10/12
to mathja...@googlegroups.com
Hello,

in the regular LaTeX, I am using a lot of Unicode characters (which I can input directly from my keyboard), such as →, ∊ and many others. However, MathJax displays them as question marks. Is it possible to display them as the original Unicode characters? Furthermore, is it possible to use a Unicode character as a macro? For example, in LaTeX, I use \def{\sum}, and then I can write, for example, ∑_i insted of \sum_i. Would anything similar be possible also in MathJax?

Thanks,

Jakub

Davide P. Cervone

unread,
Nov 12, 2012, 8:25:04 PM11/12/12
to mathja...@googlegroups.com
In the regular LaTeX, I am using a lot of Unicode characters (which I can input directly from my keyboard), such as →, ∊ and many others. However, MathJax displays them as question marks. Is it possible to display them as the original Unicode characters?

I suspect that the problem is that they are not being saved to the original HTML file properly.  You might use the View Source menu in your browser to see if the characters show up properly in the source HTML file.  MathJax certainly CAN show unicode characters if they are properly encoded in the file.  But using such characters directly is often complicated because of encoding issues.  Not only must you save the file correctly, you also have to make sure the file is being shipped to your reader with the proper encoding so that their browser will interpret the characters properly.  That means you should probably include the line

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

in your document's <head> to make sure the encoding is set properly.

Furthermore, is it possible to use a Unicode character as a macro? For example, in LaTeX, I use \def{\sum}, and then I can write, for example, ∑_i insted of \sum_i. Would anything similar be possible also in MathJax?

This can be done, though it takes a little coding to do it, since MathJax's \def only works for names that begin with the backslash.  If you place 

<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  var TEX = MathJax.InputJax.TeX;
  var MACROS = {
    "\u2211": "\\sum",
    "\u221A": ["\\sqrt{#1}",1]
  };
  for (var id in MACROS) {if (MACROS.hasOwnProperty(id)) {
    TEX.Definitions.special[id] = "myMacro";
  }}
  TEX.Parse.Augment({
    myMacro: function (c) {
      var args = [c].concat(MACROS[c] instanceof Array ? MACROS[c] : [MACROS[c]]);
      return this.Macro.apply(this,args);
    }
  });
});
</script>

in your HTML file somewhere BEFORE the script that loads MathJax.js itself, then this will define the characters listed in the MACROS variables to act like macros.

In this case, the first of these will define ∑ to work like \sum.  You can add more definitions to the MACROS variable above as well (make sure you separate them by commas, and make sure there is no comma after the last one in the list).  Note that the definitions are JavaScript strings, so you have to double all the backslashes.  You can make a macro that has arguments as in the second example, by putting the macro definition inside brackets and followed by the number of arguments that there are.  In the second definition, I've defined √{...} to work like \sqrt{...}.  Here, I have used numeric references to the unicode characters to avoid encoding problems, but you could use the characters directly within the quotes, as in

  var MACROS = {
    "∑": "\\sum",
    "√": ["\\sqrt{#1}",1]
  };

if you prefer, but again, using actual unicode characters does introduce more complications of making sure your files are saved properly and transferred to the server undamaged (as you have already seen).

Davide

jakub....@gmail.com

unread,
Nov 13, 2012, 5:22:24 AM11/13/12
to mathja...@googlegroups.com
That was a great answer! You were right about the encoding. I was using a WordPress plugin, which, for some reason, turned the Unicode characters inside $$ into question marks. After deactivating the plugin and inserting the MathJax scripts manually, it started to work. Furthermore, Unicode macros work like a charm using the script you have written. Thank you very much.

Davide P. Cervone

unread,
Nov 13, 2012, 5:30:22 AM11/13/12
to mathja...@googlegroups.com
Glad you were able to straighten out the encoding issue, and got the macros to work.  It was an interesting challenge.

Davide

kasper...@gmail.com

unread,
Sep 26, 2013, 6:25:54 PM9/26/13
to mathja...@googlegroups.com
Wow, very cool feature, thank you for explaining that :)
Reply all
Reply to author
Forward
0 new messages