LaTeX in browser

Alec Jacobson

February 05, 2010

weblog/

I did a search for "Latex in browser" without any immediate finds for what I thought would come up. Namely, an in-bowser app that has a text area, where I can type in a full latex document program, and a rendering window/frame where I can on the fly see my typeset pdf. I've quickly coded up a simple demo of the idea. Here's a screen shot of the app running on Safari: latex in browser screenshot The code is rather simple it just calls pdflatex on a server using AJAX. Then it dumps the pdf into an <OBJECT> tag. I spent more (too much) tinkering with the layout than actually making the latex and in-browser part come together. Here's the simple php code that the LaTeX program gets POSTed to asynchronously:
<?php
  $myFile = str_replace("\.","-",tempnam(".","latex-in-browser-"));
  $fh = fopen($myFile, 'w') or die("can't open file");
  fwrite($fh, $_POST['latex']);
  fclose($fh);
  #$error=`if pdflatex -interaction nonstopmode $myFile &> /dev/null ; echo "0";else echo "1";fi`;
  $output=`pdflatex -interaction nonstopmode -halt-on-error $myFile `;
  if(strstr($output,"Fatal error"))
    $error = "1";
  else
    $error = "0";
  $log=`cat $myFile.log`;
  `rm $myFile`;
  `rm $myFile.log`;
  `rm $myFile.aux`;
  print trim($myFile).".pdf ".trim($error)." ".$log;
?>
Try LaTeX in browser now!