math equations (with latex)

This forum is now closed as part of retiring phpBB2
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
moldo
Registered User
Posts: 12
Joined: Thu Aug 02, 2007 2:53 pm
Location: Roumanian
Contact:

Re: math equations (with latex)

Post by moldo » Fri Aug 03, 2007 1:27 pm

1. no is operating system is host
2.yes
3.no
4.yes
5. text code is [tex]a^b[/tex]
6.yes

stevem
Registered User
Posts: 398
Joined: Sun Aug 25, 2002 1:59 pm

Re: math equations (with latex)

Post by stevem » Fri Aug 03, 2007 1:52 pm

1. Now don't be silly. Is it Windows or Linux and which version of these?
3. You MUST install Ghostscript & Imagemagick for the system to work. Nothing will happen until you do so.
4. What does 'yes' mean? What files were created in /tnp? Have you looked at them? Are they correct? Are there any errors? Surely there can't be gif files since you haven't installed all the programs.

For the very last time, you MUST give me information; I cannot guess what is going on so you have to tell me. So:
a. Windows or Linux and which version?
b. install Ghostscript & Imagemagick.
c. tell me exactly what files are created in /tmp once you have changed class.latexrender.php. View them to see if they are correct and tell me abou them.
d. Some tex codes don't give the error. Is it only [tex]a^b[/tex] that does this?

moldo
Registered User
Posts: 12
Joined: Thu Aug 02, 2007 2:53 pm
Location: Roumanian
Contact:

Re: math equations (with latex)

Post by moldo » Fri Aug 03, 2007 4:15 pm

a.I,Windows
b.How install Ghostscript & Imagemagick????.I don't know this
c. in tmp:fc2534ee3311768e61d31b7cd8474885.tex,ed36d7c9ea98c5d8ed3e739f298c314c.tex, etc and pictures nothing

class.latexrender.php:

Code: Select all

<?php
/**
 * LaTeX Rendering Class
 * Copyright (C) 2003  Benjamin Zeiss <zeiss@math.uni-goettingen.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * --------------------------------------------------------------------
 * @author Benjamin Zeiss <zeiss@math.uni-goettingen.de>
 * @version v0.8
 * @package latexrender
 *
 */

class LatexRender {

    // ====================================================================================
    // Variable Definitions
   var $_picture_path = "";
    var $_picture_path_httpd = "";
    var $_tmp_dir = "";
    // i was too lazy to write mutator functions for every single program used
    // just access it outside the class or change it here if nescessary
 var $_latex_path = "/usr/bin/latex";
    var $_dvips_path = "/usr/bin/dvips";
    var $_convert_path = "/usr/bin/convert";
    var $_identify_path="/usr/bin/identify";
    var $_xsize_limit = 500;
    var $_ysize_limit = 500;
    var $_string_length_limit = 500;
	var $_font_size = 10;
	var $_latexclass = "article"; //install extarticle class if you wish to have smaller font sizes
    var $_tmp_filename;
	var $_image_format = "png"; //change to png if you prefer
    // this most certainly needs to be extended. in the long term it is planned to use
    // a positive list for more security. this is hopefully enough for now. i'd be glad
    // to receive more bad tags !
    var $_latex_tags_blacklist = array(
        "include","def","command","loop","repeat","open","toks","output","input",
        "catcode","name","^^",
        "\\every","\\errhelp","\\errorstopmode","\\scrollmode","\\nonstopmode","\\batchmode",
        "\\read","\\write","csname","\\newhelp","\\uppercase", "\\lowercase","\\relax","\\aftergroup",
        "\\afterassignment","\\expandafter","\\noexpand","\\special"
        );
    var $_errorcode = 0;
	var $_errorextra = "";


    // ====================================================================================
    // constructor
    // ====================================================================================

    /**
     * Initializes the class
     *
     * @param string path where the rendered pictures should be stored
     * @param string same path, but from the httpd chroot
     */
    function LatexRender($picture_path,$picture_path_httpd,$tmp_dir) {
        $this->_picture_path = $picture_path;
        $this->_picture_path_httpd = $picture_path_httpd;
        $this->_tmp_dir = $tmp_dir;
        $this->_tmp_filename = md5(rand());
    }

    // ====================================================================================
    // public functions
    // ====================================================================================

    /**
     * Picture path Mutator function
     *
     * @param string sets the current picture path to a new location
     */
    function setPicturePath($name) {
        $this->_picture_path = $name;
    }

    /**
     * Picture path Mutator function
     *
     * @returns the current picture path
     */
    function getPicturePath() {
        return $this->_picture_path;
    }

    /**
     * Picture path HTTPD Mutator function
     *
     * @param string sets the current httpd picture path to a new location
     */
    function setPicturePathHTTPD($name) {
        $this->_picture_path_httpd = $name;
    }

    /**
     * Picture path HTTPD Mutator function
     *
     * @returns the current picture path
     */
    function getPicturePathHTTPD() {
        return $this->_picture_path_httpd;
    }

    /**
     * Tries to match the LaTeX Formula given as argument against the
     * formula cache. If the picture has not been rendered before, it'll
     * try to render the formula and drop it in the picture cache directory.
     *
     * @param string formula in LaTeX format
     * @returns the webserver based URL to a picture which contains the
     * requested LaTeX formula. If anything fails, the resultvalue is false.
     */
    function getFormulaURL($latex_formula) {
        // circumvent certain security functions of web-software which
        // is pretty pointless right here
        $latex_formula = preg_replace("/>/i", ">", $latex_formula);
        $latex_formula = preg_replace("/</i", "<", $latex_formula);

        $formula_hash = md5($latex_formula);

        $filename = $formula_hash.".".$this->_image_format;
        $full_path_filename = $this->getPicturePath()."/".$filename;

        if (is_file($full_path_filename)) {
            return $this->getPicturePathHTTPD()."/".$filename;
        } else {
            // security filter: reject too long formulas
            if (strlen($latex_formula) > $this->_string_length_limit) {
            	$this->_errorcode = 1;
                return false;
            }

            // security filter: try to match against LaTeX-Tags Blacklist
            for ($i=0;$i<sizeof($this->_latex_tags_blacklist);$i++) {
                if (stristr($latex_formula,$this->_latex_tags_blacklist[$i])) {
                	$this->_errorcode = 2;
                    return false;
                }
            }

            // security checks assume correct formula, let's render it
            if ($this->renderLatex($latex_formula)) {
                return $this->getPicturePathHTTPD()."/".$filename;
            } else {
                // uncomment if required
                // $this->_errorcode = 3;
                return false;
            }
        }
    }

    // ====================================================================================
    // private functions
    // ====================================================================================

    /**
     * wraps a minimalistic LaTeX document around the formula and returns a string
     * containing the whole document as string. Customize if you want other fonts for
     * example.
     *
     * @param string formula in LaTeX format
     * @returns minimalistic LaTeX document containing the given formula
     */
    function wrap_formula($latex_formula) {
        $string  = "\documentclass[".$this->_font_size."pt]{".$this->_latexclass."}\n";
        $string .= "\usepackage[latin1]{inputenc}\n";
        $string .= "\usepackage{amsmath}\n";
        $string .= "\usepackage{amsfonts}\n";
        $string .= "\usepackage{amssymb}\n";
        $string .= "\pagestyle{empty}\n";
        $string .= "\begin{document}\n";
        $string .= "$".$latex_formula."$\n";
        $string .= "\end{document}\n";

        return $string;
    }

    /**
     * returns the dimensions of a picture file using 'identify' of the
     * imagemagick tools. The resulting array can be adressed with either
     * $dim[0] / $dim[1] or $dim["x"] / $dim["y"]
     *
     * @param string path to a picture
     * @returns array containing the picture dimensions
     */
    function getDimensions($filename) {
        $output=exec($this->_identify_path." ".$filename);
        $result=explode(" ",$output);
        $dim=explode("x",$result[2]);
        $dim["x"] = $dim[0];
        $dim["y"] = $dim[1];

        return $dim;
    }

    /**
     * Renders a LaTeX formula by the using the following method:
     *  - write the formula into a wrapped tex-file in a temporary directory
     *    and change to it
     *  - Create a DVI file using latex (tetex)
     *  - Convert DVI file to Postscript (PS) using dvips (tetex)
     *  - convert, trim and add transparancy by using 'convert' from the
     *    imagemagick package.
     *  - Save the resulting image to the picture cache directory using an
     *    md5 hash as filename. Already rendered formulas can be found directly
     *    this way.
     *
     * @param string LaTeX formula
     * @returns true if the picture has been successfully saved to the picture
     *          cache directory
     */
    function renderLatex($latex_formula) {
        $latex_document = $this->wrap_formula($latex_formula);

        $current_dir = getcwd();

        chdir($this->_tmp_dir);

        // create temporary latex file
        $fp = fopen($this->_tmp_dir."/".$this->_tmp_filename.".tex","a+");
        fputs($fp,$latex_document);
        fclose($fp);

        // create temporary dvi file
echo "Temp directory is ".$this->_tmp_dir."<br>";

        if (!$status_code) { $this->cleanTemporaryDirectory(); chdir($current_dir); $this->_errorcode = 4; return false; }

        // convert dvi file to postscript using dvips
        $command = $this->_dvips_path." -E ".$this->_tmp_filename.".dvi"." -o ".$this->_tmp_filename.".ps";
        $status_code = exec($command);

        // imagemagick convert ps to image and trim picture
        $command = $this->_convert_path." -density ".$this->_formula_density.
                    " -trim -transparent \"#FFFFFF\" ".$this->_tmp_filename.".ps ".
                    $this->_tmp_filename.".".$this->_image_format;

        $status_code = exec($command);

        // test picture for correct dimensions
        $dim = $this->getDimensions($this->_tmp_filename.".".$this->_image_format);

        if ( ($dim["x"] > $this->_xsize_limit) or ($dim["y"] > $this->_ysize_limit)) {
            $this->cleanTemporaryDirectory();
            chdir($current_dir);
            $this->_errorcode = 5;
            $this->_errorextra = ": " . $dim["x"] . "x" . number_format($dim["y"],0,"","");
            return false;
        }

        // copy temporary formula file to cahed formula directory
        $latex_hash = md5($latex_formula);
        $filename = $this->getPicturePath()."/".$latex_hash.".".$this->_image_format;

        $status_code = copy($this->_tmp_filename.".".$this->_image_format,$filename);

        $this->cleanTemporaryDirectory();

        if (!$status_code) { chdir($current_dir); $this->_errorcode = 6; return false; }
        chdir($current_dir);

        return true;
    }

    /**
     * Cleans the temporary directory
     */
     function cleanTemporaryDirectory() {
        $current_dir = getcwd();
        chdir($this->_tmp_dir);

       // unlink($this->_tmp_dir."/".$this->_tmp_filename.".tex");
// unlink($this->_tmp_dir."/".$this->_tmp_filename.".aux");
// unlink($this->_tmp_dir."/".$this->_tmp_filename.".log");
// unlink($this->_tmp_dir."/".$this->_tmp_filename.".dvi");
// unlink($this->_tmp_dir."/".$this->_tmp_filename.".ps");
// unlink($this->_tmp_dir."/".$this->_tmp_filename.".".$this->_image_format);

        chdir($current_dir);
    }

}

?>
phpbb_hook_2.php:

Code: Select all

<?php
/**
 * LaTeX Rendering Class - PHPBB Hook
 * Copyright (C) 2003  Benjamin Zeiss <zeiss@math.uni-goettingen.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * --------------------------------------------------------------------
 * @author Benjamin Zeiss <zeiss@math.uni-goettingen.de>
 * @version v0.8
 * @package latexrender
 *
 */
    // --------------------------------------------------------------------------------------------------
    // adjust this to match your system configuration
    $latexrender_path = "/storage0/home/scoalaonline.evonet.ro/www/forum/latexrender";
    $latexrender_path_http = "/forum/latexrender/";

    // --------------------------------------------------------------------------------------------------
     include_once($latexrender_path."/class.latexrender.php");

    preg_match_all("#\[tex:$uid\](.*?)\[/tex:$uid\]#si",$text,$tex_matches);

    $latex = new LatexRender($latexrender_path."/pictures",$latexrender_path_http."/pictures",$latexrender_path."/tmp");

    for ($i=0; $i < count($tex_matches[0]); $i++) {
        $pos = strpos($text, $tex_matches[0][$i]);
        $alt_latex_formula=$tex_matches[1][$i];;
        $latex_formula = "\displaystyle ".$tex_matches[1][$i];

      $alt_latex_formula = htmlentities($alt_latex_formula, ENT_QUOTES);
      $alt_latex_formula = str_replace("\r","&#13;",$alt_latex_formula);
      $alt_latex_formula = str_replace("\n","&#10;",$alt_latex_formula);

   if (trim($latex_formula)=="") {
         $url=false;
      } else {
          $url = $latex->getFormulaURL($latex_formula);
      }

        if ($url != false) {
            $text = substr_replace($text, "<img src='".$url."' title='".$alt_latex_formula."' alt='".$alt_latex_formula."' align='absmiddle'>",$pos,strlen($tex_matches[0][$i]));
        } else {
            $text = substr_replace($text, "[unparseable or potentially dangerous latex formula]",$pos,strlen($tex_matches[0][$i]));
        }
    }

    preg_match_all("#\\\$:$uid(.*?)\\\$:$uid#si",$text,$tex_matches2);

    for ($i=0; $i < count($tex_matches2[0]); $i++) {
        $pos = strpos($text, $tex_matches2[0][$i]);
        $latex_formula = $tex_matches2[1][$i];

      $alt_latex_formula = htmlentities($latex_formula, ENT_QUOTES);
      $alt_latex_formula = str_replace("\r","&#13;",$alt_latex_formula);
      $alt_latex_formula = str_replace("\n","&#10;",$alt_latex_formula);

   if (trim($latex_formula)=="") {
         $url=false;
      } else {
          $url = $latex->getFormulaURL($latex_formula);
      }

        if ($url != false) {
            $text = substr_replace($text, "<img src='".$url."' title='".$alt_latex_formula."' alt='".$alt_latex_formula."' align='absmiddle'>",$pos,strlen($tex_matches2[0][$i]));
        } else {
            $text = substr_replace($text, "[unparseable or potentially dangerous latex formula]",$pos,strlen($tex_matches2[0][$i]));
        }
    }

    preg_match_all("#\\\\\[:$uid(.*?)\\\\\]:$uid#si",$text,$tex_matches3);

    for ($i=0; $i < count($tex_matches3[0]); $i++) {
        $pos = strpos($text, $tex_matches3[0][$i]);
        $alt_latex_formula=$tex_matches3[1][$i];;
        $latex_formula = "\displaystyle".$tex_matches3[1][$i];

      $alt_latex_formula = htmlentities($alt_latex_formula, ENT_QUOTES);
      $alt_latex_formula = str_replace("\r","&#13;",$alt_latex_formula);
      $alt_latex_formula = str_replace("\n","&#10;",$alt_latex_formula);

   if (trim($latex_formula)=="") {
         $url=false;
      } else {
          $url = $latex->getFormulaURL($latex_formula);
      }

        if ($url != false) {
            $text = substr_replace($text, "<center><img src='".$url."' title='".$alt_latex_formula."' alt='".$alt_latex_formula."' align='absmiddle'></center>",$pos,strlen($tex_matches3[0][$i]));
        } else {
            $text = substr_replace($text, "[unparseable or potentially dangerous latex formula]",$pos,strlen($tex_matches3[0][$i]));
        }
    }
?>
/latexrender/tmp
/latexrender/pictures
permissions are ok (777)

I known that maybe I am annoying,but me want this site :cry:

stevem
Registered User
Posts: 398
Joined: Sun Aug 25, 2002 1:59 pm

Re: math equations (with latex)

Post by stevem » Fri Aug 03, 2007 6:21 pm

moldo wrote:a.I,Windows
moldo wrote:/latexrender/tmp
/latexrender/pictures
permissions are ok (777)
These two don't agree! 777 is for Linux! So I ask again
Which operating system do you have on the server? If it's Linux then which one?
If it's Windows then you must change the paths as it says in readme:
# Adjust the paths in class.latexrender.php, if necessary, to point to the latex, dvips and ImageMagick executables
# var $_latex_path = "/usr/bin/latex";
# var $_dvips_path = "/usr/bin/dvips";
# var $_convert_path = "/usr/bin/convert";
# var $_identify_path="/usr/bin/identify";
# For Windows the paths in class.latexrender.php must use \\ or / not just a single \
# For example
# var $_latex_path = "C:\\texmf\\miktex\\bin\\latex.exe";
# or
# var $_latex_path = "C:/texmf/miktex/bin/latex.exe";
moldo wrote:b.How install Ghostscript & Imagemagick????.I don't know this
I have answered this already but it does depend on your operating system which is still unclear
stevem wrote:... difficult to answer as I have so little to go on. Is it a server? Does it have a control panel where you install software? Obviously it all depends on what operating system you use.

If you can't find the installation program for your system (RPM or whatever) you can get the binaries from
Ghostscript is downloadable from http://pages.cs.wisc.edu/~ghost/
ImageMagick is downloadable from http://www.imagemagick.com/www/binary-releases.html
moldo wrote:c. in tmp:fc2534ee3311768e61d31b7cd8474885.tex,ed36d7c9ea98c5d8ed3e739f298c314c.tex, etc and pictures nothing
That means latex & dvips haven't worked so you must get their paths correct as in the readme.txt above.

moldo
Registered User
Posts: 12
Joined: Thu Aug 02, 2007 2:53 pm
Location: Roumanian
Contact:

Re: math equations (with latex)

Post by moldo » Sat Aug 04, 2007 5:46 pm

Appear error this ,now

Code: Select all

Warning: exec() has been disabled for security reasons in /www/8tt.org/m/a/t/mathlinks/htdocs/latexrender/class.latexrender.php on line 241

stevem
Registered User
Posts: 398
Joined: Sun Aug 25, 2002 1:59 pm

Re: math equations (with latex)

Post by stevem » Sat Aug 04, 2007 6:22 pm

You'll have to enable exec() for LatexRender to work. You can access the setting in php.ini as root/administrator and will likely require restarting Apache or rebooting the server.

moldo
Registered User
Posts: 12
Joined: Thu Aug 02, 2007 2:53 pm
Location: Roumanian
Contact:

Re: math equations (with latex)

Post by moldo » Sat Aug 04, 2007 7:35 pm

Code: Select all

Warning: copy() [function.copy]: Unable to access 8e1007800d2fd2254ee08354f164d3bf.gif in /www/8tt.org/m/a/t/mathlinks/htdocs/latexrender/class.latexrender.php on line 270

Warning: copy(8e1007800d2fd2254ee08354f164d3bf.gif) [function.copy]: failed to open stream: No such file or directory in /www/8tt.org/m/a/t/mathlinks/htdocs/latexrender/class.latexrender.php on line 270

stevem
Registered User
Posts: 398
Joined: Sun Aug 25, 2002 1:59 pm

Re: math equations (with latex)

Post by stevem » Sat Aug 04, 2007 8:00 pm

Why won't you say what you've done? I cannot know what you've done. All the error message says is that the gif hasn't been created but I don't know if you have enabled exec() or installed Ghostscript & ImageMagick. What files are created in /tmp?

moldo
Registered User
Posts: 12
Joined: Thu Aug 02, 2007 2:53 pm
Location: Roumanian
Contact:

Re: math equations (with latex)

Post by moldo » Sun Aug 05, 2007 2:03 pm

not me let that put Ghostscript & ImageMagick on servar(host).Pls tell step why instala for Ghostscript & ImageMagick.I this can not
My website isn`t on my localhost, it is hosted on a public server

stevem
Registered User
Posts: 398
Joined: Sun Aug 25, 2002 1:59 pm

Re: math equations (with latex)

Post by stevem » Sun Aug 05, 2007 7:56 pm

You have to install Ghostscript & Imagemagick on the public server since they make the image. LaTeX creates the dvi file, dvips converts it to a ps file and Ghostscript & Imagemagick convert the ps file to a gif.
All the work must be done on the server as that is where the forum is and where users put messages. So the programs that do tex --> dvi --> ps -->gif need to be installed on the server.
All the programs are free and work on any operating system and are used by lots of servers.

moldo
Registered User
Posts: 12
Joined: Thu Aug 02, 2007 2:53 pm
Location: Roumanian
Contact:

Re: math equations (with latex)

Post by moldo » Sun Aug 05, 2007 8:40 pm

Consequently,Latex not intall on a public server?

stevem
Registered User
Posts: 398
Joined: Sun Aug 25, 2002 1:59 pm

Re: math equations (with latex)

Post by stevem » Sun Aug 05, 2007 8:44 pm

Yes you need LaTeX installed on the server as well.

moldo
Registered User
Posts: 12
Joined: Thu Aug 02, 2007 2:53 pm
Location: Roumanian
Contact:

Re: math equations (with latex)

Post by moldo » Sun Aug 05, 2007 8:54 pm

OK.Thank you :cry: :cry:

MB_MathemaTeX
Registered User
Posts: 15
Joined: Wed Feb 15, 2006 10:52 am
Contact:

Re: math equations (with latex)

Post by MB_MathemaTeX » Thu Aug 16, 2007 6:27 pm

Hello, I was using latexrender on phpBB2 and I have to use now phpBB3. Il haven't any problem for the basic installation (thx to stevem for this post).

My problem is with the old modification I have made for using $ et $$ instead of [tex]and [/tex].

The problem is with the new bitfield parameter. If the message doesn't contain any bbcode (only dollars for example) then the function bbcode_second_pass isn't launched and so like the phpbb_hook_2 file.

Is there a good solution for that ?
MB :: MathemaTeX ::

stevem
Registered User
Posts: 398
Joined: Sun Aug 25, 2002 1:59 pm

Re: math equations (with latex)

Post by stevem » Thu Aug 16, 2007 6:47 pm

That's a problem with not sticking to the conventions that phpBB uses. I haven't tried it so this is speculative, but how about adding code before phpBB checks for bbcode that:
1. changes $...$ to [tex]...[/tex] or
2. adds in a harmless [ b][ /b] somewhere. If that's not good enough then you could put [ b]...[ /b] round the $...$ as it won't have any effect on the image.

Post Reply

Return to “[2.0.x] MOD Requests”

Who is online

Users browsing this forum: No registered users and 7 guests