How to receive a user supplied path in a package function?

Hi everyone,

I finally decided to try the package the system with my home cooked templates.

One of the templates takes the path to an image as an argument. My problem is that all paths seems to be relative to the package folder rather than the .typ file to compile.

Is it possible to :

  • Supply a path relative to the path of the file to compile, or failing that,
  • Supply an absolute path ?

Many thanks.

Packages cannot access project files, unless the package is installed inside of the project
If you want to pass an image to your template, you will have to read it in your project and pass the image object to the template

#let img = image("my_nice_image.png")
#show: my-template.with(img)

Works like a charm thank you very much!

Hey @Peio, welcome to the forum! I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category

For future posts, make sure your title is a question you’d ask to a friend about Typst. :wink:

In Typst 0.15.0 the path type was added. It is resolved relative to where it is called (e.g. user code) and can also be passed to packages, allowing them to read the file.

Your package could therefore take a path for the image. Or for convenience it could support both path and content (assuming the content represents an image(...)), and then check type(...) to determine how to handle it:

let img-type = type(img)
if img-type == content {
  // show as-is, assuming it is already an image
  img
} else if img-type == path {
  image(img)
} else {
  panic("unsupported img type: " + str(img-type))
}

Users can then either provide a path(...) and your package decides how to display the image, or for more advanced use cases users can directly provide an image(...) and customize it as they like.