From ab3381d2331b96aadccb6ad0b833b3328426611d Mon Sep 17 00:00:00 2001 From: Marc Thieme Date: Fri, 23 Feb 2024 18:46:10 +0100 Subject: [PATCH] Feat: Add tags which can be provided on each theorem instantiation and which are accessible to the styling --- src/lib.typ | 33 +++++++++++++++++++++++++++++---- src/theorem.typ | 2 ++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/lib.typ b/src/lib.typ index ac1a757..879b605 100644 --- a/src/lib.typ +++ b/src/lib.typ @@ -8,6 +8,19 @@ #let LEMMIFY-DEFAULT-THEOREM-GROUP = "LEMMIFY-DEFAULT-THEOREM-GROUP" #let LEMMIFY-DEFAULT-PROOF-GROUP = "LEMMIFY-DEFAULT-PROOF-GROUP" +#let validate-and-extract-tags(tag-defaults, tags-args) = { + assert-type(tags-args, "tags", arguments) + assert(tags-args.pos() == (), message: "The tags (custom parameters) may not be positional. Yours were " + repr(tags-args.pos())) + let tags = tags-args.named() + + for (key, value) in tags { + assert(key in tag-defaults, message: "Your tag with the key " + key + "(=" + repr(value) + ") was not expected") + } + + return tag-defaults + tags +} + + /// Creates a new #ref-type("theorem-function"). /// /// - kind-name (str): The name of the theorem kind. It also acts @@ -24,6 +37,10 @@ /// which is then used in the #ref-type("theorem-numbering-function"). /// - style (style-function): Specifies how the #ref-type("theorem")s will look. This will only be /// visible once the @@theorem-rules() have been applied. +/// - tags (dictionary): Tags are optional parameters the author can previde for each instance of a +/// theorem in his document separately. A dictionary whose keys are the available tags for this +/// kind of theorem and the values are the default values for those tags. +/// Tags are exposed to the styling function through the 'tags'-value of the theorem parameters. /// -> theorem-function #let theorem-kind( kind-name, @@ -31,7 +48,8 @@ link-to: last-heading, numbering: numbering-concat, subnumbering: "1", - style: style-simple + style: style-simple, + tags: (:) ) = { assert-type(kind-name, "kind-name", str) assert-type(group, "group", str) @@ -39,11 +57,13 @@ assert-type(numbering, "numbering", function, None) assert-type(subnumbering, "subnumbering", function, str, None) assert-type(style, "style", function) + assert-type(tags, "tag defaults", dictionary) return ( name: none, link-to: link-to, numbering: numbering, + ..provided-tags, body ) => create-theorem( name, @@ -53,6 +73,7 @@ numbering, subnumbering, style, + validate-and-extract-tags(tags, provided-tags), body ) } @@ -110,6 +131,7 @@ /// be reset on headings below `max-reset-level`. /// And if `link-to` is set to `last-heading` /// higher levels will not be displayed in the numbering. +/// - tags (dictionary): /// -> dictionary #let default-theorems( group: LEMMIFY-DEFAULT-THEOREM-GROUP, @@ -122,7 +144,8 @@ link-to: last-heading, proof-link-to: none, subnumbering: "1", - max-reset-level: none + max-reset-level: none, + tags: (:), ) = { assert-type(group, "group", str) assert-type(proof-group, "proof-group", str) @@ -150,7 +173,8 @@ numbering: numbering, subnumbering: subnumbering, style: style, - link-to: link-to + link-to: link-to, + tags: tags, )) } @@ -160,7 +184,8 @@ numbering: proof-numbering, subnumbering: subnumbering, style: proof-style, - link-to: proof-link-to + link-to: proof-link-to, + tags: tags, )) let rules = concat-fold(( diff --git a/src/theorem.typ b/src/theorem.typ index 4a33b91..3a75c4d 100644 --- a/src/theorem.typ +++ b/src/theorem.typ @@ -10,6 +10,7 @@ numbering, subnumbering, style, + tags, body ) = { assert-type(name, "name", str, content, None) @@ -31,6 +32,7 @@ link-to: link-to, numbering: numbering, subnumbering: subnumbering, + tags: tags, style: style ) )