#native_company# #native_desc#
#native_cta#

Latex Renderer Class for linux

By Carlos Segura
on March 7, 2006

Version: 1.0

Type: Function

Category: Graphics

License: GNU General Public License

Description: Latex Renderer Class for linux without any other install, no cgi needed, only /usr/bin/latex /usr/bin/dvips and /usr/bin/convert

<?php

/*
    Latex Render Class 
    Based on http://www.linuxjournal.com/article/7870
    Copyright 2005 Carlos Segura
    [email protected]
    

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   Configuration: 
	create a temporal directory and set $TMP_DIR
	create a cache directory and set $CACHE_DIR
	set the path to: latex, dvips, convert
	set the relative path $URL_PATH to $CACHE_DIR from your webpage

   PmWiki configuration:
	copy this file in pmwiki_relative_path/local/
	edit pmwiki_relative_path/local/config.php
	add:
	
		Markup('{%', 'block',
	  	'/{%(.*?)%}/e',
  		"Keep(toLatex('$1'))");
		
		function toLatex($text){
		require_once('render.class.php');
		$text = "[tex]" . $text . "[/tex]";
		$render = new render();
		return $render->transform($text);
		}
	
	All text written with markup {$ latex formula $} will be rendered with latex

*/









class render {

 var $LATEX_PATH = "/usr/bin/latex";
 var $DVIPS_PATH = "/usr/bin/dvips";
 var $CONVERT_PATH = "/usr/bin/convert";
 var $TMP_DIR = "/srv/www/htdocs/pmwiki/local/tmp";
 var $CACHE_DIR = "/srv/www/htdocs/pmwiki/local/cache";
 var $URL_PATH = "/local/cache";
 function wrap($thunk) {  
    return <<<EOS
    documentclass[12pt]{article}
    % add additional packages here
    usepackage{amsmath}
    usepackage{amsfonts}
    usepackage{amssymb}
    usepackage{pst-plot}
    usepackage{color}
    pagestyle{empty}
    begin{document}
    $thunk
    end{document}
EOS;
}
 function transform($text) { 
// echo $text;
 $base_url = 'http' . (isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] == 'on' ? 's' : '' : '');
$base_url .= '://' .$_SERVER['HTTP_HOST'];
if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), ',/')) {
  $base_url .= "/$dir";
}
 preg_match_all("/[tex](.*?)[/tex]/si", $text, $matches);
  for ($i = 0; $i < count($matches[0]); $i++) {
    $position = strpos($text, $matches[0][$i]);
    $thunk = $matches[1][$i];
    $hash = md5($thunk);
    $full_name = $this->CACHE_DIR . "/" .
                 $hash . ".png";
    $url = $base_url . $this->URL_PATH . "/" .
           $hash . ".png";
    if (!is_file($full_name)) {
      $this->render_latex($thunk, $hash);
      $this->cleanup($hash);
    }
    $text = substr_replace($text,
      "<img src="$url" alt="Formula: $i" />",
      $position, strlen($matches[0][$i]));
  }
  return $text;
 }
 
 function render_latex($thunk, $hash) {
  $thunk = $this->wrap($thunk);
  $current_dir = getcwd();
  chdir($this->TMP_DIR);
  // create temporary LaTeX file
  $fp = fopen($this->TMP_DIR . "/$hash.tex", "w+");
  fputs($fp, $thunk);
  fclose($fp);
  // run LaTeX to create temporary DVI file
  $command = $this->LATEX_PATH .
             " --interaction=nonstopmode " .
             $hash . ".tex";
  exec($command);
  // run dvips to create temporary PS file
  $command = $this->DVIPS_PATH .
             " -E $hash" .
             ".dvi -o " . "$hash.ps";
  exec($command);
  // run PS file through ImageMagick to
  // create PNG file
  $command = $this->CONVERT_PATH .
             " -density 120 $hash.ps $hash.png";
  exec($command);
  // copy the file to the cache directory
  copy("$hash.png", $this->CACHE_DIR .
       "/$hash.png");
  chdir($current_dir);
}

function cleanup($hash) {
  $current_dir = getcwd();
  chdir($this->TMP_DIR);
  unlink($this->TMP_DIR . "/$hash.tex");
  unlink($this->TMP_DIR . "/$hash.aux");
  unlink($this->TMP_DIR . "/$hash.log");
  unlink($this->TMP_DIR . "/$hash.dvi");
  unlink($this->TMP_DIR . "/$hash.ps");
  unlink($this->TMP_DIR . "/$hash.png");
  chdir($current_dir);
}
}

?>