Here is one way to do it. Add (or merge) the following TeX block to
your MathJax configuration:
TeX: {
Augment: {
Definitions: {macros: {
unitfrac: "myUnitFrac"
}},
Parse: {prototype: {
myUnitFrac: function (name) {
var n = this.GetBrackets(name),
num = this.GetArgument(name),
den = this.GetArgument(name);
if (n == null) {n = ""}
var tex = n + '\\,\\text{'+num+'}/\\text{'+den+'}';
this.string = tex + this.string.slice(this.i); this.i = 0;
}
}}
}
}
The Definitions.macros section adds a new macro \unitfrac that is tied
to the javascript function called myUnitFrac. That function is
defined in the Parser.prototype section. It receives the name of the
macro that called it (since it can be linked to more than one macro),
and this particular function looks up the optional bracketed argument,
then the numerator and denominator. If there was no bracket argument,
it is null, and we set it to "" (so that we can use it in the next
statement). The tex variable holds the replacement string, and then
it replaces the current string being parsed by the new tex string plus
whatever was left of the old one and sets the current location to the
beginning of the new string. This effectively inserts your
replacement text into the TeX string being parsed.
If you want to put this into a separate extension file, you should
look at something like MathJax/unpacked/extensions/TeX/verb.js for an
example of how that is done.
Note that this does rely on the internals of MathJax's TeX input jax,
and those are subject to change without notice. The use of
this.string and this.i are the only items here that I would worry
about, but I don't expect them to change in the future (but I expect
there should be a method added in the future to do this replacement
rather than doing this by hand).
Of course, a full implementation of the units and unitsdef package
would be more complicated, but this is a starting point.
Davide
Also, isn't this hint something that belongs into the FAQ? I imagine
many people would like to know how to implement custom commands which
take optional arguments...
Michael
<SCRIPT>
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
var TEX = MathJax.InputJax.TeX;
TEX.Definitions.macros.unitfrac = "myUnitFrac";
TEX.Parse.Augment({
myUnitFrac: function (name) {
var n = this.GetBrackets(name),
num = this.GetArgument(name),
den = this.GetArgument(name);
if (n == null) {n = ""}
var tex = n + '\\,\\text{'+num+'}/\\text{'+den+'}';
this.string = tex + this.string.slice(this.i); this.i = 0;
}
});
</SCRIPT>
in the HTML after MathJax.js is loaded.
Davide
PS, sorry for the delay in getting back to you. The holidays, and
work on version 1.1 have put me behind in my responses to the user
group.