lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Choose paper size and layout with command line options?


From: M Sun
Subject: Re: Choose paper size and layout with command line options?
Date: Sun, 14 Aug 2022 10:13:39 -0700

On Tue, Aug 9, 2022 at 1:35 AM Jean Abou Samra <jean@abou-samra.fr> wrote:
>
>
>
> Le 07/08/2022 à 22:51, M Sun a écrit :
> > Hi all,
> >
> > I’m writing a sheet targeting two different paper sizes, with some
> > layout adjustments. My goal is to be able to specify (maybe
> > indirectly) the paper size in command line, without changing the
> > lilypond file itself. My initial set up was this:
> >
> >    \version "2.23.2"
> >    #(set! paper-alist
> >      (cons '("kobo" . (cons (* 15.7 cm) (* 20.9 cm))) paper-alist))
> >    #(set-global-staff-size 18)
> >    \paper {
> >      top-margin = 0
> >      bottom-margin = 0
> >      left-margin = 5
> >      right-margin = 0
> >    }
> >    #(set-default-paper-size "kobo")
> >    \score { \new Staff {a b c}}
> >
> > As you can see this is a cramped layout for a small paper size. Now I
> > also want a version with a normal layout on us-letter size, so I
> > removed the set-global-staff-size line, and the paper block, and
> > changed the paper size to “letter”. The file became this:
> >
> >    \version "2.23.2"
> >    #(set! paper-alist
> >      (cons '("kobo" . (cons (* 15.7 cm) (* 20.9 cm))) paper-alist))
> >    #(set-default-paper-size "letter")
> >    \score { \new Staff {a b c}}
> >
> > These worked. But like I mentioned my goal is to be able to switch
> > between these two setups in command line, without changing the file.
> > My idea was to set a variable in the command line using “-e”, and in
> > the file choose the layout and paper size based on the variable.
> > Here’s my code:
> >
> >    \version "2.23.2"
> >
> >    #(define (configOutput spec)
> >      (cond
> >       ((eqv? spec 'letter) (set-default-paper-size "letter"))
> >       ((eqv? spec 'kobo)
> >        (set! paper-alist
> >         (cons '("kobo" . (cons (* 15.7 cm) (* 20.9 cm))) paper-alist))
> >        (set-global-staff-size 18)
> >        #{ \paper {
> >        top-margin = 0
> >        bottom-margin = 0
> >        left-margin = 5
> >        right-margin = 0 }
> >        #}
> >        (set-default-paper-size "kobo"))
> >       (default nil)))
> >
> >    #(use-modules (guile-user))
> >    #(configOutput output-spec)
> >
> >    \score { \new Staff {a b c}}
> >
> > Then in the shell I ran a command like this:
> >
> >    lilypond -e "(define-public output-spec 'kobo)" test.ly
> >
> > This produced the output with the “kobo” paper size. Or I could change
> > that “kobo” to “letter” and it’d produce with the “letter” paper size.
> >
> > This *almost* worked, except that the \paper block didn’t take effect.
> > This is where I’m lost. How should I make this work?
>
>
>
> set!, set-global-staff-size and set-default-paper-size all act
> through a side effect. On the other hand, \paper doesn't have
> a side effect, it is a value that you should pass to LilyPond
> to make its settings integrated into the default settings. Because
> the principle of cond in Scheme is that the value of the last
> expression the a cond clause is returned, your \paper is evaluated
> and just thrown away. You need to put it last. Furthermore,
> there is a subtlety here that forces to use $ instead of #;
> see
> https://lilypond.org/doc/v2.23/Documentation/extending/lilypond-scheme-syntax
> and
> https://extending-lilypond.readthedocs.io/en/latest/lily-and-scheme.html#hash-vs-dollar
>
> Some minor comments on your code:
> - eqv? is more general than needed for symbols, you can use the
>    (theoretically) faster eq?
> - case does the job too,
> - there is no "default" in Scheme, it's "else",
> - there is no "nil" (the empty list is '() and the boolean
>    false is #f). You don't need it anyway, just leave that
>    "default" clause out (it will return *unspecified*, a special
>    Guile value that is ignored by LilyPond).

I might have written too much elisp recently haha... (not sure where
that "default" came from though)

> Giving:
>
> \version "2.23.12"
>
> #(use-modules (guile-user))
>
> $(case output-spec
>     ((letter)
>      (set-default-paper-size "letter"))
>     ((kobo)
>      (set! paper-alist
>       (cons '("kobo" . (cons (* 15.7 cm) (* 20.9 cm))) paper-alist))
>      (set-global-staff-size 18)
>      (set-default-paper-size "kobo")
>      #{
>        \paper {
>          top-margin = 0
>          bottom-margin = 0
>          left-margin = 5
>          right-margin = 0
>        }
>      #})))
>
> \score { \new Staff {a b c}}

This works perfectly. Thank you!

-- 
MetroWind



reply via email to

[Prev in Thread] Current Thread [Next in Thread]