Style sheets - GNU LilyPond
Description: GNU LilyPond

Previous: Saving typing with identifiers and functions, Up: Working on LilyPond projects

### 4.6 Style sheets

The output that LilyPond produces can be heavily modified; see Tweaking output for details. But what if you have many files that you want to apply your tweaks to? Or what if you simply want to separate your tweaks from the actual music? This is quite easy to do.

Let's look at an example. Don't worry if you don't understand the parts with all the #(). This is explained in Advanced tweaks with Scheme.

>          mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
>            #:line(#:dynamic "mp" #:text #:italic "dolce" )))
>          tempoMark = #(define-music-function (parser location markp) (string?)
>          #{
>            \once \override Score . RehearsalMark #'self-alignment-X = #left
>            \once \override Score . RehearsalMark #'no-spacing-rods = ##t
>            \mark \markup { \bold $markp }
>          #})

>          \relative c'' {
>            \tempo 4=50
>            a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
>            \tempoMark "Poco piu mosso"
>            cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
>          }

There are some problems with overlapping output; we'll fix those using the techniques in Fixing overlapping notation. But let's also do something about the `mpdolce` and `tempoMark` definitions. They produce the output we desire, but we might want to use them in another piece. We could simply copy-and-paste them at the top of every file, but that's an annoyance. It also leaves those definitions in our music files, and I personally find all the #() somewhat ugly. Let's hide them in another file:

    %%% save this to a file called "definitions.ly"
    mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
      #:line(#:dynamic "mp" #:text #:italic "dolce" )))
    tempoMark = #(define-music-function (parser location markp) (string?)
    #{
      \once \override Score . RehearsalMark #'self-alignment-X = #left
      \once \override Score . RehearsalMark #'no-spacing-rods = ##t
      \mark \markup { \bold $markp }
    #})

Now let's modify our music (let's save this file as `"music.ly"`).

    \include "definitions.ly"

    \relative c'' {
      \tempo 4=50
      a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
      \once \override Score.RehearsalMark #'padding = #2.0
      \tempoMark "Poco piu mosso"
      cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
    }

That looks better, but let's make a few changes. The glissando is hard to see, so let's make it thicker and closer to the noteheads. Let's put the metronome marking above the clef, instead of over the first note. And finally, my composition professor hates "C" time signatures, so we'd better make that "4/4" instead.

Don't change `music.ly`, though. Replace our `definitions.ly` with this:

    %%%  definitions.ly
    mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
      #:line( #:dynamic "mp" #:text #:italic "dolce" )))
    tempoMark = #(define-music-function (parser location markp) (string?)
    #{
      \once \override Score . RehearsalMark #'self-alignment-X = #left
      \once \override Score . RehearsalMark #'no-spacing-rods = ##t
      \mark \markup { \bold $markp }
    #})

    \layout{
      \context { \Score
        \override MetronomeMark #'extra-offset = #'(-9 . 0)
        \override MetronomeMark #'padding = #'3
      }
      \context { \Staff
        \override TimeSignature #'style = #'numbered
      }
      \context { \Voice
        \override Glissando #'thickness = #3
        \override Glissando #'gap = #0.1
      }
    }

That looks nicer! But now suppose that I want to publish this piece. My composition professor doesn't like "C" time signatures, but I'm somewhat fond of them. Let's copy the current `definitions.ly` to `web-publish.ly` and modify that. Since this music is aimed at producing a pdf which will be displayed on the screen, we'll also increase the overall size of the output.

    %%%  definitions.ly
    mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
      #:line( #:dynamic "mp" #:text #:italic "dolce" )))
    tempoMark = #(define-music-function (parser location markp) (string?)
    #{
      \once \override Score . RehearsalMark #'self-alignment-X = #left
      \once \override Score . RehearsalMark #'no-spacing-rods = ##t
      \mark \markup { \bold $markp }
    #})

    #(set-global-staff-size 23)
    \layout{
      \context { \Score
        \override MetronomeMark #'extra-offset = #'(-9 . 0)
        \override MetronomeMark #'padding = #'3
      }
      \context { \Staff
      }
      \context { \Voice
        \override Glissando #'thickness = #3
        \override Glissando #'gap = #0.1
      }
    }

Now in our music, I simply replace `\include "definitions.ly"` with `\include "web-publish.ly"`. Of course, we could make this even more convenient. We could make a `definitions.ly` file which contains only the definitions of `mpdolce` and `tempoMark`, a `web-publish.ly` file which contains only the `\layout` section listed above, and a `university.ly` file which contains only the tweaks to produce the output that my professor prefers. The top of `music.ly` would then look like this:

    \include "definitions.ly"

    %%%  Only uncomment one of these two lines!
    \include "web-publish.ly"
    %\include "university.ly"

This approach can be useful even if you are only producing one set of parts. I use half a dozen different “style sheet” files for my projects. I begin every music file with `\include "../global.ly"`, which contains

    %%%   global.ly
    \version "2.8.0"
    #(ly:set-option 'point-and-click #f)
    \include "../init/init-defs.ly"
    \include "../init/init-layout.ly"
    \include "../init/init-headers.ly"
    \include "../init/init-paper.ly"

This page is for LilyPond-2.8.8 (stable-branch).

Other languages: English.
Using automatic language selection.