[plt-scheme] In-line LaTeX in REPL

From: Anthony Cowley (acowley at seas.upenn.edu)
Date: Tue Dec 2 18:21:41 EST 2008

Here's a version of Mark's code that does the processing locally. It
needs some attention regarding the system calls depending on your
setup, as it calls pdflatex and convert (ImageMagick) as well as
creates a couple temporary files.

;;; Create an image snip object from a latex equation.
;;; Technique from
http://dererumnatura.us/archives/2008/02/rendering-equat-1.html
#lang scheme
(require scheme/system)
(require scheme/gui/base)
(define latex
  (let ((template-head (string-join  '("\\documentclass{article}"
                                       "\\usepackage{amsmath}"
                                       "\\pagestyle{empty}"
                                       "\\begin{document}"
                                       "\\Huge"
                                       "\\[")
                                     "~n"))
        (template-foot (string-join '("\\]"
                                      "\\end{document}")
                                    "~n")))
    (lambda (str)
      (call-with-output-file "/tmp/latex.tex"
        (lambda (f) (fprintf f (string-join (list template-head str
template-foot) "~n")))
        #:mode 'text
        #:exists 'replace)
      (system "PATH=/sw/bin:$PATH pdflatex -output-directory /tmp
latex.tex > /dev/null")
      (system "PATH=/sw/bin:$PATH convert /tmp/latex.pdf -trim +repage
/tmp/latex.png")
      (make-object image-snip% "/tmp/latex.png"))))

;; Example
; (latex "\\sum_{i=0}^{\\infty}\\lambda_i")

Anthony

On Tue, Dec 2, 2008 at 4:32 PM, Mark Engelberg <mark.engelberg at gmail.com> wrote:
> BTW, my code looks like this:
> #lang scheme
> (require scheme/system)
> (require scheme/gui/base)
> (define (latex str)
>  (let ([escaped-str (regexp-replace* #rx" " str "%20")])
>    (system (format "C:\\temp\\curl
> \"http://www.codecogs.com/eq.latex?~a\" -o C:\\temp\\latex.gif"
> escaped-str))
>    (make-object image-snip% "c:\\temp\\latex.gif")))
>
> I keep the curl executable in my temp directory.
>
> So you can do something like (latex "3 \\geq 2") and it will produce
> the latex output for that.
>
> While it's downloading the latex gif, the repl displays a read input
> box.  I'm not sure how to suppress that.
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.