Skip to content

Commit 066badf

Browse files
Fix #260, work around inconsistent treatment of math syntax between typst and texoutput formats (#262)
1 parent a1bf209 commit 066badf

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Fixed
11+
12+
- Fix missing display maths output in Typst. Works around inconsistency in handling of markdown math syntax between Quarto output formats [#262]
13+
1014
## [v0.13.1] - 2025-02-18
1115

1216
### Fixed
@@ -378,3 +382,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
378382
[#253]: https://github.com/PumasAI/QuartoNotebookRunner.jl/issues/253
379383
[#255]: https://github.com/PumasAI/QuartoNotebookRunner.jl/issues/255
380384
[#257]: https://github.com/PumasAI/QuartoNotebookRunner.jl/issues/257
385+
[#262]: https://github.com/PumasAI/QuartoNotebookRunner.jl/issues/262

src/QuartoNotebookWorker/ext/QuartoNotebookWorkerLaTeXStringsExt.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ struct LaTeXStringWrapper <: QNW.WrapperType
99
value::LS.LaTeXString
1010
end
1111

12-
Base.show(io::IO, ::MIME"text/markdown", s::LaTeXStringWrapper) = print(io, s.value)
12+
function Base.show(io::IO, ::MIME"text/markdown", s::LaTeXStringWrapper)
13+
qnr = get(io, :QuartoNotebookRunner, nothing)
14+
isnothing(qnr) && error("No QuartoNotebookRunner found in IO context")
15+
to_format = QNW.rget(qnr.options, ("format", "pandoc", "to"), nothing)
16+
# Workaround for some weirdness in the treatment of rendering for math in
17+
# typst output which fails to render display maths if it doesn't have to
18+
# correct leading and trailing dollars. PDF(tex) output requires that
19+
# `\begin` blocks don't have `$$` wrappers, but typst requires them.
20+
wrap = to_format == "typst" && !qnr.inline && !startswith(s.value, "\$")
21+
wrap && print(io, "\$\$")
22+
show(io, MIME("text/latex"), s.value)
23+
wrap && print(io, "\$\$")
24+
end
1325
Base.showable(::MIME"text/markdown", ::LaTeXStringWrapper) = true
1426

1527
end

src/QuartoNotebookWorker/src/render.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ function io_capture(f; cell_options, kws...)
261261
end
262262

263263
# passing our module removes Main.Notebook noise when printing types etc.
264-
function with_context(io::IO, cell_options = Dict{String,Any}())
265-
return IOContext(io, _io_context(cell_options)...)
264+
function with_context(io::IO, cell_options = Dict{String,Any}(), inline = false)
265+
return IOContext(io, _io_context(cell_options, inline)...)
266266
end
267267

268-
function _io_context(cell_options = Dict{String,Any}())
268+
function _io_context(cell_options = Dict{String,Any}(), inline = false)
269269
return [
270270
:module => NotebookState.notebook_module(),
271271
:limit => true,
@@ -279,7 +279,8 @@ function _io_context(cell_options = Dict{String,Any}())
279279
#
280280
# TODO: perhaps preprocess the metadata provided here rather
281281
# than just passing it through as-is.
282-
:QuartoNotebookRunner => (; cell_options, options = NotebookState.OPTIONS[]),
282+
:QuartoNotebookRunner =>
283+
(; cell_options, options = NotebookState.OPTIONS[], inline),
283284
]
284285
end
285286

@@ -394,12 +395,12 @@ function render_mimetypes(
394395
try
395396
if inline && mime == "text/plain"
396397
Base.@invokelatest __print_barrier__(
397-
with_context(buffer, cell_options),
398+
with_context(buffer, cell_options, inline),
398399
value,
399400
)
400401
else
401402
Base.@invokelatest __show_barrier__(
402-
with_context(buffer, cell_options),
403+
with_context(buffer, cell_options, inline),
403404
mime,
404405
value,
405406
)

test/testsets/mimetypes.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,20 @@ test_example(joinpath(@__DIR__, "../examples/mimetypes.qmd")) do json
2929
cell = cells[14]
3030
@test !isempty(cell["outputs"][1]["data"]["text/plain"])
3131
@test !isempty(cell["outputs"][1]["data"]["text/latex"])
32-
@test !isempty(cell["outputs"][1]["data"]["text/markdown"])
32+
33+
md = cell["outputs"][1]["data"]["text/markdown"]
34+
@test !isempty(md)
35+
@test !startswith(md, "\$\$")
36+
@test !endswith(md, "\$\$")
37+
end
38+
39+
test_example(joinpath(@__DIR__, "../examples/mimetypes.qmd"), to_format("typst")) do json
40+
cells = json["cells"]
41+
42+
cell = cells[14]
43+
@test !isempty(cell["outputs"][1]["data"]["text/plain"])
44+
@test !isempty(cell["outputs"][1]["data"]["text/latex"])
45+
md = cell["outputs"][1]["data"]["text/markdown"]
46+
@test startswith(md, "\$\$")
47+
@test endswith(md, "\$\$")
3348
end

test/utilities/prelude.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@ if !@isdefined(SCHEMA)
1212
open(JSON3.read, joinpath(@__DIR__, "../schema/nbformat.v4.schema.json")),
1313
)
1414

15-
function test_example(f, each)
15+
function test_example(f, each, options = Dict{String,Any}())
1616
examples = joinpath(@__DIR__, "../examples")
1717
name = relpath(each, pwd())
18-
@info "Testing $name"
18+
if isempty(options)
19+
@info "Testing $name"
20+
else
21+
@info "Testing $name (extra options)" options
22+
end
1923
@testset "$(name)" begin
2024
server = QuartoNotebookRunner.Server()
2125
buffer = IOBuffer()
22-
QuartoNotebookRunner.run!(server, each; output = buffer, showprogress = false)
26+
QuartoNotebookRunner.run!(
27+
server,
28+
each;
29+
options = options,
30+
output = buffer,
31+
showprogress = false,
32+
)
2333
seekstart(buffer)
2434
json = JSON3.read(buffer, Any)
2535

@@ -70,4 +80,5 @@ if !@isdefined(SCHEMA)
7080
end
7181
GC.gc()
7282
end
83+
to_format(format) = Dict{String,Any}("format" => Dict("pandoc" => Dict("to" => format)))
7384
end

0 commit comments

Comments
 (0)