Skip to content

Commit

Permalink
Added referencing using labels
Browse files Browse the repository at this point in the history
  • Loading branch information
sahasatvik committed Mar 29, 2023
1 parent bbbc906 commit 37f8119
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ An implementation of numbered theorem environments in
[typst](https://github.com/typst/typst).
Copy and import the [theorems.typ](theorems.typ) file to use in your own projects.

### Features
- Numbered theorem environments can be created and customized.
- Environment counters can be _attached_ (just as subheadings are attached to headings) to other environments, headings, or keep a global count.
- Environment numbers can be referenced, via `#thmref(<label>)`.
Currently, the `<label>` must be placed _inside_ the environment.

## Examples

Minimal example below; also see [example.typ](example.typ) ([render](example.pdf)) for a demonstration of more features, and [differential_calculus.typ](differential_calculus.typ) ([render](differential_calculus.pdf)) for a practical use case.

![basic example](basic.png)

### Preamble
```
#import "theorems.typ": *
Expand All @@ -21,7 +30,7 @@ Minimal example below; also see [example.typ](example.typ) ([render](example.pdf
base: "theorem",
titlefmt: strong
)
#let definition = thmbox("definition", "Definition")
#let definition = thmbox("definition", "Definition", inset: (x: 1.2em, top: 1em))
#let example = thmplain("example", "Example").with(numbering: none)
#let proof = thmplain(
Expand All @@ -30,15 +39,21 @@ Minimal example below; also see [example.typ](example.typ) ([render](example.pdf
base: "theorem",
bodyfmt: body => [#body #h(1fr) $square$]
).with(numbering: none)
```

### Document
```
= Prime numbers
#definition[
A natural number is called a _prime number_ if it is greater than 1
and cannot be written as the product of two smaller natural numbers.
]
#example[The numbers $2$, $3$, and $17$ are prime.]
#example[
The numbers $2$, $3$, and $17$ are prime.
Corollary #thmref(<cor_largest_prime>) shows that this list is not
exhaustive!
]
#theorem(name: "Euclid")[
There are infinitely many primes.
Expand All @@ -51,7 +66,12 @@ Minimal example below; also see [example.typ](example.typ) ([render](example.pdf
contradiction.
]
#corollary[There is no largest prime number.]
#corollary[There are infinitely many composite numbers.]
#corollary[
There is no largest prime number. <cor_largest_prime>
]
#corollary[
There are infinitely many composite numbers.
]
```

Binary file modified basic.pdf
Binary file not shown.
Binary file modified basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions basic.typ
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
base: "theorem",
titlefmt: strong
)
#let definition = thmbox("definition", "Definition")
#let definition = thmbox("definition", "Definition", inset: (x: 1.2em, top: 1em))

#let example = thmplain("example", "Example").with(numbering: none)
#let proof = thmplain(
Expand All @@ -30,6 +30,8 @@
]
#example[
The numbers $2$, $3$, and $17$ are prime.
Corollary #thmref(<cor_largest_prime>) shows that this list is not
exhaustive!
]

#theorem(name: "Euclid")[
Expand All @@ -44,7 +46,7 @@
]

#corollary[
There is no largest prime number.
There is no largest prime number. <cor_largest_prime>
]
#corollary[
There are infinitely many composite numbers.
Expand Down
Binary file modified example.pdf
Binary file not shown.
17 changes: 13 additions & 4 deletions example.typ
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@
]

#corollary[#lorem(4)]
#corollary[#lorem(8)]
#corollary[
This corollary will be referenced later, in
Lemma #thmref(<lemref>). // Reference future lemma
<corref> // Label this corollary
]

#example[#lorem(10)]

Expand All @@ -105,7 +109,7 @@
#notation[#lorem(7)]

#example(name: [#lorem(3)])[#lorem(10)]
#remark[#lorem(5)]
#remark[#lorem(8)]

#theorem[#lorem(6)]

Expand All @@ -114,10 +118,15 @@
#proof(numbering: "1.1")[#lorem(5)]

= Heading
#lemma[#lorem(14)]
#lemma[
#lorem(14)
Refer to Corollary #thmref(<corref>). <lemref>
]
#remark[#lorem(8)]

#corollary(name: [#lorem(4)])[#lorem(12)]
#corollary(name: [#lorem(4)])[
#lorem(12) <myref>
]
// The base can be overridden
#example(numbering: "1.1.1.a", base: "corollary")[#lorem(20)]
#example(numbering: "1.1.1.a", base: "corollary")[#lorem(10)]
Expand Down
32 changes: 25 additions & 7 deletions theorems.typ
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// Store theorem environment numbering

#let thmcounters = state("thm",
(
"counters": ("": (), "heading": ()),
"latest": ()
)
)

// Create a theorem environment with counter identified by "identifier",
// attached to environments with identifier "base". Contents are formatted
// using "fmt", which maps (name, number, body) to content.
Expand All @@ -13,13 +22,6 @@

#let thmenv(identifier, base, fmt) = {

let thmcounters = state("thm",
(
"counters": ("": (), "heading": ()),
"latest": ()
)
)

let global_numbering = numbering

return (body, name: none, numbering: "1.1", base: base) => {
Expand Down Expand Up @@ -59,6 +61,22 @@
}


// Reference a theorem with a <label> _inside_ it, using #thmref(<label>).
// Optionally supply a "fmt" function to change the display style.

#let thmref(label, fmt: nums => numbering("1.1", ..nums)) = {
locate(loc => {
let elements = query(label, loc)
assert(elements.len() > 0, message: "Label not found!")
assert(elements.len() == 1, message: "Multiple labels found!")
let number = thmcounters.at(
elements.first().location()
).at("latest")
return fmt(number)
})
}


// Creates a box-like theorem environment with parameters "identifier" and "base" (defaulted to "heading").
// head indicates the name of the environment appearing in the title
// namefmt: formatting to apply to the "name", defaults to wrapping in parentheses
Expand Down

0 comments on commit 37f8119

Please # to comment.