DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:math_old

This is an old revision of the document!


(Old Version) Math Plugin (Expandable)

This is an old version of the mathmulti plugin, you may want to use the new version of this plugin instead.

experimental

The Math Plugin render mathematical expression (LaTex, plain text…) graphically (image) or as MathML.
By default it uses mimetex to process LaTex math expression to images.1)
It can be easily adapted/expanded to use other rendering tools (or math syntax). For instance one can use

Thanks to the nice work from Andreas Gohr with his sample info plugin, to the great Graphviz plugin by Carl-Christian Salvesen and to the latexrenderer Plugin by Alexander 'E-Razor' Krause, it was easy to code a light weight and expandable math expression plugin.

Before going any further, you may want to know that, plugins work only in the version 2005-07-01 or newer ones.

Also you may be interested to know more about the plugin mechanism itself: plugins.

Installation

1. Create Math Plugin Directory

cd /YOUR/DOKUWIKI/BASE/DIR/
mkdir lib/plugins/math

2. Download external tool

This base version of the math plugin make use of a stand alone program to render the Tex Math expression into a gif image.

  1. Download the package from mimetex
  2. Compile as specified
  3. Install/copy the program (renamed mimetex.cgi) into your cgi-bin directory.
  4. link your cgi-bin dir into the lib/plugins/math dir [cd lib/plugins/math; ln -s /PATH/TO/cgi-bin/ cgi-bin] or change the path to mimetex.cgi in the syntax.php script below.

Sorry for IIS server users and those who do not have command line access to their server… If you can update these instruction it would be great!

3. Add Plugin code

Create the following file lib/plugins/math/syntax.php with the following content.

<?php
/**
 * Math Plugin: Render Tex Math expression into images using mimetex
 *              http://www.forkosh.com/mimetex.html
 *              Can be easely adapted to render using latexrenderer, or itex2mml or plain2mml
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stephane Chamberland <stephane.chamberland@ec.gc.ca>
 */
 
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_math extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Stephane Chamberland',
            'email'  => 'stephane.chamberland@ec.gc.ca',
            'date'   => '2005-07-04',
            'name'   => 'Math Plugin',
            'desc'   => 'Render (la)Tex Math expretions as images (<math>x=frac{y^2}{2}</math>)',
            'url'    => 'http://www.dokuwiki.org/plugin:math',
        );
    }
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        //return 'substition';
        return 'formatting';
    }
 
   /**
    * Paragraph Type
    *
    * Defines how this syntax is handled regarding paragraphs. This is important
    * for correct XHTML nesting. Should return one of the following:
    *
    * 'normal' - The plugin can be used inside paragraphs
    * 'block'  - Open paragraphs need to be closed before plugin output
    * 'stack'  - Special case. Plugin wraps other paragraphs.
    *
    * @see Doku_Handler_Block
    */
    function getPType(){
        return 'normal';
    }
 
    /**
     * Where to sort in?
     */ 
    function getSort(){
        return 155;
    }
 
 
    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addEntryPattern('<math(?=.*\x3C/math\x3E)',$mode,'plugin_math');
    }
    function postConnect() {
        $this->Lexer->addExitPattern('</math>','plugin_math');
    }
 
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        switch ( $state ) {
            case DOKU_LEXER_UNMATCHED:
                $matches = preg_split('/>/u',$match,2);
                $matches[0] = trim($matches[0]);
                if ( trim($matches[0]) == '' ) {
                    $matches[0] = NULL;
                }
                // $matches[0] contains name of programming language if available
	        return array($matches[1],$matches[0]);
            break;
        }
        return TRUE;
    }
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml' && strlen($data[0]) > 1) {
	    if (is_null($data[1]) || $data[1] == 'tex' ) {
	        $renderer->doc .= $this->_mimetex($data[0]);
            //} else if ($data[1] == 'plain') {
            //} else if ($data[1] == 'itex') {
            //} else if ($data[1] == 'latex') {
            } else {
		//I guess it would be better to escape the character in this case
                $renderer->doc .= $data[0];
            }
            return true;
        }
        return false;
    }
 
    /**
     * Render math expression to gif using mimetex
     * http://www.forkosh.com/mimetex.html
     */
    function _mimetex($mymathexpr) {
	return '<img src="'.DOKU_BASE.'lib/plugins/math/cgi-bin/mimetex.cgi?'.
		$mymathexpr.'" alt="'.$mymathexpr.
		'" class="math"/>';
    }
 
}
?>

Syntax Example

<math tex>y=\frac{x}{2}</math>

Comments

Great job, had an easier time getting this one working then any other of the math plug-ins that I tried. I did have to change the return '<img src=“'.DOKU_BASE.'lib/plugins/math/cgi-bin/mimetex.cgi?'. line (near the very end of the syntax.php script) to match the location of my cgi-bin.
    function _mimetex($mymathexpr) {
	return '<img src="/cgi-bin/mimetex.cgi?'.
		$mymathexpr.'" alt="'.$mymathexpr.
		'" class="math"/>';
    }

How to make the output image file type is “gif”, not “bmp”? OK

1)
Please note that the rendering, using MimeTex does not use high quality fonts. For a better rendering you may prefer to use:
  • the latexrenderer plugin [A bit more complex to install, still use LaTex syntax for Maths]
  • or the Math Plugin by ChrisS [as simple, if not simpler, to install but use another mathematical syntax].
plugin/math_old.1183025897.txt.gz · Last modified: 2011-06-18 16:27 (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki