# Lilypond: Set key change signature before D.C. or D.S., and different key in next line

Imagine you have a piece consisting of 3 parts, say A, B, C, and they appear in order A B A C. You want to typeset this in the following form

with Da Capo and a Coda, and a line break after the "D.C.", so that the coda appears on the next line.

Now imagine that A is in B flat major, B is in F major, and C is in E flat major (say). To indicate that, I'd like to have a sign for B flat major at the end of the first line, because that's what the next key will be when you do the jump to the beginning.

I tried to achieve that with

\relative c' {
% Part A
\key bes \major a b c d e f g a
\mark \markup {\musicglyph #"scripts.coda"}
% Part B
\key f \major a, b c d e f g a_"D.C."
\key bes \major
\break
\mark \markup {\musicglyph #"scripts.coda"}
% Part C
\key es \major
a, b c d e f g a
}


When compiling this, lilypond outputs a warning about a "conflict with event: key-change-event'" and discards the second one. The result is

with the wrong key in part C. If I omit the \key bes \major statement before the \break, then I get

which has the correct key in part C, but a misleading signature for E flat major before the D.C. that's going to jump to B flat major.

How can I achieve the result that I want, with the correct keys everywhere but a signature for B flat at the end of the line before the D.C.?

Assuming you are using (the new) v2.23, there are now some commands for "segno repeats" (i.e. D.C. al fine, D.S. al Coda, etc.). I've found the syntax to be very tricky to get used to, but also very useful. In your case they'll save you from using manual rehearsal marks and jump instructions.

It's not clear exactly how you want the end of the first line to look, but I think it looks best with the key signature for the repeat in parentheses, after a single bar line, and before the double barline.

If you don't want the parentheses you can remove that part of the below code, but I think it looks strange to have two key signatures in a row without one of them in parentheses.

One way to get LilyPond to place consecutive key signatures is by placing an invisible grace note in-between them.

## Code

\version "2.23.6"

\new Staff {
\repeat segno 2 {
\key bes \major
a'4 4 4 4 |
}
\alternative {
\volta 1 {
\key f \major
bes'4 4 4 4 |
\volta #'() {
%\bar ""     %% uncomment these lines
%\grace {s8} %% to remove the single barline
\once \override Staff.KeySignature.stencil =
#(lambda (grob)
(parenthesize-stencil
(ly:key-signature-interface::print grob)
0.1 0.5 1 0.2
)
)
\key bes \major
\grace {s8}
}
}
\volta 2 {
\section
\sectionLabel "Coda"
\break
}
}
\key ees\major
c''4 4 4 4 |
\fine
}


## Result

The old way of writing codas (or segnos) was by using the same symbol for both the place to jump from and to with the same symbols (as in your image), but the new way is only use the symbol at the first place, and to use words at the second place.
If you want to keep using the old method you can replace \sectionLabel "Coda" with: \sectionLabel \markup {\musicglyph #"scripts.coda"} in the above code. This will work but the automatic jump instruction will look a bit weird.

A coda really should be on a new staff, either with a space after the main part of the music, or after an indent on a new line. Unfortunately v2.23 can't be made to do this without difficulty (hopefully future versions will accommodate this).

• Thanks a lot, that's (almost) exactly what I wanted! I didn't know about the new segno repeat syntax, but I've wished for something like that for a while :) I prefer the end of the first line to not set the e flat major (for the next line) at all, since you'll never go to e flat major from there. With an additional \once \set Staff.explicitKeySignatureVisibility = #end-of-line-invisible before the \break statement this can be achieved. Apr 12 at 5:49
• @MichaelFütterer — Ah yeah that should work. Perhaps you do want the key sig. for the Coda to appear (in parentheses) before the barline at the end of the A section then? Apr 12 at 12:09
• That‘s a nice idea, and I can use your code then to achieve that. Thanks! Apr 12 at 18:49

Maybe something like this?

{
\repeat segno 2 {
\key bes\major
R1
}
\alternative {
{
\key f\major
R1
}
{
\break
\once\override Staff.KeySignature.stencil =
#(lambda (grob)
(if (= (ly:item-break-dir grob) -1)
(let ((stc (ly:key-signature-interface::print grob)))
(ly:grob-set-property! grob 'alteration-alist '((2 . -1/2) (-1 . -1/2)))
(ly:stencil-combine-at-edge stc X RIGHT
(parenthesize-stencil (ly:key-signature-interface::print grob)
0.12
0.5
0.3
0.2)
0.8))
(ly:key-signature-interface::print grob)))
\key ees\major
R1
}
}
}
`
• The coda is supposed to be outside of the repeat structure Apr 12 at 3:01