jsmath plugin

jsmath plugin by Stephen Gould
Plugin for displaying latex equations using jsMath

Last updated on 2008-12-05. Provides Syntax.
No compatibility info given!

Similar to asciimathml, format, latex, math, math2.

Tagged with formula, latex, math, tex.

Advantages

  • Use LaTeX syntax without the need to have LaTeX installed (both client and server).
  • The rendering is done automatically by a JavaScript library that just has to be uploaded to the server.
  • The client does not need to install anything since there is a quite nice picture font available.
  • If the client installs the LaTeX fonts, the client's browser can display math formulas in the high-quality fonts also used by LaTeX.
  • If you enable the optional $x^i$ syntax, you can write inline math as if you were in LaTeX.

Installation

  1. install jsMath
  2. create a directory called jsmath under lib/plugins
  3. add the syntax.php and script.js to the new directory
  4. edit the jsMathURL variable in script.js to contain the correct path to jsMath.js
  5. optionally set the JSMATH_USE_DOLLAR variable to 1 in syntax.php

NOTE: When trying to display latex formulas for the first time, make sure to clear the browser cache! Otherwise you'll get frustrated thinking the install didn't go through; alternatively add somewhere in your latex test page this:

~~NOCACHE~~


syntax.php

<?php
  /**
   * jsMath Plugin - see http://www.math.union.edu/~dpvc/jsmath/
   *
   * Syntax:    <jsm> ...latex... </jsm>
   *            <jsmath> ...latex... </jsmath>
   * (optional) $ ...latex... $
   * 
   * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
   * @author     Stephen Gould <sgould@cs.stanford.edu>
   */
 
/* set this to 1 if you want to use $\sum_i i$ to use math mode */
/* Works precisely as <jsm>\sum_i i</jsm> */
if(!defined('JSMATH_USE_DOLLAR')) define('JSMATH_USE_DOLLAR',0);
 
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_jsmath extends DokuWiki_Syntax_Plugin {
 
    /**
     * Get an associative array with plugin info.
     */
    function getInfo(){
        return array(
                     'author' => 'Stephen Gould',
                     'email'  => 'sgould@cs.stanford.edu',
                     'date'   => '2008-07-12',
                     'name'   => 'jsMath Plugin',
                     'desc'   => 'Plugin for displaying latex equations using jsMath
                                  See http://www.math.union.edu/~dpvc/jsmath/
                                  Syntax: <jsm(ath)?>math formulae</jsm(ath)?>',
                     'url'    => 'http://www.dokuwiki.org/wiki:plugins',
                     );
    }
 
    function getType(){ return 'formatting'; }
    function getAllowedTypes() {
        return array('substition','formatting','disabled');
    }
    function getPType(){ return 'normal'; }
    function getSort(){ return 222; }
 
     /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addEntryPattern('<jsm>(?=.*?</jsm>)',$mode,'plugin_jsmath');
        $this->Lexer->addEntryPattern('<jsmath>(?=.*?</jsmath>)',$mode,'plugin_jsmath');
        if(JSMATH_USE_DOLLAR)
        {
            $this->Lexer->addEntryPattern('\$(?=.*?\$)',$mode,'plugin_jsmath');
        }
 
    }
 
    function postConnect() {
        $this->Lexer->addExitPattern('</jsm>','plugin_jsmath');
        $this->Lexer->addExitPattern('</jsmath>','plugin_jsmath');
        if(JSMATH_USE_DOLLAR)
        {
            $this->Lexer->addExitPattern('\$','plugin_jsmath');
        }
    } 
 
    /**
     * Handler to prepare matched data for the rendering process.
     */
    function handle($match, $state, $pos, &$handler){
        switch ($state) {
        case DOKU_LEXER_ENTER : 
            if(JSMATH_USE_DOLLAR)
            {
                return array($state, preg_match("/^(\\\$|<jsm>)/", $match));
            }
            else
            {
                return array($state, preg_match("/^<jsm>/", $match));
            }
            break;
        case DOKU_LEXER_MATCHED :
            break;
        case DOKU_LEXER_UNMATCHED :
            return array($state, $match);
            break;
        case DOKU_LEXER_EXIT :
            if(JSMATH_USE_DOLLAR)
            {
                return array($state, preg_match("/^(\\\$|<\/jsm>)/", $match));
            }
            else
            {
                return array($state, preg_match("/^<\/jsm>/", $match));
            }
            break;
        case DOKU_LEXER_SPECIAL :
            break;
        }
        return array($state, '');
    }
 
    /**
     * Handle the actual output creation.
     */
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml') {
            list($state, $match) = $data;
            switch ($state) {
            case DOKU_LEXER_ENTER :
                if ($match) {
                    $renderer->doc .= '<span class="math">';
                } else {
                    $renderer->doc .= '<div class="math">';
                }
                break;
            case DOKU_LEXER_MATCHED :
                break;
            case DOKU_LEXER_UNMATCHED :
                $renderer->doc .= $renderer->_xmlEntities($match);
                break;
            case DOKU_LEXER_EXIT :
                if ($match) {
                    $renderer->doc .= '</span>';
                } else {
                    $renderer->doc .= '</div>';
                }
                break;
            case DOKU_LEXER_SPECIAL :
                break;
            }
        }
        return false;
    }
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :

script.js

// full url to jsMath installation                                              
var jsMathURL = '';                        
 
document.write('<SCRIPT>jsMath = {Controls: {cookie: {scale: 120}}}</SCRIPT>'); 
document.write('<SCRIPT SRC="' + jsMathURL + '/jsMath.js"></SCRIPT>'); 
 
function installJsMath()
{
    jsMath.Process(document);
}
 
addInitEvent(installJsMath);

Discussion

jsMath returns warnings

On DokuWiki 2008-05-05. jsMath returns warnings. It seems it has some conflicts with DokuWiki. See following:

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:\DokuWikiStick\dokuwiki\lib\plugins\syntax.php on line 134

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\DokuWikiStick\dokuwiki\lib\plugins\syntax.php on line 134

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\DokuWikiStick\dokuwiki\lib\plugins\syntax.php on line 134
Re: jsMath returns warnings

I think this was an error in getAllowedTypes(), now fixed.

Works fine now

It works now. Thank you.

Use Dollar Signs

I added support to use dollar notation instead of the <jsm> tags. So you can now conveniently do both

<jsm>\sum_i i</jsm>
$\sum_i i$

For this to work, you have to change the line near the beginning of the file.

define('JSMATH_USE_DOLLAR',1)

Is the original author ok with that change? We could make this an option accessible through the admin interface.

Error

Then trying to clear cache, dokuWiki return this error message:

“It looks like jsMath failed to set up properly (error code -7). I will try to keep going, but it could get ugly.”

What could be an error?

 
plugin/jsmath.txt · Last modified: 2009/01/04 17:19 by 91.78.56.130
 

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported

Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsDarcsXRefTranslate