Latex Helper June 14, 2006
Posted by rossoft in CakePHP.trackback
Helper for rendering LaTeX formulas easy
Usage: in your view,
echo $latex->render(’\displaystyle\int_{0}^{1}\frac{x^{4}\left(1-x\right)^{4}}{1+x^{2}}dx=
\frac{25}{8}-\pi’);
It will create an IMG tag to the destination URL of the image. You need a folder in webroot writable by the web sever
1. Download latexrender, and save class.latexrender.php to vendors/latexrender/
2. Edit class.latexrender.php and change the paths to your apps imagemagik and latex
3. Copy this app/views/helpers/latex.php and change the paths of your temp folder and the destination folder of images (writable by webserver under wwwroot)
<?php
/**
* Latex Helper
* Renders some LaTeX formula to a image
* and returns the <IMG> tag to it
*
* You need to set the destination directory under webroot folder
* to store the images (set write permissions on it)
*
* @author RosSoft
* @version 0.1
* @license MIT
*
* @link http://www.mayer.dial.pipex.com/tex.htm LatexRender
*
*/
class LatexHelper extends Helper
{
var $_instance=null;
var $images_url=’img/latex’; // under webroot folder
var $tmp_dir=’/tmp’;
/**
* Renders the formula and returns the <IMG> tag
*
* @param string $formula LaTeX Formula
* @return string <IMG> tag to the rendered image
*/
function render($formula)
{
$this->_init();
$url = $this->_instance->getFormulaURL($formula);
$alt = htmlentities($formula, ENT_QUOTES);
$alt= str_replace(”\r”,”
”,$alt);
$alt= str_replace(”\n”,”
”,$alt);
if ($url != false)
{
return “<img src=’”.$url.”‘ title=’”.$alt.”‘ alt=’”.$alt.”‘ align=’middle’ />”;
}
else
{
return ‘[unparseable or potentially dangerous latex formula]‘;
}
}
function _init()
{
if (! $this->_instance)
{
$dest_path=WWW_ROOT. $this->images_url;
$dest_url=$this->webroot . $this->images_url;
//edit class.latexrender and change the paths to your apps
vendor(’latexrender/class.latexrender’);
$this->_instance= new LatexRender($dest_path,$dest_url,$this->tmp_dir);
}
}
}
?>
Comments»
No comments yet — be the first.