• Welcome to League Of Reason Forums! Please read the rules before posting.
    If you are willing and able please consider making a donation to help with site overheads.
    Donations can be made via here

RE: Inserting Equations

Fordi

New Member
arg-fallbackName="Fordi"/>
This is in response to the sticky about equation insertion from AndromedasWake. That thread is locked, and rightly so, as it wouldn't do to have an instructional post distracted by further comment.

Still...

I'm looking into alternatives to off-site LaTeX formatting, including options such as HeVeA. HeVeA is a smallish program used to translate LaTeX into straight HTML. It's pretty extensible, but obviously can't be complete.

I don't know how much control the admins have over this server, but, I was just able to build a copy on my Dreamhost account with very little trouble. The same could probably be done with almost any LaTeX renderer, and I'd be happy to assist (this - programming, building, tying stuff to web applications - being my area of expertise) if you'd like a LaTeX formatter / renderer built into the site. No charge; I'd be building a lib / UI for my own purposes as well.

Additionally, if anyone else has insight into this, tell me; I have a very good rich-edit lib that I'd like to add LaTeX / complex expression support to as well.
 
arg-fallbackName="Fordi"/>
Fordi said:
I'm looking into alternatives to off-site LaTeX formatting...

Just finished off a texToImg and mathTexToImg (provides all the document / font formatting for you, so you only need provide the math internals) for PHP, based on the textogif Perl script.

...

Can't attach a file, eh? I'll just post in the code.
Code:
<?php
if (!defined('CR')) define('CR',"\r\n");
class texToImg {
//
//                          by John Walker
//                      http://www.fourmilab.ch/
//
//		PHP object-oriented Port by Bryan Elliott
	var $version = '1.1 (2003-11-07)';
//
//
//   Converts a LaTeX string containing equations(s) into an image file for
//   embedding into an HTML document.  The black and white image of the
//   equation is created at high resolution and then resampled to the
//   target resolution to antialias what would otherwise be jagged
//   edges.
//
//   Online documentation with sample output is available on the Web
//   at http://www.fourmilab.ch/webtools/texToImg/
//
//   Write your equation (or anything else you can typeset with LaTeX)
//   in a file like:
//
//       \documentclass[12pt]{article}
//       \pagestyle{empty}
//       \begin{document}
//
//       \begin{displaymath}
//       \bf  % Compiled formulae often look better in boldface
//       \int H(x,x')\psi(x')dx' = -\frac{\hbar2}{2m}\frac{d2}{dx2}
//                                 \psi(x)+V(x)\psi(x)
//       \end{displaymath}
//
//       \end{document}
//
//   The "\pagestyle{empty}" is required to avoid generating a huge
//   image with a page number at the bottom.
//
//   Then (assuming you have all the software described below installed
//   properly), you can simply say:
//
//       texToImg [options] filename ...
//
//   to compile filename.tex to filename.gif, an interlaced,
//   transparent background GIF file ready to use an an inline image.
//   You can specify the base name, for example, "schrod", rather than
//   the full name of the TeX file ("schrod.tex").  TeX requires the
//   input file to have an extension of ".tex".  The command line
//   options are described in the help text at the end of this program
//   and in the "Default Configuration" section below.
//
//   A sample IMG tag, including the image width and height is printed
//   on standard error, for example:
//
//       <img src="schrod.gif" width=508 height=56>
//
//                         Required Software
//
//   This script requires the following software to be installed
//   in the standard manner.  Version numbers are those used in the
//   development and testing of the script.
//
//   Perl        5.8.0 (anything later than 4.036 should work)
//   TeX         3.14159 (Web2C 7.3.1)
//   LaTeX2e     <2000/06/01>
//   dvips       dvipsk 5.86
//   Ghostscript 6.52 (2001-10-20)
//   Netpbm      9.24
//
//
//                       Default Configuration
//
//   The following settings are the defaults used if the -dpi and
//   -res options are not specified on the command line.
//
//   The parameter $dpi controls how large the equation will appear
//   with respect to other inline images and the surrounding text.
//   The parameter is expressed in "dots per inch" in the PostScript
//   sense.  Unfortunately, since there's no standard text size in
//   Web browsers (and most allow the user to change fonts and
//   point sizes), there's no "right" value for this setting.  The
//   default of 150 seems about right for most documents.  A setting
//   of 75 generates equations at half the normal size, while 300
//   doubles the size of equations.  The setting of $dpi can always be
//   overridden by specifying the "-dpi" command line option.
//
	var $dpi = 150;
//
//   The parameter $res specifies the oversampling as the ratio
//   of the final image size to the initial black and white image.
//   Smaller values produce smoothing with more levels of grey but
//   require (much) more memory and intermediate file space to create
//   the image.  If you run out of memory or disc space with the
//   default value of 0.5, try changing it to 0.75.  A $res setting of
//   1.0 disables antialiasing entirely.  The setting of $res can
//   always be overridden by specifying the "res" command line option.
//
	var $res = 0.5;
//
//   The $background parameter supplies a command, which may be
//   void, to be inserted in the image processing pipeline to
//   adjust the original black-on-white image so that its background
//   agrees with that of the document in which it is to be inserted.
//   For a document with the default grey background used by Mosaic
//   and old versions of Netscape, use:
//
//       $background = "ppmdim 0.7 |";  $transparent = "b2/b2/b2";
//
//   If your document uses a white background, the void specification:
//
//       $background = "";  $transparent = "ff/ff/ff";
//
//   should be used.  For colour or pattern backgrounds, you'll have
//   to hack the code.  The reason for adjusting the background is to
//   ensure that when the image is resampled and then output with a
//   transparent background the edges of the characters will fade
//   smoothly into the page background.  Otherwise you'll get a
//   distracting "halo" around each character.  You can override this
//   default specification with the -grey command line option.
//
	var $background = "";
	var $transparent = "ff/ff/ff";
//
//   Image generation and decoding commands for GIF and PNG output.
//
	var $cmdGIF = 'ppmToImg';
	var $cmdGIFdecode = 'giftopnm';
	var $cmdPNG = 'pnmtopng';
	var $cmdPNGdecode = 'pngtopnm';
//
//   Default image creation modes
//
	var $imageCmd = null;
	var $imageCmdD = null;
	var $imageExt = 'gif';
	
	function texToImg($arrOpts=null) {
		$this->imageCmd = $this->cmdGIF;
		$this->imageCmdD = $this->cmdGIFDecode;
		$this->options($arrOpts);
	}
	function options($arrOpts) {
		if ($arrOpts == null) return;
		$arrOpts = (object)$arrOpts;
		if (isset($arrOpts->dpi))
			$this->dpi = $arrOpts->dpi;
		if (isset($arrOpts->output)) switch (strToLower($arrOpts->output)) {
			case 'png':
				$this->imageCmd = $this->cmdPNG;
				$this->imageCmdD = $this->cmdPNGdecode;
				$this->imageExt = 'png';
				break;
			default: 
				$this->imageCmd = $this->cmdGIF;
				$this->imageCmdD = $this->cmdGIFdecode;
				$this->imageExt = 'gif';
				break;
		}
		if (isset($arrOpts->grey) && is_numeric($arrOpts->grey) && $arrOpts->grey > 0 && $arrOpts->grey < 1) {
			$this->background = 'ppmdim '.$arrOpts->grey.' | ';
			$grey = (int)(255*$arrOpts->grey);
			$this->transparent = sprintf('%02x/%02x/%02x', $grey, $grey, $grey);
		}
		if (isset($arrOpts->res) && is_numeric($arrOpts->res) && $arrOpts->res > 0 && $arrOpts->res < 1) {
			$this->res = $arrOpts->res;
		}
	}
    //
    //   Main file processing loop
    //
    // Use: $tempFileName = $texObject->render($texString);
    // copy the temp file name where you can access it from the web, and paste an image tag where you need it.
	function render($tex) {
		$dir = getcwd();
		$srcFile = tempnam(null,'texToImg_source_');
		$dstFile = tempnam(null, 'texToImg_dest_');
		chdir(dirname($srcFile));
		$srcName = basename($srcFile);
		$dstName = basename($dstFile);
		$f = fopen($srcFile,'w');
		fwrite($f,$tex);
		fclose($f);
		
		
		
		`echo x | latex "$srcName" 2> /dev/null`;
		`dvips -f "$srcName" > "$srcName.ps" 2> /dev/null`;
		
	//   Assemble and execute the command pipeline which generates the image.

	//   Start by invoking Ghostscript with the pbmraw output device and
	//   output file set to standard output ("-") and the requested resolution.
	//   The -q (Quiet) option is required; otherwise Ghostscript will send
	//   processing information to standard output and corrupt transmission
	//   of the bitmap to the next component in the pipeline.
		
		$cmd = 'echo quot | gs -q -dNOPAUSE -r'.((int)$this->dpi / $this->res).' -sOutputFile=- -sDEVICE=pbmraw "'.$srcName.'.ps" | ';
	//   Next we crop white space surrounding the generated text, promote
	//   the monochrome bitmap to a grey scale image with 8 bits per pixel,
	//   apply whatever background adjustment transform is requested, and
	//   scale the image to the desired size.
		$cmd.= 'pnmcrop -white | pnmdepth 255 | '.$this->background.' pnmscale '.$this->res.' | ';
	//   Finally, convert the image to the desired output format and write
	//   the output file.
		$cmd.= $this->imageCmd.' -interlace -transparent rgb:'.$this->transparent.' > '.$dstName.'.'.$this->imageExt;
		$cmd .= ' 2>/dev/null';
		`$cmd`;
		@unlink($srcName, $srcName.'.dvi', $srcName.'.ps', $srcName.'.aux');
		chdir($dir);
		return $dstFile.'.'.$this->imageExt;
	}
}
define('MTTG_HEAD', <<<HEADER
\documentclass[10pt]{article}
\pagestyle{empty}
\begin{document}
\begin{displaymath}
\bf
HEADER
);
define('MTTG_FOOT', <<<FOOTER
\end{displaymath}
\end{document}
FOOTER
);
class mathTexToImg extends texToImg {
    // Use is the same as the ->render() method for texToImg, except that you don't need to plug in 
// the stuff to make a full LaTeX document - just the equations themselves.
	function render($tex) {
		return parent::render(MTTG_HEAD.$tex.MTTG_FOOT);
	}
}

The next step, I suppose, is aping the UI from the other formatter.
 
Back
Top