Is there an an equivalent to the `\graphicspath{path}` command from LaTeX in Typst?

In LaTeX I can set the command \graphicspath{path/} such that I then only have to write \includegraphics{name} instead of \includegraphics{path/name}.

Is there a way to do this in typst with the image command?

I did think about show rules (like #show image: im => { image("mypath/"+im.path)} which did not work), but I could find a good one.

First, I’ll link this discussion because there is some relevant stuff in there:

This gives us some limitations, but also opportunities. Let’s say all your images are found in path/. Then you can create a file path/images.typ and define the following function there:

#let _image = image

#let image(file, ..args) = _image(file, ..args)

a call to this (not the builtin) image function will automatically treat the file path relative to the path/images.typ file!

What I usually do has a similar result, but I actually try to avoid to even use image paths across the whole project. Instead I define helper functions for my assets:

#let logo = image.with("logo.svg")
#let banner = image.with("banner.jpg")

Then I call these like assets.logo(height: 2cm). For the kinds of images I use in my documents that feels more useful to me, but it’s definitely an opinionated solution.

2 Likes

That is indeed very useful, thanks! :)

Your method to define helper-functions only makes sense if you have not so many images and they appear multiple times and less sense if you have many images which all appear only once, doesn’t it? (Or are there some advantages, which I don’t realize?)

Absolutely, that’s what I meant with “it’s definitely an opinionated solution”: it works well for my use cases, not for others. If you have logos or other assets that you reuse a lot, it’s a good tool to have; if you have images that you each include once, it’s pretty useless.

1 Like

I think you missed the part that adds the path prefix:

#let _image = image

#let image(file, ..args) = _image("path/" + file, ..args)

Edit: As SillyFreak points out below, the code in this post does not apply to the situation he describes. However, it does apply if you put it in your main .typ file since then the path from the file to the photos begins outside of “path/” and therefore it needs to be added.

no, since the images.typ file is inside path/, the filename is already relative to that directory :slight_smile:

Ahhh, got it. I missed that part even though it was in the quote! :person_facepalming:

1 Like