Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved version of equation-desc.typ in examples #11

Open
fakeGenuis opened this issue Dec 25, 2024 · 0 comments
Open

Improved version of equation-desc.typ in examples #11

fakeGenuis opened this issue Dec 25, 2024 · 0 comments

Comments

@fakeGenuis
Copy link

fakeGenuis commented Dec 25, 2024

Hi,
I really enjoy using this package, especially the pinit-highlight-equation-from function. However, I was wondering if it could be improved to:

  1. Allow placement relative to the left and right margins.
  2. Adjust the inset when placing a formula (or something with higher than normal text) in the body text.

Here’s my attempt to implement it:

// Example adapted from: @Matt https://discord.com/channels/1054443721975922748/1088371919725793360/1166508915572351067

#import "@preview/pinit:0.2.2": *

#set page(width: 700pt, height: auto, margin: 30pt)
#set text(size: 20pt)
#set math.equation(numbering: "(1)")

#let pinit-highlight-equation-from(height: 2em, pos: (right, bottom), dy: 0.1em, fill: rgb(0, 180, 255), highlight-pins, point-pin, body) = {
  pinit-highlight(..highlight-pins, dy: -0.9em, fill: rgb(..fill.components().slice(0, -1), 40))
  context {
    let pos_x = pos.at(0)
    let pos_y = pos.at(1)
    let insett = measure(body).height/2 + dy
    pinit-point-from(
      fill: fill, pin-dx: 0em, offset-dx: 0em,
      body-dx: if pos_x == right { 0em } else { -measure(body).width - 2*insett },
      body-dy: -measure(body).height - 2*insett + (if pos_y == bottom {-0.06em} else {0.06em}),
      pin-dy: if pos_y == bottom { 0.5em } else { -0.9em },
      offset-dy: if pos_y == bottom { 0.8em + height } else { -0.6em - height },
      point-pin,
      rect(
        inset: insett,
        stroke: (bottom: 0.12em + fill),
        {
          set text(fill: fill)
          body
        }
      )
    )}
}

Equation written out directly (for comparison):

$ (q_T^* p_T)/p_E p_E^* >= (c + q_T^* p_T^*)(1+r^*)^(2N) $

Laid out with pinit:

#v(3.5em)

$ (#pin(1)q_T^* p_T#pin(2))/(#pin(3)p_E#pin(4))#pin(5)p_E^*#pin(6) >= (c + q_T^* p_T^*)(1+r^*)^(2N) $

#v(5em)

#pinit-highlight-equation-from((1, 2, 3, 4), (3, 4), height: 3.5em, pos: (left, bottom), fill: rgb(0, 180, 255))[
  quantity of Terran goods
]

#pinit-highlight-equation-from((1, 2, 3, 4), (1, 2), dy: 0.3em, height: 2.5em, pos: (left, top), fill: rgb(0, 180, 255))[
  $(q_T^* p_T)/p_E p_E^*$
]

#pinit-highlight-equation-from((5, 6), (5, 6), height: 2.5em, pos: (right, top), fill: rgb(150, 90, 170))[
  price of Terran goods, on Trantor
]

Paragraph after the equation.

I’m still new to Typst. Even though I use measure to get the height of the body, the formula often gets too close to the stroke line when the body has more height. As a result, I have to manually adjust dy. Any suggestions to handle this more effectively?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant