Skip to content

Commit 4c014a5

Browse files
authored
fix: render labs(tag:) above the title (#58)
2 parents 15047ee + 113c195 commit 4c014a5

5 files changed

Lines changed: 67 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- feat: `labs()` fields default to `auto`; pass `none` to suppress an axis or legend title and reclaim the space it reserved. (#12)
1313
- feat: `element-blank()` on a text surface (axis, plot, or legend title) collapses the space the text would reserve. (#12)
1414
- feat: `width`/`height` accept `auto` to fill the available space of a bounded container. (#10)
15+
- fix: `labs(tag:)` draws the figure tag above the title on a standalone plot, styled by the `plot-tag` theme element, instead of being ignored. (#58)
1516
- fix: `labs(alt:)` fills in the figure's accessibility alt text when `plot(alt:)` is unset, instead of being stored and ignored. (#57)
1617
- fix: `geom-qq()`/`geom-qq-line()` honour the `distribution` argument (`uniform`/`exponential`) instead of always plotting against the normal reference. (#56)
1718
- fix: the `font` set on `element-text`/`element-typst`/`element-geom` is applied to every text surface and to the text-drawing geoms, inheriting the base `text` font when unset. (#54)

docs/examples/gallery.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
description: 'Title, subtitle, caption, and axis labels set in a single [`labs`](../reference/labs/labs.qmd) call.'
2424
alt: "Scatter and line chart of count against month showing a rising trend, framed by a title, subtitle, caption, and axis labels."
2525

26+
- slug: labs-tag
27+
section: basics
28+
title: "Figure tag"
29+
description: 'A figure tag drawn above the title via [`labs`](../reference/labs/labs.qmd) `tag`, styled by the `plot-tag` theme element.'
30+
alt: "Scatter plot of highway fuel economy against engine displacement, marked with a bold 'A' tag above the title and subtitle."
31+
2632
- slug: factor-inline
2733
section: basics
2834
title: "Inline factor coercion"

examples/labs-tag.typ

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// A figure tag drawn above the title, styled by the plot-tag theme element.
2+
3+
#import "../lib.typ": *
4+
5+
#set page(width: auto, height: auto, margin: 0cm)
6+
7+
#plot(
8+
data: mpg,
9+
mapping: aes(x: "displ", y: "hwy"),
10+
layers: (geom-point(size: 2.5pt, alpha: 0.75),),
11+
labs: labs(
12+
tag: "A",
13+
title: "Engine Displacement Versus Highway Fuel Economy",
14+
subtitle: "One panel of a larger figure",
15+
x: "Displacement (L)",
16+
y: "Highway mpg",
17+
),
18+
theme: theme-minimal(),
19+
width: 12cm,
20+
height: 9cm,
21+
)

src/render.typ

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,7 @@
30693069
- outer-pad.right
30703070
- inner-inset.right
30713071
)
3072+
let tag = _text-style(theme, "plot-tag")
30723073
let title = _text-style(theme, "plot-title")
30733074
let subtitle = _text-style(theme, "plot-subtitle")
30743075
let caption = _text-style(theme, "plot-caption")
@@ -3093,6 +3094,9 @@
30933094
),
30943095
)
30953096
} else { none }
3097+
let tag-block = if labs != none {
3098+
_chrome-block(labs.tag, tag, left, weight: tag.weight)
3099+
} else { none }
30963100
let title-block = if labs != none {
30973101
_chrome-block(labs.title, title, left, weight: title.weight)
30983102
} else { none }
@@ -3109,17 +3113,22 @@
31093113
_text-margin-cm(style, side, 0.6em) * 1cm
31103114
}
31113115

3116+
// Bottom margin of the lowest header block sets the gap above the canvas.
31123117
let above-canvas = if subtitle-block != none {
31133118
subtitle
3114-
} else if title-block != none { title } else { none }
3119+
} else if title-block != none {
3120+
title
3121+
} else if tag-block != none { tag } else { none }
31153122

31163123
(
31173124
plot-bg: plot-bg,
31183125
outer-pad: outer-pad,
31193126
inner-inset: inner-inset,
3127+
tag-block: tag-block,
31203128
title-block: title-block,
31213129
subtitle-block: subtitle-block,
31223130
caption-block: caption-block,
3131+
tag-gap: _gap-length(tag, "bottom"),
31233132
inter-title-gap: _gap-length(title, "bottom"),
31243133
above-gap: if above-canvas != none {
31253134
_gap-length(above-canvas, "bottom")
@@ -3134,14 +3143,26 @@
31343143
#let _decorate-extents(parts) = {
31353144
let _h(b) = if b == none { 0cm } else { measure(b).height }
31363145
let top = parts.outer-pad.top + parts.inner-inset.top
3137-
if parts.title-block != none { top += _h(parts.title-block) }
3146+
let headers = 0
3147+
if parts.tag-block != none {
3148+
top += _h(parts.tag-block)
3149+
headers += 1
3150+
}
3151+
if parts.title-block != none {
3152+
if headers > 0 { top += parts.tag-gap }
3153+
top += _h(parts.title-block)
3154+
headers += 1
3155+
}
31383156
if parts.subtitle-block != none {
3139-
if parts.title-block != none { top += parts.inter-title-gap }
3157+
if headers > 0 {
3158+
top += if parts.title-block != none { parts.inter-title-gap } else {
3159+
parts.tag-gap
3160+
}
3161+
}
31403162
top += _h(parts.subtitle-block)
3163+
headers += 1
31413164
}
3142-
if parts.title-block != none or parts.subtitle-block != none {
3143-
top += parts.above-gap
3144-
}
3165+
if headers > 0 { top += parts.above-gap }
31453166
let bottom = parts.outer-pad.bottom + parts.inner-inset.bottom
31463167
if parts.caption-block != none {
31473168
bottom += parts.caption-gap + _h(parts.caption-block)
@@ -3181,19 +3202,26 @@
31813202
),
31823203
)
31833204
} else { content }
3205+
let tag-block = parts.tag-block
31843206
let title-block = parts.title-block
31853207
let subtitle-block = parts.subtitle-block
31863208
let caption-block = parts.caption-block
31873209

31883210
let items = ()
3189-
if title-block != none { items.push(title-block) }
3211+
if tag-block != none { items.push(tag-block) }
3212+
if title-block != none {
3213+
if items.len() > 0 { items.push(v(parts.tag-gap)) }
3214+
items.push(title-block)
3215+
}
31903216
if subtitle-block != none {
3191-
if items.len() > 0 { items.push(v(parts.inter-title-gap)) }
3217+
if items.len() > 0 {
3218+
items.push(v(if title-block != none { parts.inter-title-gap } else {
3219+
parts.tag-gap
3220+
}))
3221+
}
31923222
items.push(subtitle-block)
31933223
}
3194-
if title-block != none or subtitle-block != none {
3195-
items.push(v(parts.above-gap))
3196-
}
3224+
if items.len() > 0 { items.push(v(parts.above-gap)) }
31973225
items.push(canvas)
31983226
if caption-block != none {
31993227
items.push(v(parts.caption-gap))
71.2 KB
Loading

0 commit comments

Comments
 (0)