Hi Sami,
for the purpose of a text at the beginning of a music-book, I created a
markup-list command.
This command expects some installed commands: pdflatex and/or xelatex,
pdftops and pdfinfo.
I use ubuntu so this is installed via aptitude and callable on the
console. I don't know anything about how this could work on windows ...
These are the function, that I created for my own purposes, so I cannot
give any warranty, that they do work as expected!
And take care/ATTENTION: This function creates files on your harddisk,
processes them with the above mentioned commands *and then removes these
working files!*
So this is a proof of concept and the answer to your question: yes it is
possible
But you (or whoever) might have to tweak this stuff to work as *you*
expect ;-)
HTH
cheers, Jan-Peter
On 21.06.2012 01:50, Sami wrote:
Hi all!
This has probably been asked before, but I haven't found it anywhere, so I
ask it myself. Excuse any repetition, and please direct me there if it
exists and you can find it easily.
With lilypond-book and co, what I understood is that one writes a LaTeX
document with lilypond snippets. Essentially, it is a text document, and has
some musical examples. LaTeX is the boss.
What if we want to do the exact opposite? Like, a music book with snippets
of TeX or LaTex-formatted text. An ideal situation would be for prefaces,
contents, etc. to be in LaTex, or TeX or whatever, and then the music in
lilypond, and all of this inside the lilypond file.
I suppose that each LaTeX snippet would be a small LaTeX document with its
own preamble etc, and most page-formatting variables dictated by lilypond,
which would do the analogous of what lilypond-book does, but in reverse.
Such a case would be great if one wants to write a music book, but using the
Tex text functionality for any big chunks of text, like cover pages,
prefaces, contents, etc.
Is any of this possible?
\version "2.15.37"
#(use-modules
(lily)
(ice-9 popen)
(ice-9 rdelim)
(ice-9 regex)
(scm framework-eps))
% string-join with arbitrary objects
#(define-public (glue-list lst glue)
"create string from list containing arbitrary objects"
(string-join (map (lambda (s)(format "~A" s)) lst) glue 'infix))
% taken from markup-macros.scm
#(define (markup-function? x)
(and (markup-command-signature x)
(not (object-property x 'markup-list-command))))
% extract available page-height from layout and properties
#(define-public (content-height layout props)
(let ((mm (ly:output-def-lookup layout 'mm))
(height (ly:output-def-lookup layout 'paper-height))
(perc (chain-assoc-get 'percent props 100))
(tm (ly:output-def-lookup layout 'top-margin))
(bm (ly:output-def-lookup layout 'bottom-margin))
(hom (ly:output-def-lookup layout 'oddHeaderMarkup))
(hem (ly:output-def-lookup layout 'evenHeaderMarkup))
(bom (ly:output-def-lookup layout 'oddFooterMarkup))
(bem (ly:output-def-lookup layout 'evenFooterMarkup)))
(define (mupheight mup)(if (markup? mup) (let ((y-ext (ly:stencil-extent (interpret-markup layout props mup) Y)))
(- (cdr y-ext)(car y-ext))) 0))
(/ (* (- height (+ tm bm (max (mupheight hom)(mupheight hem))
(max (mupheight hom)(mupheight hem)))) (/ perc 100)) mm)
))
% create TeX-string from markup
#(define-public (markup->tex mup . mprops)
(let ((conc (if (> (length mprops) 0) (car mprops) #f))
(layout (if (> (length mprops) 1) (cadr mprops) #f))
(props (if (> (length mprops) 2) (caddr mprops) '()))
(mup? (lambda (m)(or (string? m)(list? m)(markup? m))))
(result ""))
(cond ((string? mup) (set! result mup))
((null? mup) (set! result ""))
((and (pair? mup) (equal? (car mup) concat-markup))
(set! result (markup->tex (cdr mup) #t layout props)))
((and (pair? mup) (equal? (car mup) fromproperty-markup))
(set! result (markup->tex (chain-assoc-get (cadr mup) props "???") conc layout props)))
((and (pair? mup) (equal? (car mup) override-markup))
(set! result (markup->tex (cdr (cdr mup)) conc layout (cons (list (car (cdr mup))) props))))
((and (pair? mup) (equal? (car mup) page-ref-markup))
(set! result (let* ((table (if layout (ly:output-def-lookup layout 'label-page-table) '()))
(pg (assoc-get (car (cdr mup)) table)))
(if pg (format "~A" pg) (caddr (cdr mup)))) ))
((and (pair? mup)(markup-function? (car mup)))
(let ((proc (get-markup-producer (car mup))))
(if (procedure? proc)
(set! result (markup->tex (proc layout props (cdr mup))))
(for-each (lambda (m)(set! result (string-append result (if (or conc (string=? result "")) "" " ") (markup->tex m conc layout props))))
(filter mup? (cdr mup))))))
((list? mup)
(for-each (lambda (m)(set! result (string-append result (if (or conc (string=? result "")) "" " ") (markup->tex m conc layout props))))
(filter mup? mup)))
(else (ly:message "~A" mup)))
result))
#(define-public (tex-markup-list layout props pkgs cmd opts m)
(let* ((mm (ly:output-def-lookup layout 'mm))
(scropts (chain-assoc-get 'scrartcl props ""))
(size (chain-assoc-get 'line-width props (ly:output-def-lookup layout 'line-width 10)))
; width of our box in mm
(width (/ size mm))
; percent of page to use
(perc (chain-assoc-get 'percent props))
; height of our box in mm
(height (- (content-height layout props) (chain-assoc-get 'bottom-gap props (if perc 0 3))))
; the text to fill into template.tex
(text (markup->tex m #f))
; basename of working files
(basename (strftime (format "~A-%Y%m%d%H%M%S" cmd) (localtime (current-time))))
; result of each command
(result 0)
; stencil to return
(text-stencil empty-stencil)
(pages 0)
(epslist '()))
(define (readpipe port text)
(let ((line (read-line port)))
(if (not (eof-object? line))
(set! text (readpipe port (string-append (string-append text line) "\n"))))
text))
(define (cmd->string cmd)
(let* ((port (open-pipe cmd OPEN_READ))
(str (readpipe port ""))
(result (close-pipe port)))
(if (and (= 0 result)(> (string-length str) 0))
str "")))
(define (loop n callback)(let ((ret (list)))
(define (lp i)
(if (<= i n)
(begin
(set! ret (append ret (list (callback i))))
(lp (+ i 1)))))
(lp 1)
ret))
; write <basename>.tex
(with-output-to-file (format "~A.tex" basename) (lambda ()
(format #t "\\documentclass~A{scrartcl}
\\usepackage[paperwidth=~Amm,paperheight=~Amm,margin=0mm]{geometry}
\\usepackage[~A]{babel}
~A
\\begin{document}
~A
\\end{document}
"
scropts width height
(chain-assoc-get 'babel props "ngerman")
(glue-list pkgs "\n")
text)))
; produce pdf
(set! result (system (format "~A ~A \"~A.tex\"" cmd opts basename)))
; how many pages?
(set! pages (let* ((r (make-regexp "Pages:\\s+([0-9]+)"))
(m (regexp-exec r (cmd->string (format "pdfinfo \"~A.pdf\"" basename)))))
(string->number (match:substring m 1))))
; add pages to markup-list
(loop pages (lambda (pag)(let ((pagname (format "~A-~A.eps" basename pag)))
; convert page to EPS
(set! result (system (format "pdftops -eps -f ~A -l ~A \"~A.pdf\" \"~A\"" pag pag basename pagname)))
; include EPS
(set! text-stencil (eps-file->stencil X size (format "~A-~A.eps" basename pag)))
(set! epslist (append epslist (list text-stencil)))
)))
; remove working files
(system (format "rm -v \"~A\"*" basename))
; return eps-stencils
epslist
))
%; pdflatex markup-list command
#(define-markup-list-command (pdflatex layout props m)(markup-list?)
(tex-markup-list layout props
`("\\usepackage[utf8]{inputenc}") "pdflatex" "-interaction=batchmode" m))
%; xelatex markup-list command
#(define-markup-list-command (xelatex layout props m)(markup-list?)
(tex-markup-list layout props
`("\\usepackage{xunicode}" "\\usepackage{fontspec}"
,(format "\\setmainfont{~A}" (chain-assoc-get 'font-family props "DejaVu Serif")))
"xelatex" "-interaction=batchmode" m))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Example
% Tex new paragraph
texnp = "\n\n"
\markuplist {
% line-width of latex block
\override-lines #'(line-width . 75)
% font to use (for xelatex)
\override-lines #'(font-family . "Century Schoolbook L")
% latex font-size
\override-lines #'(scrartcl . "[10pt]")
% percent of page-height to use
\override-lines #'(percent . 90)
% This markup text is converted to a tex-string and written to a tex-file.
% The tex-file is processed to pdf (pdflatex or xelatex) of size line-width x (percent * page-height / 100).
% All pages are exported to EPS with pdftops -eps and then a markup-list containing all EPS-files is returned.
\xelatex {
Lorem ipsum dolor sit amet, modus simul cu est, molestie principes his ad.
Eum cu albucius noluisse dissentias.
Sea at aeterno fabellas delicatissimi, et quidam sanctus eos, labores debitis reprehendunt mei te.
Numquam apeirian intellegam ex cum, posse molestiae sed ad, habeo recusabo ad eam.
\texnp
Mea simul dicam semper eu. Falli saepe timeam est id, ea vidisse appetere sed.
Ei sea duis populo vidisse. Quis rationibus in cum, erat salutatus ullamcorper ei has.
Admodum ponderum pro no.
\texnp
Ei pro postea tibique, eam in liber paulo diceret.
Vidit alterum eos ex, veri posse discere at pri.
Nobis expetendis vix at, te inani dolorum sit, atqui homero nec ad.
Eu commodo labitur facilisi nec, iudico melius eu duo, dolore deseruisse ex nec.
Malis semper option ne quo, mutat quidam delicatissimi an vel.
\texnp
At hinc maiorum sententiae nam. Eum an graece iisque, veri sapientem eam ut.
Semper feugiat mel eu, dolore laoreet persecuti ne nec.
Ut duo veniam noster dicunt, nostrud necessitatibus ei vel.
\texnp
Pri cu sapientem sententiae. Ea his tation impedit definiebas,
harum choro ex mea, delectus consequat ea sea. Per magna tempor hendrerit an, congue homero his at.
Sit singulis corrumpit interpretaris in. Ut insolens comprehensam has.
Eu delectus lucilius has, ne vel amet tamquam.
Illud maiorum interesset sit at, id eum audiam placerat.
}
}
_______________________________________________
lilypond-user mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/lilypond-user