add the --dvipdf flag to scribble

This adds a new back-end pipeline for generating pdf to
scribble, with the hope that included picts (e.g., those
generated by Redex) will look better when viewed with
on-screen pdf viewers

Current problem: the descent adjustment is messed up,
as can be seen by this example program's output:

 #lang scribble/base
@(require redex/pict
          pict
          redex/reduction-semantics)
@title{}
@(define-language L
   (e ::= (e e) x (λ (x) e)))
@(render-language L)

ff@(text "f" '(italic . roman) 20)ff

x@(text "x")y@(text "y")

original commit: 5280395f8802d21764ef074540421441cb8ac999
This commit is contained in:
Robby Findler 2014-06-27 03:25:15 -05:00
parent adc82b0c67
commit 1afd6c802a
5 changed files with 66 additions and 26 deletions

View File

@ -35,6 +35,9 @@ its file suffix:
@item{@DFlag{pdf} --- PDF @filepath{@|fn|.pdf} that is generated @item{@DFlag{pdf} --- PDF @filepath{@|fn|.pdf} that is generated
via @exec{pdflatex}} via @exec{pdflatex}}
@item{@DFlag{dvipdf} --- PDF @filepath{@|fn|.pdf} that is generated
via @exec{latex} and @exec{dvipdf}}
@item{@DFlag{latex-section} @nonterm{n} --- LaTeX source @item{@DFlag{latex-section} @nonterm{n} --- LaTeX source
@filepath{@|fn|.tex} plus additional @filepath{.tex} files to @filepath{@|fn|.tex} plus additional @filepath{.tex} files to
be included in the enclosing document's preamble, where the be included in the enclosing document's preamble, where the

View File

@ -43,7 +43,7 @@
(define-runtime-path skull-tex "scribble-skull.tex") (define-runtime-path skull-tex "scribble-skull.tex")
(define skull-style (make-style #f (list (tex-addition skull-tex)))) (define skull-style (make-style #f (list (tex-addition skull-tex))))
(define (render-mixin %) (define (render-mixin % #:convert-as-ps-not-pdf? [convert-as-ps-not-pdf? #t])
(class % (class %
(inherit-field prefix-file style-file style-extra-files) (inherit-field prefix-file style-file style-extra-files)
@ -355,10 +355,14 @@
(not (disable-images)) (not (disable-images))
(let ([ftag (lambda (v suffix) (and v (list v suffix)))] (let ([ftag (lambda (v suffix) (and v (list v suffix)))]
[xlist (lambda (v) (and v (list v #f #f #f #f)))]) [xlist (lambda (v) (and v (list v #f #f #f #f)))])
(or (ftag (convert e 'pdf-bytes+bounds) ".pdf") (if convert-as-ps-not-pdf?
(ftag (xlist (convert e 'pdf-bytes)) ".pdf") (or (ftag (convert e 'eps-bytes+bounds) ".ps")
(ftag (xlist (convert e 'eps-bytes)) ".ps") (ftag (xlist (convert e 'eps-bytes)) ".ps")
(ftag (xlist (convert e 'png-bytes)) ".png")))) (ftag (xlist (convert e 'png-bytes)) ".png"))
(or (ftag (convert e 'pdf-bytes+bounds) ".pdf")
(ftag (xlist (convert e 'pdf-bytes)) ".pdf")
(ftag (xlist (convert e 'eps-bytes)) ".ps")
(ftag (xlist (convert e 'png-bytes)) ".png")))))
=> (lambda (bstr+info+suffix) => (lambda (bstr+info+suffix)
(check-render) (check-render)
(let* ([bstr (list-ref (list-ref bstr+info+suffix 0) 0)] (let* ([bstr (list-ref (list-ref bstr+info+suffix 0) 0)]

View File

@ -3,8 +3,15 @@
(require "private/indirect-renderer.rkt" "private/run-pdflatex.rkt" (require "private/indirect-renderer.rkt" "private/run-pdflatex.rkt"
(prefix-in latex: "latex-render.rkt")) (prefix-in latex: "latex-render.rkt"))
(provide render-mixin) (provide render-mixin
dvi-render-mixin)
(define render-mixin (define render-mixin
(make-indirect-renderer-mixin (make-indirect-renderer-mixin
latex:render-mixin #".tex" #".pdf" run-pdflatex)) latex:render-mixin #".tex" #".pdf"
run-pdflatex))
(define dvi-render-mixin
(make-indirect-renderer-mixin
(λ (%) (latex:render-mixin % #:convert-as-ps-not-pdf? #t)) #".tex" #".pdf"
run-dvipdf-latex))

View File

@ -2,11 +2,18 @@
(require scheme/system scheme/port) (require scheme/system scheme/port)
(provide run-pdflatex) (provide run-pdflatex run-dvipdf-latex)
(define (run-pdflatex file [notify void]) (define (run-pdflatex file [notify void]) (run file notify #f))
(define (run-dvipdf-latex file [notify void])
(parameterize ([function-name 'run-dvipdf-latex])
(run file notify #t)))
(define max-runs 5)
(define (run file notify via-dvipdf?)
(define latex-cmd-name (if via-dvipdf? "latex" "pdflatex"))
(define cmd (define cmd
(list (get-pdflatex-binary) (list (get-latex-binary latex-cmd-name)
"-interaction=batchmode" "-interaction=batchmode"
(format "~a" file))) (format "~a" file)))
(define logfile (path-replace-suffix file #".log")) (define logfile (path-replace-suffix file #".log"))
@ -19,13 +26,13 @@
(lambda (log) (copy-port log (current-error-port)))) (lambda (log) (copy-port log (current-error-port))))
(err "got error exit code"))) (err "got error exit code")))
(let loop ([n 0]) (let loop ([n 0])
(when (= n 5) (when (= n max-runs)
(err "didn't get a stable result after ~a runs" n)) (err "didn't get a stable result after ~a runs" n))
(if (zero? n) (if (zero? n)
(notify "running pdflatex on ~a" file) (notify "running ~a on ~a" latex-cmd-name file)
(notify " running ~a~a time" (notify " running ~a~a time"
(add1 n) (add1 n)
(case (add1 n) [(2) 'nd] [(3) 'rd] [else 'th]))) (case (normalize-for-suffix (add1 n)) [(2) 'nd] [(3) 'rd] [else 'th])))
(run) (run)
;; see if we get a "Rerun" note, these seem to come in two flavors ;; see if we get a "Rerun" note, these seem to come in two flavors
;; * Label(s) may have changed. Rerun to get cross-references right. ;; * Label(s) may have changed. Rerun to get cross-references right.
@ -36,27 +43,43 @@
[(zero? n) [(zero? n)
(notify "WARNING: no \"Rerun\" found in first run of pdflatex for ~a" (notify "WARNING: no \"Rerun\" found in first run of pdflatex for ~a"
file)])) file)]))
(when via-dvipdf?
(define dvi-file (path-replace-suffix file #".dvi"))
(unless (file-exists? dvi-file) (err "didn't find .dvi file"))
(define dvipdf (get-latex-binary "dvipdf"))
(notify "running dvipdf on ~a" dvi-file)
(unless (parameterize ([current-output-port (open-output-nowhere)])
(system* dvipdf dvi-file))
(err "got error exit code")))
(path-replace-suffix file #".pdf")) (path-replace-suffix file #".pdf"))
(define (get-pdflatex-binary) (define (normalize-for-suffix n)
(cond
[(<= 10 n 20) 0]
[else (modulo n 10)]))
(define (get-latex-binary name)
(define ans (define ans
(case (system-type) (case (system-type)
[(macosx) (or (find-executable-path "pdflatex") [(macosx) (or (find-executable-path name)
(for/or ([macosx-candidate (in-list macosx-candidates)]) (for/or ([macosx-candidate-dir (in-list macosx-candidate-dirs)])
(define macosx-candidate (build-path macosx-candidate-dir name))
(and (file-exists? macosx-candidate) (and (file-exists? macosx-candidate)
macosx-candidate)))] macosx-candidate)))]
[(windows) (or (find-executable-path "pdflatex") [(windows) (or (find-executable-path name)
(find-executable-path "pdflatex.exe"))] (find-executable-path (format "~a.exe" name)))]
[(unix) (find-executable-path "pdflatex")])) [(unix) (find-executable-path name)]))
(unless ans (unless ans
(err "could not find a `pdflatex' executable")) (err (format "could not find a `~a' executable" name)))
ans) ans)
(define (err fmt . args) (apply error 'run-pdflatex fmt args)) (define function-name (make-parameter 'run-pdflatex))
(define (err fmt . args) (apply error (function-name) fmt args))
;; under mac os x, gui apps do not get started with ;; under mac os x, gui apps do not get started with
;; a good path environment, so put likely candidates ;; a good path environment, so put likely candidates
;; for a latex installation here so that the "scribble ;; for directories holding latex/pdflatex binaries
;; pdf" button is more likely to work in drracket ;; here so that the "scribble pdf" button is more
(define macosx-candidates ;; likely to work in drracket
'("/usr/texbin/pdflatex")) (define macosx-candidate-dirs
'("/usr/texbin"))

View File

@ -62,9 +62,12 @@
[("--latex") "generate LaTeX-format output" [("--latex") "generate LaTeX-format output"
(current-html #f) (current-html #f)
(current-render-mixin latex:render-mixin)] (current-render-mixin latex:render-mixin)]
[("--pdf") "generate PDF-format output (with PDFLaTeX)" [("--pdf") "generate PDF-format output (via PDFLaTeX)"
(current-html #f) (current-html #f)
(current-render-mixin pdf:render-mixin)] (current-render-mixin pdf:render-mixin)]
[("--dvipdf") "generate PDF-format output (via LaTeX and DVIPDF)"
(current-html #f)
(current-render-mixin pdf:dvi-render-mixin)]
[("--latex-section") n "generate LaTeX-format output for section depth <n>" [("--latex-section") n "generate LaTeX-format output for section depth <n>"
(current-html #f) (current-html #f)
(let ([v (string->number n)]) (let ([v (string->number n)])