# Better way to repeat with alternative at the beginning

Using LilyPond, I would like to repeat a section with an alternative at the beginning. My desired unfolded structure is "A-B-A-B-C-B", so that A becomes C the third time through. I have tried the following solution

``````music = \fixed c {
\repeat volta 3 {
\alternative {
\volta 1,2 { s1_"A" }
\volta 3 { s1_"C" }
}
s1_"B"
}
}

\score { \music }
\score { \unfoldRepeats \music }
``````

But I am looking for a solution where the folded version reads "A-B-C", with an alternative on "C" that leads back to a fine after "B", maybe via a segno.

Is there a way to do this?

• The structure is easy: A-B repeated, then C then dal segno B until fine. Jan 20 at 23:33
• Thanks. That's how I pictured it but I can't find a way to translate this into a working lilypond script. Jan 23 at 14:10

The structure that you’ll want to use has both a Volta repeat loop (AB-AB) and a Dal Segno al Fine repeat loop (B-C-B), it will look something like this:

### Problem

In LilyPond both of these repeat structures work fine by themselves, but when they are combined like this it gets complicated. This logical flowchart shows why:

These loops aren’t sequential because the B is inside both. But they aren’t nested either. Also, they can’t be combined into a single loop because they are different types, and there is no way of indicating to LilyPond whether the `\volta n` instructions are referring to the volta loop or the segno loop.

(The `\repeat segno` structure has only been available since v2.23, so it’s relatively new and still needs some work — perhaps the developers need to create a `\segno n` command.)

### Solution

To get around this problem, you can use some of the special repeat commands. The command `\volta #'()` can be used to add markup/symbols (such as the manual segno repeat mark `\segnoMark`) to the folded representation of the score only. The command `\unfolded` does the opposite.

But even with these, unfortunately, you’ll still end up having to write the B section out twice in the code (unless you make it a variable).

#### Code

The following will look right (like the image at the top of this answer) when folded, and importantly it will also unfold correctly.

``````    {
\repeat segno 2 {
\volta 1 {
\repeat volta 2 {
s1_"A" |
\volta #'() { \segnoMark 1 }
s1_"B" |
\volta #'() { \fine }
\section
}
}
\volta 2 {
s1_"C" |
\section
\unfolded { s1_"B" | \fine }
}
}
}
``````
• Thanks a lot. Indeed, I forgot to explicitly state the constraint (or my desire) of writing each part only once, but embedding the code in a variable kind of solves this issue. I also noticed that with the `\repeat segno` solution, there is an another implicit segno mark that would show if some music preceded A section. So, for this fairly common structure, it seems quite complex to fulfill all conditions (write music once, correct folded and unfolded structure). Jan 23 at 14:31
• @matvila I didn’t notice that extra segno mark before. If you want to get rid of it, place `\omit\segnoMark 1` before the A section (inside or outside of the repeat structure). Jan 23 at 15:00
• This is all just a workaround for the problem that the two loops are inherently inseparable and both are competing for the `\volta n` command. As I suggested, I think the developers probably need to make a separate `\segno n` command for a elegant solution. Jan 23 at 15:15
• Great. Thanks for your help Jan 23 at 16:18