Skip to content

Commit

Permalink
Merge pull request #76 from andreasKroepelin/fancy-alternatives
Browse files Browse the repository at this point in the history
Implement more versions of alternatives
  • Loading branch information
andreasKroepelin authored Aug 26, 2023
2 parents c0032a0 + 3ec95ee commit ae50953
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
72 changes: 55 additions & 17 deletions logic.typ
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,71 @@
}
}

#let alternatives(
start: 1,
position: bottom + left,
repeat-last: false,
..children
) = {
#let alternatives-match(subslides-contents, position: bottom + left) = {
let subslides-contents = if type(subslides-contents) == "dictionary" {
subslides-contents.pairs()
} else {
subslides-contents
}

let subslides = subslides-contents.map(it => it.first())
let contents = subslides-contents.map(it => it.last())
style(styles => {
let sizes = children.pos().map(c => measure(c, styles))
let sizes = contents.map(c => measure(c, styles))
let max-width = calc.max(..sizes.map(sz => sz.width))
let max-height = calc.max(..sizes.map(sz => sz.height))
for (idx, child) in children.pos().enumerate() {
let last = idx == children.pos().len() - 1
let visible-subslides = if last and repeat-last {
(beginning: start + idx)
} else {
start + idx
}

only(visible-subslides, box(
for (subslides, content) in subslides-contents {
only(subslides, box(
width: max-width,
height: max-height,
align(position, child)
align(position, content)
))
}
})
}

#let alternatives(
start: 1,
repeat-last: false,
..args
) = {
let contents = args.pos()
let kwargs = args.named()
let subslides = range(start, start + contents.len())
if repeat-last {
subslides.last() = (beginning: subslides.last())
}
alternatives-match(subslides.zip(contents), ..kwargs)
}

#let alternatives-fn(
start: 1,
end: none,
count: none,
..kwargs,
fn
) = {
let end = if end == none {
if count == none {
panic("You must specify either end or count.")
} else {
start + count
}
} else {
end
}

let subslides = range(start, end)
let contents = subslides.map(fn)
alternatives-match(subslides.zip(contents), ..kwargs.named())
}

#let alternatives-cases(cases, fn, ..kwargs) = {
let idcs = range(cases.len())
let contents = idcs.map(fn)
alternatives-match(cases.zip(contents), ..kwargs.named())
}

#let line-by-line(start: 1, mode: "invisible", body) = {
let items = if repr(body.func()) == "sequence" {
body.children
Expand Down
2 changes: 1 addition & 1 deletion polylux.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "themes/themes.typ"
#import "logic.typ"
#import "logic.typ": polylux-slide, uncover, only, alternatives, one-by-one, line-by-line, pause, enable-handout-mode
#import "logic.typ": polylux-slide, uncover, only, alternatives, alternatives-match, alternatives-fn, alternatives-cases, one-by-one, line-by-line, pause, enable-handout-mode
#import "helpers.typ"
#import "helpers.typ": polylux-outline, fit-to-height, pdfpc
28 changes: 28 additions & 0 deletions tests/alternatives.typ
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,31 @@

#uncover(5)[You can go now.]
]

#polylux-slide[
== Test that `alternatives-match` works

#alternatives-match(position: center, (
"-2": [beginning],
"3, 5": [main part],
"4": [short break],
"6-" : [end]
))

#uncover("1-8")[I am always here, for technical reasons.]
]

#polylux-slide[
== Test that `alternatives-cases` works

#alternatives-cases(("1,3,4", "2,5", "6"), case => {
set text(fill: lime) if case == 1
lorem(10)
})
]

#polylux-slide[
== Test that `alternatives-fn` works

#alternatives-fn(count: 5, subslide => numbering("(i)", subslide))
]

0 comments on commit ae50953

Please sign in to comment.