Using tags

The \tag #'partA command marks a music expression with the name partA. Expressions tagged in this way can be selected or filtered out by name later, using either \keepWithTag #'name or \removeWithTag #'name. The result of applying these filters to tagged music is as follows:

FilterResult
Tagged music preceded by
\keepWithTag #'name

or

\keepWithTag #'(name1 name2…)
Untagged music and music tagged with any of the given tag names is included; music tagged with any other tag name is excluded.
Tagged music preceded by
\removeWithTag #'name

or

\removeWithTag #'(name1 name2…)
Untagged music and music not tagged with any of the given tag names is included; music tagged with any of the given tag names is excluded.
Tagged music not preceded by either
\keepWithTag or \removeWithTag
All tagged and untagged music is included.

The arguments of the \tag, \keepWithTag and \removeWithTag commands should be a symbol or list of symbols (such as #'score or #'(violinI violinII), followed by a music expression. If and only if the symbols are valid LilyPond identifiers (alphabetic characters only, no numbers, underscores, or dashes) which cannot be confused with notes, the #' may be omitted and, as a shorthand, a list of symbols can use the dot separator: i.e., \tag #'(violinI violinII) can be written \tag violinI.violinII. The same applies to \keepWithTag and \removeWithTag. Tagging commands are music functions, thus they cannot be used to filter items that are not music expressions, such as \book or \score blocks.

In the following example, we see two versions of a piece of music, one showing trills with the usual notation, and one with trills explicitly expanded:

music = \relative {
  g'8. c32 d
  \tag #'trills { d8.\trill }
  \tag #'expand { \repeat unfold 3 { e32 d } }
  c32 d
 }

\score {
  \keepWithTag #'trills \music
}
\score {
  \keepWithTag #'expand \music
}

[image of music]

Alternatively, it is sometimes easier to exclude sections of music:

music = \relative {
  g'8. c32 d
  \tag #'trills { d8.\trill }
  \tag #'expand { \repeat unfold 3 { e32 d } }
  c32 d
 }

\score {
  \removeWithTag #'expand
  \music
}
\score {
  \removeWithTag #'trills
  \music
}

[image of music]

If tags mark alternatives that have non-zero duration, the alternatives are often conceptually simultaneous, in which case it is best to put the alternatives in a simultaneous music expression so that the music expression has the same duration no matter which tags are retained. This is especially important if you are using tags in combination with commands like \cueDuring.

outputTypeTag = "isScore"

firstInstrument = \relative c' {
  <<
    \tag #'isPart {
      \cueDuring "quoteSecondInstrument" #UP { r2 } }
    \tag #'isScore { r2 }
  >>
  e4 f |
  g4 a b c |
}

secondInstrument= \relative c'' {
  c4 c r2 |
  \cueDuring "quoteFirstInstrument" #DOWN { r2 }
  c4 c |
}

\addQuote quoteFirstInstrument \firstInstrument
\addQuote quoteSecondInstrument \secondInstrument

\new Staff {
  \keepWithTag \outputTypeTag \firstInstrument
}

\new Staff {
  \keepWithTag \outputTypeTag \secondInstrument
}

[image of music]

Tagged filtering can be applied to articulations, texts, etc., by prepending

-\tag #'your-tag

to an articulation. For example, this would define a note with a conditional fingering indication and a note with a conditional annotation:

c1-\tag #'finger ^4
c1-\tag #'warn ^"Watch!"

Multiple tags may be placed on expressions with multiple \tag entries, or by combining multiple tags into one symbol list:

music = \relative c'' {
  \tag #'a \tag #'both { a4 a a a }
  \tag #'(b both) { b4 b b b }
}
<<
\keepWithTag #'a \music
\keepWithTag #'b \music
\keepWithTag #'both \music
>>

[image of music]

Multiple \removeWithTag filters may be applied to a single music expression to remove several differently named tagged sections. Alternatively, you can use a single \removeWithTag with a list of tags.

music = \relative c'' {
  \tag #'A { a4 a a a }
  \tag #'B { b4 b b b }
  \tag #'C { c4 c c c }
  \tag #'D { d4 d d d }
}
\new Voice {
  \removeWithTag #'B
  \removeWithTag #'C
  \music
  \removeWithTag #'(B C)
  \music
}

[image of music]

Using two or more \keepWithTag filters on a single music expression will cause all of the tagged sections to be removed. The first filter will remove all except the one named and any subsequent filters will remove the rest. Using one \keepWithTag command with a list of multiple tags will only remove tagged sections that are not specified in that list.

music = \relative c'' {
  \tag #'violinI { a4 a a a }
  \tag #'violinII { b4 b b b }
  \tag #'viola { c4 c c c }
  \tag #'cello { d4 d d d }
}

\new Staff {
  \keepWithTag #'(violinI violinII)
  \music
}

[image of music]

will print \tags violinI and violinII but not viola or cello.

While \keepWithTag is convenient when dealing with one set of alternatives, the removal of music tagged with unrelated tags is problematic when using them for more than one purpose. In that case ‘groups’ of tags can be declared:

\tagGroup #'(violinI violinII viola cello)

Now all the different tags belong to a single ‘tag group’. Note that individual tags cannot be members of more than one tag group.

\keepWithTag #'violinI …

will now only show music tagged from violinI’s tag group and any music tagged with one of the other tags will removed.

music = \relative {
  \tagGroup #'(violinI violinII viola cello)
  \tag #'violinI { c''4^"violinI" c c c }
  \tag #'violinII { a2 a }
  \tag #'viola { e8 e e2. }
  \tag #'cello { d'2 d4 d }
  R1^"untagged"
}

\new Voice {
  \keepWithTag #'violinI
  \music
}

[image of music]

When using the \keepWithTag command, only tags from the tag groups of the tags given in the command are visible.

Sometimes you want to splice some music at a particular place in an existing music expression. You can use \pushToTag and \appendToTag for adding material at the front or end of various music constructs. The supported places are

Sequential and simultaneous music

If you tagged an entire {…} or <<…>> construct, you can add music expressions at its front or back.

Chords

If you tagged a chord <…>, you can either add notes at its front or back, or articulations for the whole chord.

Notes and rests

If you tagged a note (also inside of a chord) or a rest, you can add articulations to the front or back of its existing articulations. Note that to add other notes, you rather have to put the note inside of a chord and tag the chord. Also note that you cannot tag a single articulation and add to it since it isn’t inherently a list. Instead, tag the note.

music = { \tag #'here { \tag #'here <<c''>> } }

{
  \pushToTag #'here c'
  \pushToTag #'here e'
  \pushToTag #'here g' \music
  \appendToTag #'here c'
  \appendToTag #'here e'
  \appendToTag #'here g' \music
}

[image of music]

Both commands get a tag, the material to splice in at every occurrence of the tag, and the tagged expression.

See also

Learning Manual: Organizing pieces with variables.

Notation Reference: Automatic part combining, Including LilyPond files.

Known issues and warnings

Calling \relative on a music expression obtained by filtering music through \keepWithTag or \removeWithTag might cause the octave relations to change, as only the pitches actually remaining in the filtered expression will be considered. Applying \relative first, before \keepWithTag or \removeWithTag, avoids this danger as \relative then acts on all the pitches as input.


LilyPond Notation Reference v2.25.21 (development-branch).