[plt-scheme] In-line LaTeX in REPL

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Dec 3 19:28:31 EST 2008

On Dec  3, Matthias Felleisen wrote:
> Nice. Here is a small improvement.

Here is an even better version:
* Uses the latex magic that deals with utf-8
* Redirects the input so drscheme doesn't show the input box
* Throws an error if a process failed, and show the output if this
* Uses scribble syntax

#reader scribble/reader
#lang scheme
(require scheme/system scheme/gui/base)

(define str string-append)

(define TEMPLATE

(define COMMANDS
  @str{pdflatex x.tex
       convert -density 96x96 x.pdf -trim +repage x.png})

(define (latex . strs)
  (define latex (make-temporary-file "latex~a" 'directory))
  (define (run)
    (parameterize ([current-directory latex]
                   [current-input-port (open-input-bytes #"")]
                   [current-output-port (open-output-string)])
      (call-with-output-file* "x.tex" #:exists 'truncate
        (lambda (o) (fprintf o TEMPLATE (string-append* strs))))
      (unless (system (regexp-replace #rx"\n+" COMMANDS " \\&\\& "))
        (display (get-output-string (current-output-port))
        (error 'latex
               "commands did not run successfully, see above output"))
      (make-object image-snip% "x.png")))
  (define (cleanup) (delete-directory/files latex))
  (dynamic-wind void run cleanup))

;; Examples
(let ([self @str{\lambda x . x x}])
  @latex{(@self) (@self)})

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.