Hello,
I wrote a wrapper function to have figure captions on the side instead of under the picture, where I’m re-implementing the image function in a show rule as to treat them separately. This function is defined in a separate template file.
Everything works fine, except for captions with tags in them: a regular figure function works,
#figure(
image("path/to/img.png", width: 45%),
caption: [Plot of @eq.],
)
while the custom function does not:
#scfigure(
image("path/to/img.png", width: 45%),
caption: [Plot of @eq.],
)
where I get the error: cannot reference context.
Here is a minimal working example of the function I wrote:
#let scfigure(
caption-ratio: auto,
caption-align: bottom + left,
gutter: 1.6em,
..args,
) = context {
let text-width = page.width - (page.margin.inside + page.margin.outside)
let half-gutter = gutter.to-absolute() / 2
show figure.caption: it => { align(caption-align)[#it] }
show figure.where(kind: image): it => {
let img-size = (width: 50% - half-gutter, height: auto)
if "width" in it.body.fields() {
img-size.width = it.body.width.ratio + (it.body.width.length.to-absolute() / text-width * 100%) - half-gutter
}
if "height" in it.body.fields() {
img-size.height = it.body.height
}
let caption-width = img-size.width.ratio - half-gutter
if img-size.width.ratio > 50% {
caption-width = 100% - img-size.width.ratio - half-gutter
}
if caption-ratio != auto {
caption-width = caption-width.ratio * caption-ratio - half-gutter
}
let columns-arrangement = (img-size.width, caption-width)
align(center, block(breakable: false, above: 2.4em, below: 2.4em)[
#grid(
columns: columns-arrangement,
column-gutter: gutter,
image(
it.body.source.trim("../"), // img folder is relative to this file now!!
width: 100%,
height: img-size.height,
),
it.caption,
)
])
}
figure(..args)
}
Is there a way to display tags correctly?
