[plt-scheme] In-line LaTeX in REPL
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
happens
* Uses scribble syntax
-------------------------------------------------------------------------------
#reader scribble/reader
#lang scheme
(require scheme/system scheme/gui/base)
(define str string-append)
(define TEMPLATE
@str{\documentclass{article}
\usepackage[mathletters]{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\pagestyle{empty}
\begin{document}
\Huge\[
~a
\]
\end{document}})
(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))
(current-error-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
@latex{\sum_{i=0}^{\infty}\lambda_i}
(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!