Skip to content

Commit

Permalink
Added makelink option, supplement to thmref
Browse files Browse the repository at this point in the history
  • Loading branch information
sahasatvik committed Mar 30, 2023
1 parent 727cab8 commit e597c65
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Copy and import the [theorems.typ](theorems.typ) file to use in your own project
- Environments can share the same counter, via same `identifier`s.
- Environment counters can be _attached_ (just as subheadings are attached to headings) to other environments, headings, or keep a global count via `base`.
- The depth of a counter can be manually set, via `base_level`.
- Environment numbers can be referenced, via `#thmref(<label>)`.
- Environment numbers can be referenced, via `#thmref(<label>)[]`.
Currently, the `<label>` must be placed _inside_ the environment.

## Manual and Examples
Expand Down Expand Up @@ -56,7 +56,7 @@ The [differential_calculus.typ](differential_calculus.typ) ([render](differentia
]
#example[
The numbers $2$, $3$, and $17$ are prime.
Corollary #thmref(<cor_largest_prime>) shows that this list is not
#thmref(<cor_largest_prime>)[Corollary] shows that this list is not
exhaustive!
]
Expand Down
Binary file modified basic.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion basic.typ
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
]
#example[
The numbers $2$, $3$, and $17$ are prime.
Corollary #thmref(<cor_largest_prime>) shows that this list is not
#thmref(<cor_largest_prime>)[Corollary] shows that this list is not
exhaustive!
]

Expand Down
Binary file modified manual.pdf
Binary file not shown.
79 changes: 51 additions & 28 deletions manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ _Corollaries_ can be created as follows.

Note that we have provided a `numbering` string; this can be any valid
numbering pattern as described in the
#link("https://typst.app/docs/reference/meta/numbering/")[numbering]
#linkb("https://typst.app/docs/reference/meta/numbering/")[numbering]
documentation.


Expand Down Expand Up @@ -191,7 +191,7 @@ You can limit the number of levels of the `base` numbering used as follows.
#definition(name: "Prime numbers")[
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.
cannot be written as the product of two smaller natural numbers. <prime>
]
```
#let definition = thmbox(
Expand All @@ -202,7 +202,7 @@ You can limit the number of levels of the `base` numbering used as follows.
)
#definition(name: "Prime numbers")[
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.
cannot be written as the product of two smaller natural numbers. <prime>
]

Note that this environment is _not_ numbered 3.2.1!
Expand Down Expand Up @@ -318,39 +318,58 @@ You can place a `<label>` _inside_ a theorem environment to reference it later
via `thmref`.

```typst
#let numfmt = (nums) => {
let joined = nums.map(str).join(".")
return [(#strong(joined))]
}
Recall that there are infinitely many prime numbers via Theorem
#thmref(<euclid>).
Recall that there are infinitely many prime numbers via
#thmref(<euclid>)[Theorem].
```
#let numfmt = (nums) => {
let joined = nums.map(str).join(".")
return [(#strong(joined))]
}
#pad(
left: 1.2em,
[
Recall that there are infinitely many prime numbers via Theorem
#thmref(<euclid>, fmt: numfmt).
Recall that there are infinitely many prime numbers via
#thmref(<euclid>)[Theorem].
]
)

The optional `fmt` argument can be used to convert the counter value (an array
of integers) into content.
of integers) and the optional body text into content.

```typst
You can reference future environments too, like Corollary
#thmref(<oddprime>).
#let numfmt = (nums, body) => {
if body.pos().len() > 0 {
body = body.pos().join(" ")
return smallcaps([#body (#strong(numbering("1.1", ..nums)))])
}
return smallcaps(strong(numbering("1.1", ..nums)))
}
You can reference future environments too, like
#thmref(<oddprime>, fmt: numfmt)[Corollary].
```
#let numfmt = (nums, body) => {
if body.pos().len() > 0 {
body = body.pos().join(" ")
return smallcaps([#body (#strong(numbering("1.1", ..nums)))])
}
return smallcaps(strong(numbering("1.1", ..nums)))
}
#pad(
left: 1.2em,
[
You can reference future environments too, like
#thmref(<oddprime>, fmt: numfmt)[Corollary].
]
)

Note that all such references are links to the the label location. The
`makelink` argument lets you disable this behaviour.
```typst
This reference to #thmref(<prime>, makelink: false)[Definition] is not
linked!
```
#pad(
left: 1.2em,
[
You can reference future environments too, like Corollary
#thmref(<oddprime>).
This reference to #thmref(<prime>, makelink: false)[Definition] is not
linked!
]
)

Expand Down Expand Up @@ -419,15 +438,20 @@ A `<label>` placed within an environment can be referenced using `thmref`.
```typst
#let thmref(
label, // label
fmt: nums => numbering("1.1", ..nums)
// formatting function, of the form
// [array of integers] -> content
fmt: auto, // formatting function, of the form
// (number array, body arguments) -> content
makelink: true, // create link to label
..body // body - typically prepended to number
) = { ... }
```

Note that the `<label>` _must_ be attached to something _inside_ the
environment.

*Caution*: Links created by `thmref` will be styled according to `#show link:`
rules, not `#show ref:` rules.


== `thmbox` and `thmplain`

The `thmbox` wraps `thmenv`, supplying a box-like `fmt` function.
Expand Down Expand Up @@ -466,7 +490,6 @@ defaults.

= Acknowledgements

Thanks to #link("https://github.com/MJHutchinson")[MJHutchinson] for
suggesting and implementing the `base_level` and `base: none` features,
and to the awesome devs of #link("https://typst.app/")[typst.app] for their
support.
Thanks to #linkb("https://github.com/MJHutchinson")[MJHutchinson] for
suggesting and implementing the `base_level` and `base: none` features, and to
the awesome devs of #linkb("https://typst.app/")[typst.app] for their support.
Binary file modified manual_examples.pdf
Binary file not shown.
54 changes: 40 additions & 14 deletions manual_examples.typ
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ these has been explained in full detail there.
"Lemma",
fill: rgb("#efe6ff")
)

#lemma[
If $n$ divides both $x$ and $y$, it also divides $x - y$.
]


#let corollary = thmbox(
"corollary",
"Corollary",
Expand Down Expand Up @@ -77,9 +77,10 @@ these has been explained in full detail there.
base_level: 1,
stroke: rgb("#68ff68") + 1pt
)

#definition(name: "Prime numbers")[
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.
cannot be written as the product of two smaller natural numbers. <prime>
]

#definition(name: "Composite numbers")[
Expand All @@ -93,6 +94,7 @@ these has been explained in full detail there.


== Custom formatting

#let proof = thmplain(
"proof",
"Proof",
Expand All @@ -113,16 +115,16 @@ these has been explained in full detail there.
]

#let notation = thmenv(
"notation",
none,
none,
(name, number, body) => [
"notation", // identifier
none, // base - do not attach, count globally
none, // base_level - use the base as-is
(name, number, body) => [ // fmt - format content using the environment name, number, and body
#h(1.2em) *Notation (#number) #name*:
#h(0.2em)
#body
#v(0.5em)
]
).with(numbering: "I")
).with(numbering: "I") // use Roman numerals

#notation[
The variable $p$ is reserved for prime numbers.
Expand All @@ -131,27 +133,51 @@ these has been explained in full detail there.

== Labels and references <references>

#let numfmt = (nums) => {
let joined = nums.map(str).join(".")
return [(#strong(joined))]
#pad(
left: 1.2em,
[
Recall that there are infinitely many prime numbers via
#thmref(<euclid>)[Theorem].
]
)

#let numfmt = (nums, body) => {
if body.pos().len() > 0 {
body = body.pos().join(" ")
return smallcaps([#body (#strong(numbering("1.1", ..nums)))])
}
return smallcaps(strong(numbering("1.1", ..nums)))
}

Recall that there are infinitely many prime numbers via Theorem
#thmref(<euclid>, fmt: numfmt).
#pad(
left: 1.2em,
[
You can reference future environments too, like
#thmref(<oddprime>, fmt: numfmt)[Corollary].
]
)

You can reference future environments too, like Corollary
#thmref(<oddprime>).
#pad(
left: 1.2em,
[
This reference to #thmref(<prime>, makelink: false)[Definition] is not
linked!
]
)


== Overriding `base`

#let remark = thmplain("remark", "Remark", base: "heading")

#remark[
There are infinitely many composite numbers.
]

#corollary[
All primes greater than $2$ are odd. <oddprime>
]

#remark(base: "corollary")[
Two is a _lone prime_.
]
6 changes: 4 additions & 2 deletions manual_template.typ
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#let linkb = (..it) => underline(text(fill: blue, link(..it)))

#let project(title: "", authors: (), url: "", body) = {
set page(paper: "a4", numbering: "1", number-align: center)
set document(author: authors, title: title)
set text(font: "Linux Libertine", lang: "en")
set heading(numbering: "1.1.")
show heading: it => pad(bottom: 0.5em, it)
set par(justify: true)
show link: it => underline(text(fill: blue, it))
show raw.where(block: true): it => pad(left: 4em, it)



align(center)[
#block(text(weight: 700, 1.75em, title))
]
Expand All @@ -22,7 +24,7 @@
gutter: 1em,
..authors.map(author => align(center)[
#author \
#link(url)
#linkb(url)
]),
),
)
Expand Down
22 changes: 20 additions & 2 deletions theorems.typ
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,33 @@
// 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)) = {
#let thmref(
label,
fmt: auto,
makelink: true,
..body
) = {
if fmt == auto {
fmt = (nums, body) => {
if body.pos().len() > 0 {
body = body.pos().join(" ")
return [#body #numbering("1.1", ..nums)]
}
return numbering("1.1", ..nums)
}
}

locate(loc => {
let elements = query(label, loc)
let locationreps = elements.map(x => repr(x.location().position())).join(", ")
assert(elements.len() > 0, message: "label <" + str(label) + "> does not exist in the document: referenced at " + repr(loc.position()))
assert(elements.len() == 1, message: "label <" + str(label) + "> occurs multiple times in the document: found at " + locationreps)
let target = elements.first().location()
let number = thmcounters.at(target).at("latest")
return link(target, fmt(number))
if makelink {
return link(target, fmt(number, body))
}
return fmt(number, body)
})
}

Expand Down

0 comments on commit e597c65

Please # to comment.