Skip to content

Commit

Permalink
Handle nested equations
Browse files Browse the repository at this point in the history
- Remove numbering
- Disable equate show rule
  • Loading branch information
EpicEricEE committed Oct 20, 2024
1 parent ae60cc5 commit e1cd504
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/equate.typ
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// State for tracking whether we're in a shared alignment block.
#let share-align-state = state("equate/share-align", false)

// State for tracking whether we're in a nested equation.
#let nested-state = state("equate/nested-depth", 0)

// Show rule necessary for referencing equation lines, as the number is not
// stored in a counter, but as metadata in a figure.
#let equate-ref(it) = {
Expand Down Expand Up @@ -451,6 +454,11 @@

show math.equation.where(block: true): set block(breakable: breakable) if type(breakable) == bool
show math.equation.where(block: true): it => {
// Don't apply show rule in a nested equations.
if nested-state.get() > 0 {
return it
}

// Allow a way to make default equations.
if it.has("label") and it.label == <equate:revoke> {
return it
Expand Down Expand Up @@ -589,6 +597,19 @@
}
}

// Apply this show rule first to update nested state.
// This works because the context provided by the other show rule does not
// yet include the updated state, so it can be retrieved correctly.
show math.equation.where(block: true): it => {
// Turn off numbering for nested equations, as it's usually unwanted.
// Workaround for https://github.com/typst/typst/issues/5263.
set math.equation(numbering: none)

nested-state.update(n => n + 1)
it
nested-state.update(n => n - 1)
}

// Add show rule for referencing equation lines.
show ref: equate-ref

Expand Down
Binary file added tests/nested/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.
13 changes: 13 additions & 0 deletions tests/nested/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import "/src/lib.typ": equate

#set page(width: 6cm, height: auto, margin: 1em)
#show: equate

// Test handling of nested equations.

$ a + b &= lr(\{#block[$ e \ #block[$ f \ g $] $]) $

#set math.equation(numbering: "(1.1)")

$ a + b &= c \
&= lr(\{#block[$ e \ f $] + #block[$ g \ h $]) $
2 changes: 2 additions & 0 deletions tests/share-align/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#set page(width: 6cm, height: auto, margin: 1em)
#show: equate

// Test correct behavior of shared alignment blocks.

#share-align[
$ a + b &= c $
leads to
Expand Down

0 comments on commit e1cd504

Please sign in to comment.