[plt-scheme] In-line LaTeX in REPL
Nice. Here is a small improvement.
#lang scheme
;;; Create an image snip object from a latex equation.
;;; Technique from http://dererumnatura.us/archives/2008/02/rendering-
equat-1.html
(require scheme/system)
(require scheme/gui/base)
(define TEMPLATE
#<<eos
\documentclass{article}
\usepackage{amsmath}
\pagestyle{empty}
\begin{document}
\Huge
\[
~a
\]
\end{document}
eos
)
(define (latex str)
(define latex (make-temporary-file "latex~a" 'directory))
(begin0
(parameterize ([current-directory latex])
(with-output-to-file "latex.tex"
(lambda () (printf TEMPLATE str))
#:mode 'text
#:exists 'replace)
(system "PATH=/sw/bin:$PATH pdflatex latex.tex > /dev/null")
(system "PATH=/sw/bin:$PATH convert latex.pdf -trim +repage
latex.png")
(make-object image-snip% "latex.png"))
(delete-directory/files latex)))
;; Example
(latex
#<<latex
\sum_{i=0}^{\infty}\lambda_i
latex
)
-- Matthias
On Dec 2, 2008, at 6:21 PM, Anthony Cowley wrote:
> 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
>>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme