Skip to content

Commit 356c950

Browse files
committed
fix(typst): leave unnumbered proofs out of the frame counter and escape frame titles
1 parent 4b569ea commit 356c950

3 files changed

Lines changed: 66 additions & 43 deletions

File tree

‎src/resources/filters/customnodes/proof.lua‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,23 @@ local proof_theme_colors = {
6666
proof = "gray", remark = "orange", solution = "teal"
6767
}
6868

69+
-- A proof is numbered only when its identifier is a crossref id (`prf-`,
70+
-- `rem-` or `sol-`).
71+
local function is_numbered_proof(identifier)
72+
local ref = identifier and refType(identifier)
73+
return ref ~= nil and crossref.categories.by_ref_type[ref] ~= nil
74+
end
75+
6976
local function ensure_typst_proofs(proof_env)
7077
local appearance = _quarto.modules.theorems.ensure_appearance_imports()
7178
local proof_info = proof_types[proof_env]
7279
local title = envTitle(proof_env, proof_info.title)
73-
local render_code
74-
75-
if appearance == "fancy" then
76-
render_code = " render: fancy-box.with(\n" ..
77-
" get-border-color: get-tertiary-border-color,\n" ..
78-
" get-body-color: get-tertiary-body-color,\n" ..
79-
" get-symbol: loc => none,\n" ..
80-
" ),\n"
81-
elseif appearance == "clouds" then
82-
local color = proof_theme_colors[proof_env] or "gray"
83-
render_code = " render: clouds-render.with(fill: " .. color .. ".lighten(85%)),\n"
84-
elseif appearance == "rainbow" then
85-
local color = proof_theme_colors[proof_env] or "gray"
86-
render_code = " render: rainbow-render.with(fill: " .. color .. ".darken(20%)),\n"
87-
else
88-
_quarto.modules.theorems.ensure_simple_render("simple-proof-render", false)
89-
render_code = " render: simple-proof-render,\n"
90-
end
80+
local render_code = _quarto.modules.theorems.render_code(appearance, {
81+
color_scheme = "tertiary",
82+
color = proof_theme_colors[proof_env] or "gray",
83+
simple_render = "simple-proof-render",
84+
italic_body = false,
85+
})
9186

9287
_quarto.modules.theorems.ensure_frame(proof_env, title, render_code)
9388
end
@@ -179,7 +174,19 @@ end, function(proof_tbl)
179174
end
180175
ensure_typst_proofs(proof.env)
181176
local preamble = pandoc.Plain({pandoc.RawInline("typst", "#" .. proof.env .. "(")})
177+
local numbered = is_numbered_proof(proof_tbl.identifier)
178+
if not numbered then
179+
-- theorion steps the counter and prints the number only for
180+
-- `number: auto`, and its prefix would keep a space before the empty
181+
-- number, so an unnumbered block pins both
182+
preamble.content:insert(pandoc.RawInline("typst",
183+
'number: "", get-prefix: (..args) => ' ..
184+
_quarto.modules.theorems.typst_string(envTitle(proof.env, proof.title))))
185+
end
182186
if name and #name > 0 then
187+
if not numbered then
188+
preamble.content:insert(pandoc.RawInline("typst", ", "))
189+
end
183190
preamble.content:insert(pandoc.RawInline("typst", 'title: ['))
184191
tappend(preamble.content, name)
185192
preamble.content:insert(pandoc.RawInline("typst", ']'))

‎src/resources/filters/customnodes/theorem.lua‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,18 @@ local function ensure_typst_theorems(reftype)
103103
local appearance = _quarto.modules.theorems.ensure_appearance_imports()
104104
local theorem_type = theorem_types[reftype]
105105
local title = titleString(reftype, theorem_type.title)
106-
local render_code
107-
108-
if appearance == "fancy" then
109-
local color_scheme = "secondary"
110-
if theorem_type.style == "definition" then
111-
color_scheme = "primary"
112-
elseif reftype == "prp" then
113-
color_scheme = "tertiary"
114-
end
115-
render_code = " render: fancy-box.with(\n" ..
116-
" get-border-color: get-" .. color_scheme .. "-border-color,\n" ..
117-
" get-body-color: get-" .. color_scheme .. "-body-color,\n" ..
118-
" get-symbol: loc => none,\n" ..
119-
" ),\n"
120-
elseif appearance == "clouds" then
121-
local color = theme_colors[reftype] or "gray"
122-
render_code = " render: clouds-render.with(fill: " .. color .. ".lighten(85%)),\n"
123-
elseif appearance == "rainbow" then
124-
local color = theme_colors[reftype] or "gray"
125-
render_code = " render: rainbow-render.with(fill: " .. color .. ".darken(20%)),\n"
126-
else
127-
_quarto.modules.theorems.ensure_simple_render("simple-theorem-render", true)
128-
render_code = " render: simple-theorem-render,\n"
106+
local color_scheme = "secondary"
107+
if theorem_type.style == "definition" then
108+
color_scheme = "primary"
109+
elseif reftype == "prp" then
110+
color_scheme = "tertiary"
129111
end
112+
local render_code = _quarto.modules.theorems.render_code(appearance, {
113+
color_scheme = color_scheme,
114+
color = theme_colors[reftype] or "gray",
115+
simple_render = "simple-theorem-render",
116+
italic_body = true,
117+
})
130118

131119
_quarto.modules.theorems.ensure_frame(theorem_type.env, title, render_code)
132120
end

‎src/resources/filters/modules/theorems.lua‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- theorems.lua
22
-- Shared Typst theorem/proof setup logic using theorion
3-
-- Copyright (C) 2020-2022 Posit Software, PBC
3+
-- Copyright (C) 2026 Posit Software, PBC
44

55
local typst_theorem_appearance_imported = false
66
local typst_theorem_like_frames = {}
@@ -75,6 +75,32 @@ local function ensure_simple_render(render_name, italic_body)
7575
]])
7676
end
7777

78+
-- Writes text as a Typst string literal, so that titles from the language
79+
-- files reach the page as written instead of being read as Typst markup.
80+
local function typst_string(text)
81+
return '"' .. text:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
82+
end
83+
84+
-- Returns the `render:` argument of make-frame for the theorem appearance.
85+
-- opts.color_scheme: fancy color set (primary, secondary or tertiary)
86+
-- opts.color: clouds and rainbow fill color
87+
-- opts.simple_render, opts.italic_body: name and body style of the simple renderer
88+
local function render_code(appearance, opts)
89+
if appearance == "fancy" then
90+
return " render: fancy-box.with(\n" ..
91+
" get-border-color: get-" .. opts.color_scheme .. "-border-color,\n" ..
92+
" get-body-color: get-" .. opts.color_scheme .. "-body-color,\n" ..
93+
" get-symbol: loc => none,\n" ..
94+
" ),\n"
95+
elseif appearance == "clouds" then
96+
return " render: clouds-render.with(fill: " .. opts.color .. ".lighten(85%)),\n"
97+
elseif appearance == "rainbow" then
98+
return " render: rainbow-render.with(fill: " .. opts.color .. ".darken(20%)),\n"
99+
end
100+
ensure_simple_render(opts.simple_render, opts.italic_body)
101+
return " render: " .. opts.simple_render .. ",\n"
102+
end
103+
78104
local function ensure_frame(env_name, title, render_code)
79105
if typst_theorem_like_frames[env_name] then
80106
return false
@@ -84,7 +110,7 @@ local function ensure_frame(env_name, title, render_code)
84110
quarto.doc.include_text("in-header", "#let (" .. env_name .. "-counter, " .. env_name .. "-box, " ..
85111
env_name .. ", show-" .. env_name .. ") = make-frame(\n" ..
86112
" \"" .. env_name .. "\",\n" ..
87-
" text(weight: \"bold\")[" .. title .. "],\n" ..
113+
" text(weight: \"bold\", " .. typst_string(title) .. "),\n" ..
88114
" inherited-levels: theorem-inherited-levels,\n" ..
89115
" numbering: theorem-numbering,\n" ..
90116
render_code ..
@@ -98,4 +124,6 @@ return {
98124
ensure_appearance_imports = ensure_appearance_imports,
99125
ensure_simple_render = ensure_simple_render,
100126
ensure_frame = ensure_frame,
127+
render_code = render_code,
128+
typst_string = typst_string,
101129
}

0 commit comments

Comments
 (0)