### 2.21 Integrating text and music

Some texts include music examples. Examples are musicological treatises, songbooks or manuals like this. Such texts can be made by hand, simply by importing a PostScript figure into the word processor. However, there is an automated procedure to reduce the amount of work involved HTML, LaTeX, and Texinfo documents.

A script called `lilypond-book` will extract the music fragments, run format them, and put back the resulting notation. This program is fully described in lilypond-book manual. Here we show a small example. The example also contains explanatory text, so we will not comment on it further

         \documentclass[a4paper]{article}
         \begin{document}

         Documents for lilypond-book may freely mix music and text.  For
         example,

         \begin{lilypond}
           \score { \notes \relative c' {
              c2 g'2 \times 2/3 { f8 e d } c'2 g4
           } }
         \end{lilypond}

         If there is no \verb+\score+ block in the fragment,
         \texttt{lilypond-book} will supply one

         \begin{lilypond}
           c'4
         \end{lilypond}

         In this example two things happened: a
         \verb+\score+ block was added, and the line width was set to natural
         length.

         Options are put in brackets.

         \begin[staffsize=26,verbatim]{lilypond}
           c'4 f16
         \end{lilypond}

         Larger examples can be put in a separate file, and introduced with
         \verb+\lilypondfile+.

         \lilypondfile{screech-boink.ly}

         \end{document}

Under Unix, you can view the results as follows

         $ cd input/tutorial
         $ mkdir -p out/
         $ lilypond-book --output=out/ lilybook.tex
         lilypond-book (GNU LilyPond) 2.1.19
         Reading `input/tutorial/lilybook.tex'
         Reading `input/screech-boink.ly'
         lots of stuff deleted
         Writing `out/lilybook.tex'
         $ cd out
         $ latex lilybook
         lots of stuff deleted
         $ xdvi lilybook

To convert the file into a nice PDF document, run the following commands

         $ dvips -Ppdf -u +lilypond lilybook
         $ ps2pdf lilybook.ps

Running lilypond-book and running latex creates a lot of temporary files, which would clutter up the working directory. To remedy this, use the `output` option. It will create the files in a separate subdirectory out.

The result looks more or less like

Documents for lilypond-book may freely mix music and text. For example

If you have no `\score` block in the fragment, `lilypond-book` will supply one

In this example two things happened: a `score` block was added, and the line width was set to natural length.

Options are put in brackets.

>          c'4 f16

Larger examples can be put in a separate file, and introduced with `\lilypondfile`.

Read comments on this page, or add one. This page is for LilyPond-2.2.5 (stable-branch).