3.2.1 LaTeX

LaTeX is the de-facto standard for publishing layouts in the exact sciences. It is built on top of the TeX typesetting engine, providing the best typography available anywhere.

See The Not So Short Introduction to LaTeX for an overview on how to use LaTeX.

lilypond-book provides the following commands and environments to include music in LaTeX files:

In the input file, music is specified with any of the following commands:

\begin{lilypond}[options,go,here]
  YOUR LILYPOND CODE
\end{lilypond}

\lilypond[options,go,here]{ YOUR LILYPOND CODE }

\lilypondfile[options,go,here]{filename}

\musicxmlfile[options,go,here]{filename}

Additionally, \lilypondversion displays the current version of LilyPond. Running lilypond-book yields a file that can be further processed with LaTeX.

We show some examples here. The lilypond environment

\begin{lilypond}[quote,fragment,staffsize=26]
  c'4 d' e' f' g'2 g'
\end{lilypond}

produces

[image of music]

The short version

\lilypond[quote,fragment,staffsize=11]{<c' e' g'>}

produces

[image of music]

The default line width of the music is computed by examining the commands in the document preamble (that is, the part of the document before \begin{document}). The lilypond-book command sends these to LaTeX to find out how wide the text is. The line width for music fragments is then adjusted to the text width. Note that this heuristic algorithm can fail easily; in such cases it is necessary to use the line-width music fragment option.

Note: Make sure that your preamble actually compiles without errors, otherwise line width detection will fail. You can test this by directly processing your document with LaTeX, using an empty \begin{document} ... \end{document} block.

If you use a landscape paper format, be sure to add the keyword landscape to the option list of the \documentclass macro.5

Each snippet calls the following macros if they have been defined by the user:

Selected Snippets

Sometimes it is useful to display music elements (such as ties and slurs) as if they continued after the end of the fragment. This can be done by breaking the staff and suppressing inclusion of the rest of the LilyPond output.

In LaTeX, define \betweenLilyPondSystem in such a way that inclusion of other systems is terminated once the required number of systems are included. Since \betweenLilyPondSystem is first called after the first system, including only the first system is trivial.

\def\betweenLilyPondSystem#1{\endinput}

\begin{lilypond}[fragment]
  c'1\( e'( c'~ \break c' d) e f\)
\end{lilypond}

If a greater number of systems is requested, a TeX conditional must be used before \endinput. In this example, replace value 2 by the number of systems you want in the output.

\def\betweenLilyPondSystem#1{
  \ifnum#1<2\else\expandafter\endinput\fi
}

(Since \endinput immediately stops the processing of the current input file we need \expandafter to delay the call of \endinput after executing \fi so that the \if ... \fi clause is balanced.)

Remember that the definition of \betweenLilyPondSystem is effective until the current group is finished (such as the LaTeX environment) or is overridden by another definition (which it is, in most cases, for the rest of the document). To reset your definition, write

\let\betweenLilyPondSystem\undefined

in your LaTeX source.

This may be simplified by defining a TeX macro

\def\onlyFirstNSystems#1{
  \def\betweenLilyPondSystem##1{%
    \ifnum##1<#1\else\expandafter\endinput\fi}
}

and then saying only how many systems you want before each fragment,

\onlyFirstNSystems{3}
\begin{lilypond}…\end{lilypond}
\onlyFirstNSystems{1}
\begin{lilypond}…\end{lilypond}

See also

There are specific lilypond-book command-line options and other details to know when processing LaTeX documents, see Invoking lilypond-book.


Footnotes

(5)

This is necessary because lilypond-book adds \usepackage{graphics} to the preamble if neither the graphics nor the graphicx package gets loaded; this might change the paper dimensions unexpectedly for some document classes.


LilyPond Application Usage v2.25.21 (development-branch).