Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Number only labelled lines when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicEricEE committed May 4, 2024
1 parent 0aac2a6 commit dd84596
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 58 deletions.
97 changes: 43 additions & 54 deletions equate/src/equate.typ
Original file line number Diff line number Diff line change
Expand Up @@ -94,51 +94,56 @@
// Main equation number.
let main-number = counter(math.equation).get()

// Indices of lines that contain a label.
let labelled = lines
.enumerate()
.filter(((i, line)) => {
if line.len() == 0 { return false }
if line.last().func() != raw { return false }
if line.last().lang != "typc" { return false }
if line.last().text.match(regex("^<.+>$")) == none { return false }
return true
})
.map(((i, _)) => i)

// Indices of numbered lines in this equation.
let numbered = if number-mode == "line" or has-label {
let numbered = if number-mode == "line" {
range(lines.len())
} else if labelled.len() == 0 and has-label {
// Only outer label, so number all lines.
range(lines.len())
} else {
lines.enumerate()
.filter(((i, line)) => {
if line.len() == 0 { return false }
if line.last().func() != raw { return false }
if line.last().lang != "typc" { return false }
if line.last().text.match(regex("^<.+>$")) == none { return false }
return true
})
.map(((i, _)) => i)
labelled
}

lines.enumerate()
.map(((i, line)) => {
if line.len() == 0 { return line }

let last = line.last()
if last.func() != raw { return line }
if last.lang != "typc" { return line }
if last.text.match(regex("^<.+>$")) == none { return line }
(
numbered,
lines.enumerate()
.map(((i, line)) => {
if i not in labelled { return line }

// Remove trailing spacing (before label).
if line.at(-2, default: none) == [ ] { line.remove(-2) }
// Remove trailing spacing (before label).
if line.at(-2, default: none) == [ ] { line.remove(-2) }

// Append sub-numbering only if there are multiple numbered lines.
let nums = main-number + if numbered.len() > 1 {
(numbered.position(n => n == i) + 1,)
}
// Append sub-numbering only if there are multiple numbered lines.
let nums = main-number + if numbered.len() > 1 {
(numbered.position(n => n == i) + 1,)
}

// We use a figure with kind "equation" to make the sub-equation
// referenceable with the correct supplement. The numbering is stored
// in the figure body as metadata, as a counter would only show a
// single number.
line.at(-1) = [#figure(
metadata(nums),
kind: math.equation,
numbering: numbering,
supplement: supplement
)#label(last.text.slice(1, -1))]

return line
})
// We use a figure with kind "equation" to make the sub-equation
// referenceable with the correct supplement. The numbering is stored
// in the figure body as metadata, as a counter would only show a
// single number.
line.at(-1) = [#figure(
metadata(nums),
kind: math.equation,
numbering: numbering,
supplement: supplement
)#label(line.last().text.slice(1, -1))]

return line
})
)
}

// Splitting an equation into multiple lines breaks the inbuilt alignment
Expand Down Expand Up @@ -285,30 +290,14 @@
if it.number-align.x == start { right } else { left }
}

let lines = replace-labels(
let (numbered, lines) = replace-labels(
to-lines(it),
number-mode,
it.numbering,
it.supplement,
it.has("label")
)

// Indices of numbered lines in this equation.
let numbered = if number-mode == "line" or it.has("label") {
range(lines.len())
} else {
// Find lines that have a replaced label.
lines.enumerate()
.filter(((i, line)) => {
if line.len() == 0 { return false }
if line.last().func() != figure { return false }
if line.last().body == none { return false }
if line.last().body.func() != metadata { return false }
return true
})
.map(((i, _)) => i)
}

// Short-circuit for single-line equations.
if lines.len() == 1 {
if it.numbering == none { return it }
Expand Down
Binary file modified equate/tests/number-mode/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions equate/tests/number-mode/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ $ k + l $
$ m + n \
o + p $ <label>

$ q + r \
s + t $ <equate:revoke>
$ q + r #<label> \
s + t $ <label>

$ u + v \
w + x $ <equate:revoke>

#show: equate.with(sub-numbering: true, number-mode: "label")

Expand All @@ -42,5 +45,8 @@ $ k + l $
$ m + n \
o + p $ <label>

$ q + r \
s + t $ <equate:revoke>
$ q + r #<label> \
s + t $ <label>

$ u + v \
w + x $ <equate:revoke>

0 comments on commit dd84596

Please sign in to comment.