[HOME] jsMath (Authors/2.x) [Prev][Top][Up][Next]

Defining Macros for jsMath

JsMath does not implement the \def or \newcommand macros of TeX and LaTeX, however, you can still define your own macros in jsMath. But you have to do it through JavaScript. The main interface is the jsMath.Macro() function, which takes two arguments and an optional third one. The first is a string that will be the name of the macro (without the backslash), and the second is its replacement string, while the third is the number of parameters that the macro expects. (Note that jsMath does not support TeX's more complex macro syntax that allows macros to use control sequences or other characters as parameter delimiters. A plugin for this is in the works.)

There is an important caveat when specifying the replacement strings for macros defined in this way: you must double all the backslashes in the string. This is because you are using a JavaScript string, and the backslash is used as an escape character within the strings, so to obtain an actual backslash within the string, you need to escape the backslash.

For example

    <SCRIPT> jsMath.Macro('R','{\\bf R}^{#1}',1) </SCRIPT>
would define \R2 to produce {\bf R}^{2}, or "R-squared", and \R3 to produce {\bf R}^{3}, or "R-cubed".

Note that all definitions produced this way are global; they are available throughout the HTML file.

It is possible to tie macros to JavaScript functions so that the JavaScript will run whenever the macro is executed by jsMath, but that is beyond the scope of this documentation. You will need to look near the end of jsMath.js for details on that. You might also look at the definitions in the various plugins to see how they do it.



Get jsMath at SourceForge.net. Fast, secure and Free Open Source software downloads [HOME] jsMath web pages
Created: 10 Jul 2005
Last modified: 02 Dec 2005 13:00:18
Comments to: dpvc@union.edu
[Next] Using jsMath without Image Fonts
[Up] Information for jsMath v2.x Authors
[Prev] Adding Fonts to jsMath