alexander Posted October 10, 2007 Report Posted October 10, 2007 quite tired of not being able to use latex to the extent we can use it to, so i have come up with yet again a brilliant idea (i am being sarcastic) our current latex-gif converter does not seem to be working all that well, i mean it does well for what we use it for, but it fails at making pictures (talking about the pst-plot plugin that is) so because i want to be able to do nice graphs and such, i'm planning a fork of the code (problem is, i don't have anywhere i can test it, so basically i'd have to test it here...) Basic thoughts, i will work on it this week and plan to finish before the weekend, or Saturday max. I need to figure out how i'm going to test it without bringing the main site to not be able to render [math] Also i have already tested my new way of converting latex to pdf, so it should work (you can see my newest test at http://hypography.com/forums/latex/test.gif ) (oh disregard the size there too, i purposely decreased it, but its not in what it is, its in how it does what it does that the magic is at \psset{unit=0.5cm} \begin{pspicture}(-4,-0.5)(4,8) \psgrid[subgriddiv=0,griddots=5, gridlabels=7pt](-4,-0.5)(4,8) \psline[linewidth=1pt]{->}(-4,0)(+4,0) \psline[linewidth=1pt]{->}(0,-0.5)(0,8) \psplot[plotstyle=curve, linewidth=0.5pt]{-4}{0.9}{10 x exp} \rput[l](1,7.5){$10^x$} \psplot[plotstyle=curve,linecolor=red, linewidth=0.5pt]{-4}{3}{2 x exp} \rput[l](2.2,7.5){\color{blue}$e^x$} \psplot[plotstyle=curve,linecolor=blue, linewidth=0.5pt]{-4}{2.05}{2.7183 x exp} \rput[l](3.2,7.5){\color{red}$2^x$} \rput(4,8.5){\color{white}change\normalcolor} \rput(-4,-1){\color{white}bounding box\normalcolor} \end{pspicture} Quote
alexander Posted October 10, 2007 Author Report Posted October 10, 2007 oh sorry i meant to say, i have compiled this really great neat little program that allows for magic like that to happen, its called tth (its now in /usr/local/bin) and it allows me (using their pre-configured bash scripts) to convert a tex doc into gif in one step (it does more then one in the background, but for the script, it only cares about uno) Quote
Buffy Posted October 10, 2007 Report Posted October 10, 2007 I'd recommend using a knife too! :) Off-topic: do you know of any good PD html-to-gif/jpeg converters? That code line is hopelessly branched,Buffy Quote
alexander Posted October 15, 2007 Author Report Posted October 15, 2007 ok, i'm testing it later on today (like way later so there's as few people on as possible) Quote
alexander Posted October 15, 2007 Author Report Posted October 15, 2007 oh if any of you are interested, here's the new code, i ran it through php, it does not find any syntax errors, but its no guarantee that i havent missed something...<? /* * class.latex.php: Small class for rendering LaTeX formulae * Copyright Dave Moxey (e-mail: [email protected]) * Copyleft Niksoft (e-mail: [email protected]) * * vbLatex 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. * * vbLatex 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 vbLatex; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // SEE BELOW FOR CONFIGURATION // Input environments: define('LATEX_INPUT_NORM', 1); // Normal 'displaymath' environment define('LATEX_INPUT_INLINE', 2); // For inline text define('LATEX_INPUT_CHEM', 3); // Chemical equations (requires mhchem) // Output environments: //unfortunately, because IE still does not support png transparency completely, we have to stick to gif, therefore i am not putting in the functionality :doh: (or rather taking it out) define('LATEX_OUTPUT_GIF', 'gif'); // Errors: define('LATEX_ERROR_TOOLONG', -1); // Message too long define('LATEX_ERROR_BADWORD', -2); // String included a bad word define('LATEX_ERROR_NODIR', -3); // Directory(ies) don't exist define('LATEX_ERROR_NOPERM', -4); // Directory(ies) aren't writeable define('LATEX_ERROR_TOOBIG', -5); // Dimensions of resultant image are too big define('LATEX_ERROR_SYNTAX', -6); // Syntax error define('LATEX_ERROR_COPY', -7); // Copy error class Latex { /**************** BEGIN CONFIGURATION *****************/ /* * Path information: this is important! * Alter the information below according to your system. * 'latex': path to LaTeX application * 'dvipng': path to dvipng application * * 'base': base directory of your latex/ directory, WITHOUT trailing slash * 'baseurl': the URL to your latex directory, WITHOUT trailing slash * * For example, if my LaTeX directory was /home/dave/public_html/latex then: * 'base' => '/home/dave/public_html/latex' * 'baseurl' => '/~dave/latex' */ var $path = array( // Applications 'latex' => '/usr/bin/latex', 'dvips' => '/usr/bin/dvips', 'ps2gif' => '/usr/local/bin/ps2gif', // Base directory 'base' => '/home/sciencef/public_html/latex', 'baseurl' => 'http://hypography.com/forums/latex' ); /* * Presentation information: * 'class': LaTeX class to use in documentclass call * 'fontsize': Font size to use in documentclass call * 'extrapackages': An array of extra packages you might want to use * 'format': Default output format. LATEX_OUTPUT_PNG or LATEX_OUTPUT_GIF * for GIF and PNG respectively. */ var $display = array( 'class' => 'article', 'fontsize' => '12pt', 'extrapackages' => array('amsmath', 'amsfonts', 'amssymb', 'color', 'cancel', 'graphics', 'amstext', 'array', 'at', 'caption2', 'graphicx', 'units', 'pst-plot'), 'format' => LATEX_OUTPUT_GIF ); /* * dvipng options: * 'background': You can either choose transparent for a transparent background, or * a colour. The format of the colour must be 'rgb 1.0 0.0 0.0'; for * example, this defines red. See the example below. * 'foreground': Not implemented yet. * 'density': Density to render image at. 100 is about right for most applications. * 'gamma': Apply gamma to image - 1.0 to leave untouched. */ var $dvipng = array( 'background' => 'Transparent', //'background' => "'rgb 1.0 0.0 0.0'", 'foreground' => false, 'density' => '100', 'gamma' => '1.4' ); /********* Security Options **********/ // Maximum lengths of input string for each type var $str_length = array( LATEX_INPUT_NORM => 1024, LATEX_INPUT_INLINE => 768, LATEX_INPUT_CHEM => 512 ); // Maximum image dimensions var $image_dim = array( LATEX_INPUT_NORM => array(1024, 768), LATEX_INPUT_INLINE => array(1024, 768), LATEX_INPUT_CHEM => array(1024, 768) ); // Barred commands: taken from LatexRender - best not to alter var $barred_commands = 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' ); /*********** END CONFIGURATION **********/ var $tmp_filename; var $md5hash; var $method = LATEX_INPUT_NORM; var $currentdir = ''; function Latex($inputmethod, $outputmethod=FALSE) { $this->method = $inputmethod; if ($outputmethod) $this->display['format'] = $outputmethod; $this->path['tmp'] = $this->path['base'].'/tmp'; $this->path['img'] = $this->path['base'].'/img'; } function renderLatex($latexstring) { $this->tmp_filename = md5(rand()); $this->currentdir = getcwd(); $path = $this->path; $latexstring = trim($latexstring); $this->md5hash = md5($latexstring); $filename = $this->md5hash.'-'.$this->method.'.'.$this->display['format']; // Check existance of correct folders/permissions if (!is_dir($path['base']) || !is_dir($path['tmp']) || !is_dir($path['img'])) return $this->error(LATEX_ERROR_NODIR, ''); if (!is_writeable($path['tmp']) || !is_writeable($path['img'])) return $this->error(LATEX_ERROR_NOPERM, ''); // Check whether this thing already exists if (is_file($path['img'].'/'.$filename)) { touch($path['img'].'/'.$filename); return $this->path['baseurl']."/img/".$filename; } // Do security checks if ($this->str_length[$this->method] != 0 && strlen($latexstring) > $this->str_length[$this->method]) return $this->error(LATEX_ERROR_TOOLONG, strlen($latexstring)); foreach ($this->barred_commands as $command) { if (stristr($latexstring, $command)) return $this->error(LATEX_ERROR_BADWORD, $command); } // Store current directory and chdir($path['tmp']); // Wrap out string as snug as a bug in a rug and output it to something $tmp_file = fopen($path['tmp'].'/'.$this->tmp_filename.'.tex', 'w'); fwrite($tmp_file, $this->wrapFormula($latexstring)); fclose($tmp_file); // Compile command $command = $path['latex']." --interaction=nonstopmode ".$this->tmp_filename.".tex && ". $path['dvips']." -q -D ".$this->dvipng['density']." -o ".$this->tmp_filename.".ps ".$this->tmp_filename.".dvi && ". $path['ps2gif']." ".$this->tmp_filename.".ps $filename"; exec($command); if (!is_readable($filename)) return $this->error(LATEX_ERROR_SYNTAX, '', true); // Security check: image height/width $imageinfo = @getimagesize($filename); if ($this->image_dim[$this->method] != NULL && ($imageinfo[0] > $this->image_dim[$this->method][0] || $imageinfo[1] > $this->image_dim[$this->method][1])) return $this->error(LATEX_ERROR_TOOBIG, $imageinfo[0].'x'.$imageinfo[1], true); $copy = @copy($filename, $path['img'].'/'.$filename); if (!$copy) return $this->error(LATEX_ERROR_COPY, '', true); $this->cleanTmpDir(); chdir($this->currentdir); return $this->path['baseurl']."/img/".$filename; } function wrapFormula($latexstring) { $wrap = "documentclass[".$this->display['fontsize']."]{".$this->display['class']."}n"; $wrap .= "pagestyle{empty}n"; foreach ($this->display['extrapackages'] as $extra) $wrap .= "usepackage{".$extra."}n"; switch ($this->method) { case LATEX_INPUT_CHEM: $wrap .= "usepackage[version=2]{mhchem}n"; break; } $wrap .= "begin{document}n"; switch ($this->method) { case LATEX_INPUT_NORM: $wrap .= "[ $latexstring ]n"; break; case LATEX_INPUT_INLINE: $wrap .= "$ $latexstring $n"; break; case LATEX_INPUT_CHEM: $wrap .= "ce{".$latexstring."}n"; break; } $wrap .= "end{document}n"; return $wrap; } function error($errorcode, $detail, $chdir=false) { $err = 'Error: '; switch ($errorcode) { case LATEX_ERROR_TOOLONG: $err .= "String is too long (".$detail.", limit ".$this->str_length[$this->method].")"; break; case LATEX_ERROR_BADWORD: $err .= 'Restricted command found ('.$detail.')'; break; case LATEX_ERROR_NODIR: $err .= 'One or more directories do not exist'; break; case LATEX_ERROR_NOPERM: $err .= "Can't write to directory"; break; case LATEX_ERROR_TOOBIG: $err .= 'Image is too big ('.$detail.', limit '.implode('x', $this->image_dim[$this->method]).')'; break; case LATEX_ERROR_SYNTAX: $err .= 'Syntax error'; break; case LATEX_ERROR_COPY: $err .= "Couldn't copy temporary file"; break; } if ($chdir) { chdir($this->currentdir); $this->cleanTmpDir(); } return $err; } function cleanTmpDir() { @unlink($this->path['tmp'].'/'.$this->tmp_filename.'.tex'); @unlink($this->path['tmp'].'/'.$this->tmp_filename.'.aux'); @unlink($this->path['tmp'].'/'.$this->tmp_filename.'.log'); @unlink($this->path['tmp'].'/'.$this->tmp_filename.'.dvi'); @unlink($this->path['tmp'].'/'.$this->tmp_filename.'.ps'); @unlink($this->path['tmp'].'/'.$this->tmp_filename.'.'.$this->display['format']); @unlink($this->path['tmp'].'/'.$this->md5hash.'-'.$this->method.'.'.$this->display['format']); } } ?> if you guys see anything out of the ordinary, let me know :evil: Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.