Importing template from another one #145

Closed
opened 7 years ago by lobisquit · 3 comments
lobisquit commented 7 years ago (Migrated from github.com)

Hi everyboy, I am interested in using Pollen for pdf output.
As outlined in the fourth tutorial, section 8.3.6, a template can be build for this output type, transforming the latex output with pdflatex.

In line 2 of template.pdf.p two there is a small latex template to feed pdflatex:

(define latex-source ◊string-append{
    \documentclass[a4paper,12pt]{letter}
    \begin{document}
    (apply string-append (cdr doc))
    \end{document}})

Could this chunk of code be read from template.ltx.p? It would allow to perform consistent pdf output with the latex file.

Hi everyboy, I am interested in using Pollen for pdf output. As outlined in the [fourth tutorial, section 8.3.6](http://docs.racket-lang.org/pollen/fourth-tutorial.html#%28part._.Adding_support_for_.P.D.F_output%29), a template can be build for this output type, transforming the latex output with `pdflatex`. In line 2 of `template.pdf.p` two there is a small latex template to feed `pdflatex`: ```racket ◊(define latex-source ◊string-append{ \documentclass[a4paper,12pt]{letter} \begin{document} ◊(apply string-append (cdr doc)) \end{document}}) ``` Could this chunk of code be read from `template.ltx.p`? It would allow to perform consistent pdf output with the latex file.
mbutterick commented 7 years ago (Migrated from github.com)

You can’t nest one template inside another. But you can move the reusable parts of the template into a helper function inside pollen.rkt, and call that instead.

Let’s call our helper function make-latex-source. It will take doc as input and return the LaTeX-converted version. To make this function, we just pick up the current definition of latex-source and move it into the function:

#lang pollen/mode racket/base
(require racket/date txexpr pollen/setup racket/list)
(provide (all-defined-out))
 
(module setup racket/base
  (provide (all-defined-out))
  (define poly-targets '(html txt ltx pdf)))
 
(define (get-date)
  (date->string (current-date)))
 
(define (heading . elements)
  (case (current-poly-target)
    [(ltx pdf) (apply string-append `("{\\huge " ,@elements "}"))]
    [(txt) (map string-upcase elements)]
    [else (txexpr 'h2 empty elements)]))
 
(define (emph . elements)
  (case (current-poly-target)
    [(ltx pdf) (apply string-append `("{\\bf " ,@elements "}"))]
    [(txt) `("**" ,@elements "**")]
    [else (txexpr 'strong empty elements)]))

(define (make-latex-source doc)
  ◊string-append{
                 \documentclass[a4paper,12pt]{letter}
                 \begin{document}
                 (apply string-append (filter string? (flatten doc)))
                 \end{document}})

Then we update our templates to call this function:

template.ltx.p

◊(make-latex-source doc)

template.pdf.p

◊(local-require racket/file racket/system)
◊(define latex-source ◊(make-latex-source doc))
◊(define working-directory
    (build-path (current-directory) "pollen-latex-work"))
◊(unless (directory-exists? working-directory)
    (make-directory working-directory))
◊(define temp-ltx-path (build-path working-directory "temp.ltx"))
◊(display-to-file latex-source temp-ltx-path #:exists 'replace)
◊(define command (format "pdflatex -output-directory '~a' '~a'"
  working-directory temp-ltx-path))
◊(if (system command)
    (file->bytes (build-path working-directory "temp.pdf"))
    (error "pdflatex: rendering error"))
You can’t nest one template inside another. But you can move the reusable parts of the template into a helper function inside `pollen.rkt`, and call that instead. Let’s call our helper function `make-latex-source`. It will take `doc` as input and return the LaTeX-converted version. To make this function, we just pick up the current definition of `latex-source` and move it into the function: ```racket #lang pollen/mode racket/base (require racket/date txexpr pollen/setup racket/list) (provide (all-defined-out)) (module setup racket/base (provide (all-defined-out)) (define poly-targets '(html txt ltx pdf))) (define (get-date) (date->string (current-date))) (define (heading . elements) (case (current-poly-target) [(ltx pdf) (apply string-append `("{\\huge " ,@elements "}"))] [(txt) (map string-upcase elements)] [else (txexpr 'h2 empty elements)])) (define (emph . elements) (case (current-poly-target) [(ltx pdf) (apply string-append `("{\\bf " ,@elements "}"))] [(txt) `("**" ,@elements "**")] [else (txexpr 'strong empty elements)])) (define (make-latex-source doc) ◊string-append{ \documentclass[a4paper,12pt]{letter} \begin{document} ◊(apply string-append (filter string? (flatten doc))) \end{document}}) ``` Then we update our templates to call this function: `template.ltx.p` ```text ◊(make-latex-source doc) ``` `template.pdf.p` ```text ◊(local-require racket/file racket/system) ◊(define latex-source ◊(make-latex-source doc)) ◊(define working-directory (build-path (current-directory) "pollen-latex-work")) ◊(unless (directory-exists? working-directory) (make-directory working-directory)) ◊(define temp-ltx-path (build-path working-directory "temp.ltx")) ◊(display-to-file latex-source temp-ltx-path #:exists 'replace) ◊(define command (format "pdflatex -output-directory '~a' '~a'" working-directory temp-ltx-path)) ◊(if (system command) (file->bytes (build-path working-directory "temp.pdf")) (error "pdflatex: rendering error")) ```
lobisquit commented 7 years ago (Migrated from github.com)

Thank you very much, I really appreciate your effort in writing this amazing library 😉

Thank you very much, I really appreciate your effort in writing this amazing library :wink:
mbutterick commented 7 years ago (Migrated from github.com)

🤘

:metal:
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen#145
Loading…
There is no content yet.