### 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:

- the `\lilypond{…}` command, where you can directly enter short lilypond code
- the `\begin{lilypond}…\end{lilypond}` environment, where you can directly enter longer lilypond code
- the `\lilypondfile{…}` command to insert a lilypond file
- the `\musicxmlfile{…}` command to insert a MusicXML file, which will be processed by `musicxml2ly` and `lilypond`.

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' d' e' f' g'2 g'2
>     \end{lilypond}

produces

The short version

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

produces

Currently, you cannot include `{` or `}` within `\lilypond{}`, so this command is only useful with the `fragment` option.

The default line width of the music will be adjusted by examining the commands in the document preamble, 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 the 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.

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

- `\preLilyPondExample` called before the music,
- `\postLilyPondExample` called after the music,
- `\betweenLilyPondSystem[1]` is called between systems if `lilypond-book` has split the snippet into several PostScript files. It must be defined as taking one parameter and will be passed the number of files already included in this snippet. The default is to simply insert a `\linebreak`.

#### 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 the `\endinput`. In this example, replace ‘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 TeX quits the current group (such as the LaTeX environment) or is overridden by another definition (which 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`.

其他语言：deutsch, español, français, italiano, 日本語。
About automatic language selection.

#### LilyPond — Usage v2.18.2 (稳定分支).