Warning: key signatures and pitches

Music Glossary: accidental, key signature, pitch, flat, natural, sharp, transposition, Pitch names.

LilyPond makes a distinction between musical content and its printed representation. Input such as ‘d4 e fis2’ defines the pitches and durations of notes, which is musical content. The key signature is part of the printed representation. The key signature also sets rules for the printed representations of notes. LilyPond compares each input pitch to the key signature to determine whether to print an accidental.

The command \key sets the key signature, which affects the printed representation, but does not change the pitch assigned to a note such as ‘c’ in the input.

Consider this example.

\relative {
  \key d \major
  cis''4 d e fis

[image of music]

No note has a printed accidental, but you must still add ‘is’ and type ‘cis’ and ‘fis’ in the input file.

The code ‘b’ does not mean “print a thick black dot just on the middle line of the staff”. Rather, it means “there is a note with pitch B-natural”. In the key of A-flat major, it does get an accidental:

\relative {
  \key aes \major
  aes'4 c b c

[image of music]

To say it differently: Whenever you enter a pitch that is a black key on the piano, you must add ‘is’ or ‘es’ to the note name.

Adding all alterations explicitly might require a little more effort when typing, but the advantage is that transposing is easier, and accidentals can be printed according to different conventions. For some examples of how accidentals can be printed according to different rules, see Automatic accidentals.


Notation Reference: Note names in other languages, Accidentals, Automatic accidentals, Key signature.

LilyPond 学习手册 v2.25.17 (开发分支).