OrangeX4 / typst-pinit

Relative positioning by pins, especially useful for making slides in typst.
MIT License
177 stars 4 forks source link

Pinit

Relative positioning by pins, especially useful for making slides in typst.

Example

Pin things as you like

Have a look at the source here.

Example

Dynamic Slides

Pinit works with Touying or Polylux animations.

Have a look at the pdf file here.

Example Pages

Usage

Examples

The idea of pinit is pinning pins on the normal flow of the text, and then placing the content on the page by absolute-place function.

For example, we can highlight text and add a tip by pins simply:

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

#set text(size: 24pt)

A simple #pin(1)highlighted text#pin(2).

#pinit-highlight(1, 2)

#pinit-point-from(2)[It is simple.]

simple-demo

If you want to place the content relative to the center of some pins, you use a array of pins:

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

#set text(size: 12pt)

A simple #pin(1)highlighted text#pin(2).

#pinit-highlight(1, 2)

#pinit-point-from((1, 2))[It is simple.]

simple-demo2

A more complex example, Have a look at the source here.

equation-desc

Fletcher edge support

Fletcher is a powerful Typst package for drawing diagrams with arrows. We can use fletcher to draw more complex arrows.

pinit-fletcher-edge

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

Con#pin(1)#h(4em)#pin(2)nect

#pinit-fletcher-edge(
  fletcher, 1, end: 2, (1, 0), [bend], bend: -20deg, "<->",
  decorations: fletcher.cetz.decorations.wave.with(amplitude: .1),
)

fletcher

Pinit for raw

In the code block, we need to use a regex trick to get pinit to work, for example

#show raw: it => {
  show regex("pin\d"): it => pin(eval(it.text.slice(3)))
  it
}

`print(pin1"hello, world"pin2)`

#pinit-highlight(1, 2)

equation-desc

Note that typst's code highlighting breaks up the text, causing overly complex regular expressions such as '#pin(.*?)' to not work properly.

However, you may want to consider putting it in a comment to avoid highlighting the text and breaking it up.

Notice

Since Typst does not provide a reliable absolute-place function, you may consider taking the following steps if a MISALIGNMENT occurs:

  1. You could try to add a #box() after the #pinit-xxx function call, like #pinit-xxx()#box().
  2. You should add a blank line before the #pinit-xxx function call, otherwise it will cause misalignment.
  3. You can try moving #pinit-xxx() in front of or behind #pin(), or otherwhere, in short, try more.
  4. Try to add a offset to the dx or dy argument of #pinit-xxx function by yourself.
  5. Open an issue if you have any questions you can't solve.

Outline

Reference

pin

Pinning a pin in text, the pin is supposed to be unique in one page.

#let pin(name) = { .. }

Arguments:

pinit

Query positions of pins in the same page, then call the callback function callback.

#let pinit(callback: none, ..pins) = { .. }

Arguments:

absolute-place

Place content at a specific location on the page relative to the top left corner of the page, regardless of margins, current containers, etc.

This function comes from typst-drafting.

#let absolute-place(
  dx: 0em,
  dy: 0em,
  body,
) = { .. }

Arguments:

pinit-place

Place content at a specific location on the page relative to the pin.

#let pinit-place(
  dx: 0pt,
  dy: 0pt,
  pin-name,
  body,
) = { .. }

Arguments:

pinit-rect

Draw a rectangular shape on the page containing all pins with optional extended width and height.

#let pinit-rect(
  dx: 0em,
  dy: -1em,
  extended-width: 0em,
  extended-height: 1.4em,
  pin1,
  pin2,
  pin3,  // Optional
  ..pinX,
  ..args,
) = { .. }

Arguments:

pinit-highlight

Highlight a specific area on the page with a filled color and optional radius and stroke. It is just a simply styled pinit-rect.

#let pinit-highlight(
  fill: rgb(255, 0, 0, 20),
  radius: 5pt,
  stroke: 0pt,
  dx: 0em,
  dy: -1em,
  extended-width: 0em,
  extended-height: 1.4em,
  pin1,
  pin2,
  pin3,  // Optional
  ..pinX,
  ...args,
) = { .. }

Arguments:

pinit-line

Draw a line on the page between two specified pins with an optional stroke.

#let pinit-line(
  stroke: 1pt,
  start-dx: 0pt,
  start-dy: 0pt,
  end-dx: 0pt,
  end-dy: 0pt,
  start,
  end,
) = { ... }

Arguments:

pinit-line-to

Draw an line from a specified pin to a point on the page with optional settings.

#let pinit-line-to(
  stroke: 1pt,
  pin-dx: 5pt,
  pin-dy: 5pt,
  body-dx: 5pt,
  body-dy: 5pt,
  offset-dx: 35pt,
  offset-dy: 35pt,
  pin-name,
  body,
) = { ... }

Arguments:

pinit-arrow

Draw an arrow between two specified pins with optional settings.

#let pinit-arrow(
  start-dx: 0pt,
  start-dy: 0pt,
  end-dx: 0pt,
  end-dy: 0pt,
  start,
  end,
  ..args,
) = { ... }

Arguments:

pinit-double-arrow

Draw an double arrow between two specified pins with optional settings.

#let pinit-double-arrow(
  start-dx: 0pt,
  start-dy: 0pt,
  end-dx: 0pt,
  end-dy: 0pt,
  start,
  end,
  ..args,
) = { ... }

Arguments:

pinit-point-to

Draw an arrow from a specified pin to a point on the page with optional settings.

#let pinit-point-to(
  pin-dx: 5pt,
  pin-dy: 5pt,
  body-dx: 5pt,
  body-dy: 5pt,
  offset-dx: 35pt,
  offset-dy: 35pt,
  double: false,
  pin-name,
  body,
  ..args,
) = { ... }

Arguments:

pinit-point-from

Draw an arrow from a point on the page to a specified pin with optional settings.

#let pinit-point-from(
  pin-dx: 5pt,
  pin-dy: 5pt,
  body-dx: 5pt,
  body-dy: 5pt,
  offset-dx: 35pt,
  offset-dy: 35pt,
  double: false,
  pin-name,
  body,
  ..args,
) = { ... }

Arguments:

simple-arrow

Draw a simple arrow on the page with optional settings, implemented by polygon.

#let simple-arrow(
  fill: black,
  stroke: 0pt,
  start: (0pt, 0pt),
  end: (30pt, 0pt),
  thickness: 2pt,
  arrow-width: 4,
  arrow-height: 4,
  inset: 0.5,
  tail: (),
) = { ... }

Arguments:

double-arrow

Draw a double arrow on the page with optional settings, implemented by polygon.

#let double-arrow(
  fill: black,
  stroke: 0pt,
  start: (0pt, 0pt),
  end: (30pt, 0pt),
  thickness: 2pt,
  arrow-width: 4,
  arrow-height: 4,
  inset: 0.5,
  tail: (),
) = { ... }

Arguments:

pinit-fletcher-edge

Draw a connecting line or arc in an fletcher arrow diagram.

#let pinit-fletcher-edge(
  fletcher,
  start,
  end: none,
  start-dx: 0pt,
  start-dy: 0pt,
  end-dx: 0pt,
  end-dy: 0pt,
  width-scale: 100%,
  height-scale: 100%,
  default-width: 30pt,
  default-height: 30pt,
    ..args,
) = { ... }

Arguments:

Changelog

0.2.2

0.2.1

0.2.0

0.1.4

0.1.3

0.1.2

0.1.1

0.1.0

Acknowledgements

License

This project is licensed under the MIT License.