jbirnick / typst-great-theorems

Straightforward and functional theorem/proof environments in Typst.
MIT License
6 stars 2 forks source link

Override counter #2

Closed shivangp76 closed 1 month ago

shivangp76 commented 1 month ago

Hi,

Thank you for making this package! I was wondering if it is possible to override a counter for a specific instance of a mathblock. For example, I made an "Exercise" block using code similar to that provided in the README. I want to have a default counter, but be able to override the counter with specific text. Something like:

#exercise[
#lorem(100)
]
#exercise["6.7(a)"][
#lorem(100)
]
#exercise[
#lorem(100)
]

to render something like:

Exercise 1
Exercise 6.7(a)
Exercise 2
jbirnick commented 1 month ago

Hey, thanks for your question!

It seems like you don't even want the exercise environment itself to be counted. In that case, the best solution right now is to make a second exercise-extra environment, which looks just like the first one, but has no counter:

#import "@preview/great-theorems:0.1.1": *
#import "@preview/rich-counters:0.2.1": *

#show: great-theorems-init

#let mathcounter = rich-counter(
  identifier: "mathblocks",
  inherited_levels: 0
)

#let exercise = mathblock(
  blocktitle: "Exercise",
  counter: mathcounter,
)

#let exercise-extra = mathblock(
  blocktitle: "Exercise",
  // put same properties as `exercise` except the counter
)

#exercise[
  Some exercise.
]

#exercise-extra[
  Uncounted exercise.
]

#exercise[
  Some exercise.
]

image

Now you can adjust the prefix for that environment on an individual basis:

#exercise[
  Some exercise.
]

#exercise-extra(prefix: [*Exercise 6.7(a).*])[
  Uncounted exercise.
]

#exercise[
  Some exercise.
]

image

Also note that titles exist, if you want to utilize them:

#exercise[
  Some exercise.
]

#exercise(title: [6.7(a) in the book])[
  Some other exercise.
]

#exercise[
  Some exercise.
]

image

jbirnick commented 1 month ago

Replacing the counter value directly is currently not possible. If more people want this feature, I can add it though.

So if you would like to have this feature (to replace the counter value with your own number) please comment here.

shivangp76 commented 1 month ago

Thank you for the suggestions! The extra suffix seems a bit clunky. It also requires that I specify the full blocktitle, so I have to retype "Exercise". This means I can't edit the markup on "Exercise" globally. For this reason, I am in favor of replacing the counter value directly.

For reference, here is the Latex I am aiming to replicate:

  \newcounter{exercisenum}
  \newenvironment{exercise}[1][\theexercisenum]{
    \IfValueT{#1}{
      \stepcounter{exercisenum}
    }
    \textbf{Exercise #1:}
  }
  {}
jbirnick commented 1 month ago

I have implemented this now. Please have a look at the README, you have to pass number: ... to the environment.

Note that this is not yet on Typst Universe (and will take a few days at least). Please download lib.typ from this repo and use #import "lib.typ": * instead of importing the package.

Could you try it and tell me if it works for you or if anything breaks?

shivangp76 commented 1 month ago

Thank you for working on this! It works well. The trick mentioned here doesn't work, but I suspect that will change as #3 get resolved anyway.

jbirnick commented 1 month ago

Wait, what do you mean by this trick doesn't work? It should work exactly the same as before, the code which handles this hasn't been changed. Can you give a reproducible example of what doesn't work?

shivangp76 commented 1 month ago

I think I did a poor job of testing it last time. It looks fine.

jbirnick commented 1 month ago

Okay thanks. I will close this issue then. I hope to release the functionality to Typst Universe soon.