[HOME] jsMath (Authors) [Prev][Up][Skip][Next]

Defining Macros for jsMath

JsMath provides both the TeX \def command and the LaTeX \newcommand and \newenvironment macros for creating new macros and LaTeX environments, so you can include these within your mathematics to make it easier to enter complicated formulas. If you want to have a set of macros predefined, however, you will want to have them stored in a JavaScript file that can be loaded by jsMath to make your macros available on your page. This is accomplished via the jsMath.Macro() command, 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.

(The jsMath.Macro() function doesn't support TeX's more complex macro syntax that allows macros to use control sequences or other characters as parameter delimiters. The \def macro does, however, and these are implemented in the newcommand.js extension, which is loaded automatically when these macros are used. It would be possible to load the extension explicitly and predefine the more complex macros.)

There is a corresponding jsMath.Environment() command that you can use to define new environments, but you must load the newcommand.js extension explicitly before you can use it. Include the line

    jsMath.Extension.Require("newcommand");
at the top of your JavaScript file, or add "extensions/newcommand.js" to the loadFiles array in easy/load.js to have the newcommand extension preloaded for you.

The format for the jsMath.Environment() command is similar to that of jsMath.Macro(); the first argument is the name of the environment, the second is the string that replaces the \begin{name} and the third is the string that replaces \end{name}. If a fourth arguement is provided, it gives the number of parameters that are to follow the \begin{name}; these parameters are substituted for #1, #2, etc. in the string that replaces \begin{name}.

There is an important caveat when specifying the replacement strings for macros and environments 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: 13 Sep 2008 09:53:13
Comments to: dpvc@union.edu
[Next] Loading Extensions in jsMath
[Skip] Browser Support for jsMath
[Up] Information for jsMath Authors
[Prev] Adding Fonts to jsMath