mrtex2im.ss
(module mrtex2im mzscheme
  ;; Scheme Port of the tex2im utility
  (require (lib "kw.ss")
           (lib "class.ss")
           (lib "process.ss")
           (lib "mred.ss" "mred")
           (lib "file.ss"))
  
  (current-directory (find-system-path 'temp-dir))
  
  (define setup-latex-path (make-parameter (build-path "/usr/bin/latex")))
  (define setup-dvips-path (make-parameter (build-path "/usr/bin/dvips")))
  (define setup-convert-path (make-parameter (build-path "/usr/bin/convert")))

  (define (get-string-from-port fileport)
    ;; returns a string with the file contents
    (let loop ((str "") (line (read-line fileport)))
      (if (eof-object? line)
          str
          (loop (string-append str "\n" line)
                (read-line fileport)))))

  ;; Given a file path with latex code returns a path with the respective eps file.
  (define (run-latex/dvips file)
    (let ([dvi-file (string-append (path->string file) ".dvi")]
          [eps-file (string-append (path->string file) ".eps")])
      (if (and (zero? (system*/exit-code (setup-latex-path) "-interaction=batchmode" (path->string file)))
               (zero? (system*/exit-code (setup-dvips-path) "-o" eps-file "-E" dvi-file)))
          eps-file
          (begin0
              eps-file
              (printf "WARNING: Error generating latex or postscript file.")))))
      
99
  (define (run-convert trans? aa? bgcolor res file)
    (let ([png-file (string-append file ".png")])
      (if trans?
          (system*/exit-code (setup-convert-path)
                             "+adjoin"
                             (if aa? "-antialias" "+antialias")
                             "-transparent" bgcolor
                             "-density"
                             res
                             file
                             png-file)
          (system*/exit-code (setup-convert-path)
                             "+adjoin"
                             (if aa? "-antialias" "+antialias")
                             "-density"
                             res
                             file
                             png-file))
      png-file))
  
  
  (define/kw (tex2im str-or-path ;; Latex string or path to input file
                     #:key
                     [resolution "150x150"]
                     [bgcolor "white"]
                     [fgcolor "black"]
                     [transparency #f]
                     [noformula #f]
                     [anti-aliasing #t]
                     [extra-header (build-path (find-system-path 'home-dir) ".tex2im-header")])
    (let ([tmp-file (make-temporary-file)])
      (with-output-to-file tmp-file
        (lambda ()
          ;; Output header
          (printf "\\documentclass[12pt]{article} \\usepackage{color} \\usepackage[dvips]{graphicx} \\pagestyle{empty}")
        ;; Output header in file
          (if (file-exists? extra-header)
              (printf (call-with-input-file #:extra-header
                        (lambda (fileport)
                          (get-string-from-port fileport)))))
          (printf "\\pagecolor{~a} \\begin{document} {\\color{~a}" bgcolor fgcolor)
          (if (not noformula) (printf "\\begin{eqnarray*}"))
          (if (string? str-or-path)
              ;; latex code is in string
              (printf str-or-path)
              ;; latex code is in file
              (printf (call-with-input-file str-or-path
                        (lambda (fileport)
                          (get-string-from-port fileport)))))
          (if (not noformula) (printf "\\end{eqnarray*}"))
          (printf "}\\end{document}"))
        'truncate)
      (make-object bitmap%
           (run-convert transparency anti-aliasing bgcolor resolution 
                        (run-latex/dvips tmp-file))
           'png
           #f)))
    
  (provide tex2im))