]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/internals.itely
529162814ae4f93dda275cde43cddc1f3bf10e0e
[lilypond.git] / Documentation / user / internals.itely
1 @c -*-texinfo-*-
2 @c Note:
3 @c
4 @c A menu is needed before every deeper *section nesting of @nodes
5 @c Run M-x texinfo-all-menus-update
6 @c to automagically fill in these menus
7 @c before saving changes
8
9
10 @node Advanced Topics
11 @chapter Advanced Topics
12
13
14 When translating the input to notation, there are number of distinct
15 phases.  We list them here:
16
17 @c todo: moved from refman. 
18
19 The purpose of LilyPond is explained informally by the term `music
20 typesetter'.  This is not a fully correct name: Not only does the
21 program print musical symbols, it also makes aesthetic decisions.
22 Symbols and their placements are @emph{generated} from a high-level
23 musical description.  In other words, LilyPond would be best described
24 to be a `music compiler' or `music to notation compiler'.
25
26 LilyPond is linked to GUILE, GNU's Scheme library for extension
27 programming.  The Scheme library provides the glue that holds together
28 the low-level routines and separate modules which are written in C++.
29
30 When lilypond is run to typeset sheet music, the following happens:
31
32 @itemize @bullet
33 @item GUILE initialization: Various scheme files are read.
34
35 @item Parsing: First standard @code{ly} initialization files are read,
36 then the user @file{ly} file is read.
37
38 @item Interpretation: The music in the file is processed `in playing
39 order', i.e., the order that you use to read sheet music, or the
40 order in which notes are played.  The result of this step is a
41 typesetting specification.
42
43 @item Typesetting: The typesetting specification is solved: positions
44 and formatting is calculated.
45
46 @item The visible results ("virtual ink") are written to the output file.
47 @end itemize
48
49 During these stages different types of data play the the main role:
50 During parsing, @strong{Music} objects are created.  During the
51 interpretation, @strong{contexts} are constructed, and with these
52 contexts a network of @strong{graphical objects} (`grobs') is
53 created.  These grobs contain unknown variables, and the network forms a
54 set of equations.  After solving the equations and filling in these
55 variables, the printed output is written to an output file.
56
57 These threemanship of tasks (parsing, translating, typesetting) and
58 data-structures (music, context, graphical objects) permeates the entire
59 design of the program.
60
61
62 @table @b
63
64 @item Parsing
65
66 The @code{ly} file is read and converted to a list of @code{Scores}, which
67 each contain @code{Music} and paper/midi-definitions.  Here @code{Music},
68 @code{Pitch}, and @code{Duration} objects are created.
69
70 @item Interpreting music
71 @cindex interpreting music
72
73 All music events are `read' in the same order as they would be played
74 (or read from paper).  At every step of the interpretation, musical
75 events are delivered to interpretation contexts,
76 @cindex engraver
77 which use them to build @code{Grob}s (or MIDI objects for MIDI output).
78
79 In this stage @code{Music_iterators} do a traversal of the @code{Music}
80 structure.  The music events thus encountered are reported to
81 @code{Translator}s, a set of objects that collectively form interpretation
82 contexts.
83
84 @item Prebreaking
85 @cindex prebreaking
86
87 At places where line breaks may occur, clefs and bars are prepared for
88 a possible line break. 
89
90 @item Preprocessing
91 @cindex preprocessing
92
93 In this stage, all information that is needed to determine line breaking
94 is computed. 
95
96 @item Break calculation
97
98 The lines and horizontal positions of the columns are determined.
99
100 @item Breaking
101
102 Relations between all grobs are modified to reflect line breaks: When a
103 spanner, e.g. a slur, crosses a line break, then the spanner is `broken
104 into pieces'; for every line that the spanner is in, a copy of the grob
105 is made.  A substitution process redirects all grob references so that
106 each spanner grob will only reference other grobs in the same line.
107
108 @item Outputting
109
110 All vertical dimensions and spanning objects are computed, and all grobs
111 are output, line by line.  The output is encoded in the form of
112 @code{Molecule}s
113
114 @end table
115
116 The data types that are mentioned here are all discussed in this
117 section.
118
119
120 @c FIXME: Note entry vs Music entry at top level menu is confusing.
121 @c . {Music entry}
122 @menu
123 * Interpretation context::      
124 * Syntactic details::           
125 * Lexical details::             
126 * Output details::              
127 @end menu
128
129
130 @node Interpretation context
131 @section Interpretation context
132
133 @menu
134 * Creating contexts::           
135 * Default contexts::            
136 * Context properties::          
137 * Engravers and performers::    
138 * Changing context definitions::  
139 * Defining new contexts::       
140 @end menu
141
142
143 Interpretation contexts are objects that only exist during a run of
144 LilyPond.  During the interpretation phase of LilyPond (when it prints
145 @code{interpreting music} to standard output), the music expression in
146 a @code{\score} block is interpreted in time order.  This is the same
147 order that humans hear and play the music.
148
149 During this interpretation, the interpretation context holds the
150 state for the current point within the music.  It contains information
151 like
152
153 @itemize @bullet
154 @item What notes are playing at this point?
155
156 @item What symbols will be printed at this point?
157
158 @item What is the current key signature, time signature, point within
159 the measure, etc.?
160 @end itemize
161
162 Contexts are grouped hierarchically: A @internalsref{Voice} context is
163 contained in a @internalsref{Staff} context (because a staff can contain
164 multiple voices at any point), a @internalsref{Staff} context is contained in
165 @internalsref{Score}, @internalsref{StaffGroup}, or
166 @internalsref{ChoirStaff} context.
167
168 Contexts associated with sheet music output are called @emph{notation
169 contexts}, those for sound output are called @emph{performance
170 contexts}.  The default definitions of the standard notation and
171 performance contexts can be found in @file{ly/engraver-init.ly} and
172 @file{ly/performer-init.ly}, respectively.
173
174
175 @node Creating contexts
176 @subsection Creating contexts
177 @cindex @code{\context}
178 @cindex context selection
179
180 Contexts for a music expression can be selected manually, using the
181 following music expression.
182
183 @example
184 \context @var{contexttype} [= @var{contextname}] @var{musicexpr}
185 @end example
186
187 @noindent
188 This instructs lilypond to interpret @var{musicexpr} within the context
189 of type @var{contexttype} and with name @var{contextname}.  If this
190 context does not exist, it will be created.  
191
192 @lilypond[verbatim,singleline]
193 \score {
194   \notes \relative c'' {
195     c4 <d4 \context Staff = "another" e4> f
196   }
197 }
198 @end lilypond
199
200 @noindent
201 In this example, the @code{c} and @code{d} are printed on the
202 default staff.  For the @code{e}, a context Staff called
203 @code{another} is specified; since that does not exist, a new
204 context is created.  Within @code{another}, a (default) Voice context
205 is created for the @code{e4}.  When all music referring to a
206 context is finished, the context is ended as well.  So after the
207 third quarter, @code{another} is removed.
208
209
210 @node Default contexts
211 @subsection Default contexts
212
213 Most music expressions don't need an explicit @code{\context}
214 declaration: they inherit the notation context from their parent.  Each
215 note is a music expression, and as you can see in the following example,
216 only the sequential music enclosing the three notes has an explicit context. 
217
218 @lilypond[verbatim,singleline]
219 \score { \notes \context Voice = goUp { c'4 d' e' } } 
220 @end lilypond
221
222 There are some quirks that you must keep in mind when dealing with
223 defaults:
224
225 First, every top level music is interpreted by the Score context; in other
226 words, you may think of @code{\score} working like
227
228 @example
229 \score @{
230   \context Score @var{music}
231 @}
232 @end example
233
234 Second, contexts are created automatically to be able to interpret the
235 music expressions.  Consider the following example.
236
237 @lilypond[verbatim, singleline]
238 \score { \context Score \notes { c'4-( d' e'-) } }
239 @end lilypond
240
241 @noindent
242 The sequential music is interpreted by the Score context initially
243 (notice that the @code{\context} specification is redundant), but when a
244 note is encountered, contexts are setup to accept that note.  In this
245 case, a Thread, Voice, and Staff context are created.  The rest of the
246 sequential music is also interpreted with the same Thread, Voice, and
247 Staff context, putting the notes on the same staff, in the same voice.
248
249 @node Context properties
250 @subsection Context properties
251
252 Notation contexts have properties.  These properties are from
253 the @file{.ly} file using the following expression:
254 @cindex @code{\property}
255
256 @example
257 \property @var{contextname}.@var{propname} = @var{value}
258 @end example
259
260 @noindent
261 Sets the @var{propname} property of the context @var{contextname} to the
262 specified Scheme expression @var{value}.  All @var{propname} and
263 @var{contextname} are strings, which are typically unquoted.
264
265 Properties that are set in one context are inherited by all of the
266 contained contexts.  This means that a property valid for the
267 @internalsref{Voice} context can be set in the @internalsref{Score} context
268 (for example) and thus take effect in all @internalsref{Voice} contexts.
269
270 @cindex @code{Current}
271 If you don't wish to specify the name of the context in the
272 @code{\property}-expression itself, you can refer to the abstract context
273 name, @code{Current}.  The @code{Current} context is the latest
274 used context.  This will typically mean the @internalsref{Thread}
275 context, but you can force another context with the
276 @code{\property}-command.  Hence the expressions
277
278 @example
279 \property @var{contextname}.@var{propname} = @var{value}
280 @end example
281
282 @noindent
283 and
284
285 @example
286 \context @var{contextname}
287 \property Current.@var{propname} = @var{value}
288 @end example
289
290 @noindent
291 do the same thing.
292 The main use for this is in macros -- allowing the specification of a
293 property-setting without restriction to a specific context.
294
295 Properties can be unset using the following expression:
296
297 @example
298 \property @var{contextname}.@var{propname} \unset
299 @end example
300
301 @cindex properties, unsetting
302 @cindex @code{\unset} 
303
304 @noindent
305 This removes the definition of @var{propname} in @var{contextname}.  If
306 @var{propname} was not defined in @var{contextname} (but was inherited
307 from a higher context), then this has no effect.
308
309 @refbugs
310
311 The syntax of @code{\unset} is asymmetric: @code{\property \unset} is not
312 the inverse of @code{\property \set}.
313
314
315 @node Engravers and performers
316 @subsection Engravers and performers
317
318 [TODO]
319
320 Basic building blocks of translation are called engravers; they are
321 special C++ classes.
322
323
324 @node Changing context definitions
325 @subsection Changing context definitions
326 @cindex context definition
327 @cindex translator definition
328
329 The most common way to define a context is by extending an existing
330 context.  You can change an existing context from the paper block by
331 first initializing a translator with an existing context identifier:
332
333 @example
334 \paper @{
335   \translator @{
336     @var{context-identifier}
337   @}
338 @}
339 @end example
340
341 @noindent
342 Then you can add and remove engravers using the following syntax:
343
344 @example
345 \remove @var{engravername}
346 \consists @var{engravername}
347 @end example
348
349 @noindent
350 Here @var{engravername} is a string, the name of an engraver in the
351 system.
352
353 @lilypond[verbatim,singleline]
354 \score {
355   \notes {
356     c'4 c'4
357   }
358   \paper {
359     \translator {
360       \StaffContext
361       \remove Clef_engraver
362     }
363   }
364 }
365 @end lilypond
366
367 @cindex engraver
368
369 You can also set properties in a translator definition.  The syntax is as
370 follows:
371
372 @example
373 @var{propname} = @var{value}
374 @var{propname} \set @var{grob-propname} = @var{pvalue}
375 @var{propname} \override @var{grob-propname} = @var{pvalue}
376 @var{propname} \revert @var{grob-propname} 
377 @end example
378
379 @noindent
380 @var{propname} is a string, @var{grob-propname} a symbol, @var{value}
381 and @code{pvalue} are Scheme expressions.  These types of property
382 assignments happen before interpretation starts, so a @code{\property}
383 command will override any predefined settings.
384
385 To simplify editing translators, all standard contexts have standard
386 identifiers called @var{name}@code{Context}, e.g. @code{StaffContext},
387 @code{VoiceContext}; see @file{ly/engraver-init.ly}.
388
389
390 @node Defining new contexts
391 @subsection Defining new contexts
392
393 If you want to build a context from scratch, you must also supply the
394 following extra information:
395
396 @itemize @bullet
397 @item A name, specified by @code{\name @var{contextname}}.
398
399 @item A cooperation module.  This is specified by @code{\type
400 @var{typename}}.
401 @end itemize
402
403 This is an example:
404
405 @example
406 \translator @code{
407   \type "Engraver_group_engraver"
408   \name "SimpleStaff"
409   \alias "Staff"
410   \consists "Staff_symbol_engraver"
411   \consists "Note_head_engraver"
412   \consistsend "Axis_group_engraver"
413 }@
414 @end example
415
416 @noindent
417 The argument of @code{\type} is the name for a special engraver that
418 handles cooperation between simple engravers such as
419 @code{Note_head_engraver} and @code{Staff_symbol_engraver}.  Alternatives
420 for this engraver are the following:
421
422 @table @code
423 @cindex @code{Engraver_group_engraver}
424
425 @item @code{Engraver_group_engraver}  
426 The standard cooperation engraver.
427
428 @cindex @code{Score_engraver}
429 @item @code{Score_engraver}  
430 This is a cooperation module that should be in the top level context.
431 @end table 
432
433 Other modifiers are
434
435 @itemize @bullet
436 @item @code{\alias} @var{alternate-name}:
437 This specifies a different name.  In the above example,
438 @code{\property Staff.X = Y} will also work on @code{SimpleStaff}s
439
440 @item @code{\consistsend} @var{engravername}:
441 Analogous to @code{\consists}, but makes sure that
442 @var{engravername} is always added to the end of the list of
443 engravers.
444
445 Some engraver types need to be at the end of the list; this
446 insures they stay there even if a user adds or removes engravers.
447 End-users generally don't need this command.
448     
449 @item @code{\accepts} @var{contextname}:
450 Add @var{contextname} to the list of contexts this context can
451 contain in the context hierarchy.  The first listed context is the
452 context to create by default.
453
454 @item @code{\denies}:
455 The opposite of @code{\accepts}.  Added for
456 completeness, but is never used in practice.
457
458 @item @code{\name} @var{contextname}:
459 This sets the type name of the context, e.g. @internalsref{Staff},
460 @internalsref{Voice}.  If the name is not specified, the translator won't do
461 anything. 
462 @end itemize
463
464 In the @code{\paper} block, it is also possible to define translator
465 identifiers.  Like other block identifiers, the identifier can only
466 be used as the very first item of a translator.  In order to define
467 such an identifier outside of @code{\score}, you must do
468
469 @example 
470 \paper @{
471   foo = \translator @{ @dots{} @}
472 @}
473 \score @{
474   \notes @{
475     @dots{}
476   @}
477   \paper @{
478     \translator @{ \foo @dots{} @}
479   @}
480 @} 
481 @end example 
482
483 @cindex paper types, engravers, and pre-defined translators
484
485       
486 @node Syntactic details
487 @section Syntactic details
488 @cindex Syntactic details
489
490 This section describes details that were too boring to be put elsewhere.
491
492 @menu
493 * Identifiers::                 
494 * Music expressions::           
495 * Manipulating music expressions::  
496 * Assignments::                 
497 * Lexical modes::               
498 * Ambiguities::                 
499 @end menu
500
501 @c .  {Identifiers}
502 @node Identifiers
503 @subsection Identifiers
504 @cindex Identifiers
505
506 @ignore
507 What has this section got to do with identifiers?
508 It seems more appropriate in the introduction to Chapter 4,
509 "Internals".
510
511   /MB
512 @end ignore
513
514 All of the information in a LilyPond input file is internally
515 represented as a Scheme value.  In addition to normal Scheme data types
516 (such as pair, number, boolean, etc.), LilyPond has a number of
517 specialized data types,
518
519 @itemize @bullet
520 @item Input
521 @item c++-function
522 @item Music
523 @item Identifier
524 @item Translator_def
525 @item Duration
526 @item Pitch
527 @item Score
528 @item Music_output_def
529 @item Moment (rational number)
530 @end itemize
531
532 LilyPond also includes some transient object types.  Objects of these
533 types are built during a LilyPond run, and do not `exist' per se within
534 your input file.  These objects are created as a result of your input
535 file, so you can include commands in the input to manipulate them,
536 during a LilyPond run.
537
538 @itemize @bullet
539 @item Grob: short for `Graphical object'.
540
541 @item Molecule: Device-independent page output object,
542 including dimensions.  Produced by some Grob functions.
543
544 @item Translator: An object that produces audio objects or Grobs.  This is
545 not yet user-accessible.
546
547 @item Font_metric: An object representing a font.
548 @end itemize
549
550
551 @node Music expressions
552 @subsection Music expressions
553 @cindex music expressions
554
555 Music in LilyPond is entered as a music expression.  Notes, rests, lyric
556 syllables are music expressions, and you can combine music expressions
557 to form new ones, for example by enclosing a list of expressions in
558 @code{\sequential @{ @}} or @code{< >}.  In the following example, a
559 compound expression is formed out of the quarter note @code{c} and a
560 quarter note @code{d}:
561
562 @example 
563 \sequential @{ c4 d4 @} 
564 @end example 
565
566 @cindex Sequential music
567 @cindex @code{\sequential}
568 @cindex sequential music
569 @cindex @code{<}
570 @cindex @code{>}
571 @cindex Simultaneous music
572 @cindex @code{\simultaneous}
573
574 The two basic compound music expressions are simultaneous and
575 sequential music.
576
577 @example
578 \sequential @code{@{} @var{musicexprlist} @code{@}}
579 \simultaneous @code{@{} @var{musicexprlist} @code{@}}
580 @end example
581
582 For both, there is a shorthand:
583
584 @example
585 @code{@{} @var{musicexprlist} @code{@}}
586 @end example
587
588 @noindent
589 for sequential and
590
591 @example
592 @code{<} @var{musicexprlist} @code{>}
593 @end example
594
595 @noindent
596 for simultaneous music.
597 In principle, the way in which you nest sequential and simultaneous to
598 produce music is not relevant.  In the following example, three chords
599 are expressed in two different ways:
600
601 @lilypond[fragment,verbatim,center]
602 \notes \context Voice {
603   <a c'> <b d'> <c' e'>
604   < { a b c' } { c' d' e' } >
605 }
606 @end lilypond
607 However, using @code{<} and @code{>} for chords turns up various
608 syntactical peculiarities. For this reason, a special syntax for
609 chords was introduced in version 1.7: @code{<< >>}.
610
611
612
613
614
615 Other compound music expressions include
616 @example
617 \repeat @var{expr}
618 \transpose @var{from} @var{to} @var{expr}
619 \apply @var{func} @var{expr}
620 \context @var{type} = @var{id} @var{expr}
621 \times @var{fraction} @var{expr}
622 @end example
623
624
625 @c . {Manipulating music expressions}
626 @node Manipulating music expressions
627 @subsection Manipulating music expressions
628
629 The @code{\apply} mechanism gives you access to the internal
630 representation of music.  You can write Scheme-functions that operate
631 directly on it.  The syntax is 
632
633 @example
634 \apply #@var{func} @var{music}
635 @end example
636
637 @noindent
638 This means that @var{func} is applied to @var{music}.  The function
639 @var{func} should return a music expression.
640
641 This example replaces the text string of a script.  It also shows a dump
642 of the music it processes, which is useful if you want to know more
643 about how music is stored.
644
645 @lilypond[verbatim,singleline]
646 #(define (testfunc x)
647   (if (equal? (ly:get-mus-property x 'text) "foo")
648       (ly:set-mus-property! x 'text "bar"))
649   ;; recurse
650   (ly:set-mus-property! x 'elements
651     (map testfunc (ly:get-mus-property x 'elements)))
652   (display x)
653   x)
654
655 \score {
656   \notes
657   \apply #testfunc { c'4_"foo" }
658
659 @end lilypond
660
661 For more information on what is possible, see the automatically
662 generated documentation.
663
664 Directly accessing internal representations is dangerous: The
665 implementation is subject to changes, so you should avoid this feature
666 if possible.
667
668 A final example is a function that reverses a piece of music in time:
669
670 @lilypond[verbatim,singleline]
671 #(define (reverse-music music)
672   (let* ((elements (ly:get-mus-property music 'elements))
673          (reversed (reverse elements))
674          (span-dir (ly:get-mus-property music 'span-direction)))
675     (ly:set-mus-property! music 'elements reversed)
676     (if (ly:dir? span-dir)
677         (ly:set-mus-property! music 'span-direction (- span-dir)))
678     (map reverse-music reversed)
679     music))
680
681 music = \notes { c'4 d'4( e'4 f'4 }
682
683 \score {
684   \context Voice {
685     \music
686     \apply #reverse-music \music
687   }
688 }
689 @end lilypond
690
691 More examples are given in the distributed example files in
692 @code{input/test/}.
693
694 @c .   {Assignments}
695 @node Assignments
696 @subsection Assignments
697 @cindex Assignments
698
699 Identifiers allow objects to be assigned to names during the parse
700 stage.  To assign an identifier, use @var{name}@code{=}@var{value}.
701 To refer to an identifier, precede its name with a backslash:
702 `@code{\}@var{name}'.  @var{value} is any valid Scheme value or any of
703 the input-types listed above.  Identifier assignments can appear at top
704 level in the LilyPond file, but also in @code{\paper} blocks.
705
706 An identifier can be created with any string for its name, but you will
707 only be able to refer to identifiers whose names begin with a letter,
708 being entirely alphabetical.  It is impossible to refer to an identifier
709 whose name is the same as the name of a keyword.
710
711 The right hand side of an identifier assignment is parsed completely
712 before the assignment is done, so it is allowed to redefine an
713 identifier in terms of its old value, e.g.
714
715 @example
716 foo = \foo * 2.0
717 @end example
718
719 When an identifier is referenced, the information it points to is
720 copied.  For this reason, an identifier reference must always be the
721 first item in a block.
722
723 @example
724 \paper @{
725   foo = 1.0
726   \paperIdent % wrong and invalid
727 @}
728 @end example
729
730 @example
731 \paper @{
732   \paperIdent % correct
733   foo = 1.0
734 @}
735 @end example
736
737
738 @c .  {Lexical modes}
739 @node Lexical modes
740 @subsection Lexical modes
741 @cindex Lexical modes
742 @cindex input mode
743 @cindex mode, input 
744 @cindex @code{\notes}
745 @cindex @code{\chords}
746 @cindex @code{\lyrics}
747
748 To simplify entering notes, lyrics, and chords, LilyPond has three
749 special input modes in addition to the default mode: note, lyrics, and
750 chords mode.  These input modes change the way that normal, unquoted
751 words are interpreted: For example, the word @code{cis} may be
752 interpreted as a C-sharp, as a lyric syllable `cis' or as a C-sharp
753 major triad respectively.
754
755 A mode switch is entered as a compound music expression
756
757 @example
758 @code{\notes} @var{musicexpr}
759 @code{\chords} @var{musicexpr}
760 @code{\lyrics} @var{musicexpr}
761 @end example
762
763 @noindent
764 In each of these cases, these expressions do not add anything to the
765 meaning of their arguments.  They just instruct the parser in what mode
766 to parse their arguments.
767
768 Different input modes may be nested.
769
770
771 @c .  {Ambiguities}
772 @node Ambiguities
773 @subsection Ambiguities
774 @cindex ambiguities
775 @cindex grammar
776
777 The grammar contains a number of ambiguities.  We hope to resolve them at
778 some time.
779
780 @itemize @bullet
781 @item The assignment
782
783 @example 
784 foo = bar 
785 @end example 
786
787 @noindent
788 is interpreted as the string identifier assignment.  However,
789 it can also be interpreted as making a string identifier @code{\foo}
790 containing @code{"bar"}, or a music identifier @code{\foo} containing
791 the syllable `bar'.  The former interpretation is chosen.
792
793 @item If you do a nested repeat like
794
795 @example 
796 \repeat @dots{}
797 \repeat @dots{}
798 \alternative 
799 @end example 
800
801 @noindent
802 then it is ambiguous to which @code{\repeat} the
803 @code{\alternative} belongs.  This is the classic if-then-else
804 dilemma.  It may be solved by using braces.
805 @end itemize
806
807
808 @c .  {Lexical details}
809 @node Lexical details
810 @section Lexical details
811
812 Even more boring details, now on the lexical side of the input parser.
813
814 @menu
815 * Direct Scheme::               
816 * Reals::                       
817 * Strings::                     
818 @end menu
819
820
821 @node Direct Scheme
822 @subsection Direct Scheme
823 @cindex Scheme
824 @cindex GUILE
825 @cindex Scheme, in-line code
826 @cindex accessing Scheme
827 @cindex evaluating Scheme
828 @cindex LISP
829
830 LilyPond internally uses GUILE, a Scheme-interpreter.  Scheme is a
831 language from the LISP family.  You can learn more about Scheme at
832 @uref{http://www.scheme.org}.  It is used to represent data throughout
833 the whole program.  The hash-sign (@code{#}) accesses GUILE directly: The
834 code following the hash-sign is evaluated as Scheme.  The boolean value
835 @var{true} is @code{#t} in Scheme, so for LilyPond @var{true} looks like
836 @code{##t}.
837
838 LilyPond contains a Scheme interpreter (the GUILE library) for
839 internal use.  In some places, Scheme expressions also form valid syntax:
840 Wherever it is allowed,
841
842 @example
843 #@var{scheme}
844 @end example
845
846 @noindent
847 evaluates the specified Scheme code.  Example:
848
849 @example
850 \property Staff.TestObject \override #'foobar = #(+ 1 2)
851 @end example
852
853 @code{\override} expects two Scheme expressions.
854 The first one is a symbol (@code{foobar}), the second one
855 an integer (namely, 3).
856
857 In-line Scheme may be used at the top level.  In this case the result is
858 discarded.
859
860 Scheme is a full-blown programming language, and a full discussion is
861 outside the scope of this document.  Interested readers are referred to
862 the website @uref{http://www.schemers.org/} for more information on
863 Scheme.
864
865
866 @node Reals
867 @subsection Reals
868 @cindex real numbers
869
870 Formed from an optional minus sign and a sequence of digits followed
871 by a @emph{required} decimal point and an optional exponent such as
872 @code{-1.2e3}.  Reals can be built up using the usual operations:
873 `@code{+}', `@code{-}', `@code{*}', and
874 `@code{/}', with parentheses for grouping.
875
876 @cindex @code{\mm}
877 @cindex @code{\in}
878 @cindex @code{\cm}
879 @cindex @code{\pt}
880 @cindex dimensions
881
882 A real constant can be followed by one of the dimension keywords:
883 @code{\mm} @code{\pt}, @code{\in}, or @code{\cm}, for millimeters,
884 points, inches and centimeters, respectively.  This converts the number
885 that is the internal representation of that dimension.
886
887
888 @node Strings
889 @subsection Strings
890 @cindex string
891 @cindex concatenate
892
893 Begins and ends with the @code{"} character.  To include a @code{"}
894 character in a string write @code{\"}.  Various other backslash
895 sequences have special interpretations as in the C language.  A string
896 that contains no spaces can be written without the quotes.  Strings can
897 be concatenated with the @code{+} operator.
898
899
900 @c .  {Output details}
901 @node Output details
902 @section Output details
903
904 LilyPond's default output format is @TeX{}.  Using the option @option{-f}
905 (or @option{--format}) other output formats can be selected also, but
906 currently none of them reliably work.
907
908 At the beginning of the output file, various global parameters are defined.
909 It also contains a large @code{\special} call to define PostScript routines
910 to draw items not representable with @TeX{}, mainly slurs and ties.  A DVI
911 driver must be able to understand such embedded PostScript, or the output
912 will be rendered incompletely.
913
914 Then the file @file{lilyponddefs.tex} is loaded to define the macros used
915 in the code which follows.  @file{lilyponddefs.tex} includes various other
916 files, partially depending on the global parameters.
917
918 Now the music is output system by system (a `system' consists of all
919 staves belonging together).  From @TeX{}'s point of view, a system is an
920 @code{\hbox} which contains a lowered @code{\vbox} so that it is centered
921 vertically on the baseline of the text.  Between systems,
922 @code{\interscoreline} is inserted vertically to have stretchable space.
923 The horizontal dimension of the @code{\hbox} is given by the
924 @code{linewidth} parameter from LilyPond's @code{\paper} block (using the
925 natural line width if its value is@w{ }@minus{}1).
926
927 After the last system LilyPond emits a stronger variant of
928 @code{\interscoreline} only if the macro @code{\lilypondpaperlastpagefill}
929 is not defined (flushing the systems to the top of the page).  You can
930 avoid that manually by saying
931
932 @example
933 \def\lilypondpaperlastpagefill@{1@}
934 @end example
935
936 @noindent
937 or by setting the variable @code{lastpagefill} in LilyPond's @code{\paper}
938 block.
939
940 It is possible to fine-tune the vertical offset further by defining the
941 macro @code{\lilypondscoreshift}.  Example:
942
943 @example
944 \def\lilypondscoreshift@{0.25\baselineskip@}
945 @end example
946
947 @noindent
948 @code{\baselineskip} is the distance from one text line to the next.
949
950 The code produced by LilyPond can be used by both @TeX{} and La@TeX{}.
951
952 Here an example how to embed a small LilyPond file @code{foo.ly} into
953 running La@TeX{} text without using the @code{lilypond-book} script
954 (@pxref{Integrating text and music with lilypond-book}).
955
956 @example
957 \documentclass@{article@}
958
959 \def\lilypondpaperlastpagefill@{@}
960 \lineskip 5pt
961 \def\lilypondscoreshift@{0.25\baselineskip@}
962
963 \begin@{document@}
964 This is running text which includes an example music file
965 \input@{foo.tex@}
966 right here.
967 \end@{document@}
968 @end example
969
970 The file @file{foo.tex} has been simply produced with
971
972 @example
973 lilypond foo.ly
974 @end example
975
976 It is important to set the @code{indent} parameter to zero in the
977 @code{\paper} block of @file{foo.ly}.
978
979 The call to @code{\lineskip} assures that there is enough vertical space
980 between the LilyPond box and the surrounding text lines.
981
982 @c EOF