Skip to content

Commit 3f10174

Browse files
claude: Add Typst margin layout support
Implement Tufte-style margin layout for Typst format using the marginalia package. Features: - Margin notes, figures, tables, and captions via .column-margin class - Margin citations with citeproc support (citation-location: margin) - Full-width content via .column-screen and .column-page classes - Configurable margin-geometry following marginalia's proportions - Support for subfigures and panel layouts in margins - Typst show rule for automatic margin footnote placement Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3e8e0cb commit 3f10174

111 files changed

Lines changed: 6047 additions & 64 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ end)
964964
_quarto.ast.add_renderer("FloatRefTarget", function(_)
965965
return _quarto.format.isTypstOutput()
966966
end, function(float)
967+
-- Get crossref info first (needed for both margin and regular figures)
967968
local ref = ref_type_from_float(float)
968969
local info = crossref.categories.by_ref_type[ref]
969970
if info == nil then
@@ -974,6 +975,107 @@ end, function(float)
974975
end
975976
local kind = "quarto-float-" .. ref
976977
local supplement = titleString(ref, info.name)
978+
979+
-- Check if this is a margin figure (has .column-margin or .aside class)
980+
-- Skip margin handling for subfloats - the parent handles margin placement
981+
if hasMarginColumn(float) and not float.parent_id then
982+
local content = quarto.utils.as_blocks(float.content or {})
983+
984+
-- Get optional attributes
985+
local shift = float.attributes and float.attributes["shift"] or "auto"
986+
local alignment = float.attributes and float.attributes["alignment"] or "baseline"
987+
local dy = float.attributes and float.attributes["dy"] or "0pt"
988+
989+
-- Get caption location (tables default to top, figures to bottom)
990+
local caption_location = cap_location(float)
991+
if caption_location ~= "top" and caption_location ~= "bottom" then
992+
caption_location = "bottom"
993+
end
994+
995+
-- Check for subfloats - need to use quarto_super wrapped in note()
996+
if float.has_subfloats then
997+
-- Wrap quarto_super in note() for margin placement with proper subfloat numbering
998+
local result = pandoc.Blocks({})
999+
result:insert(pandoc.RawBlock("typst",
1000+
'#note(counter: none, alignment: "' .. alignment .. '", dy: ' .. dy ..
1001+
', shift: ' .. _quarto.format.typst.format_shift_param(shift) .. ')['))
1002+
result:insert(_quarto.format.typst.function_call("quarto_super", {
1003+
{"kind", kind},
1004+
{"caption", _quarto.modules.typst.as_typst_content(float.caption_long)},
1005+
{"label", pandoc.RawInline("typst", "<" .. float.identifier .. ">")},
1006+
{"position", pandoc.RawInline("typst", caption_location)},
1007+
{"supplement", supplement},
1008+
{"subrefnumbering", "1a"},
1009+
{"subcapnumbering", "(a)"},
1010+
_quarto.modules.typst.as_typst_content(content)
1011+
}, false))
1012+
result:insert(pandoc.RawBlock("typst", ']\n\n'))
1013+
return result
1014+
end
1015+
1016+
-- No subfloats - use notefigure for margin placement
1017+
return make_typst_margin_figure {
1018+
content = content,
1019+
caption = float.caption_long,
1020+
caption_location = caption_location,
1021+
identifier = float.identifier,
1022+
shift = shift,
1023+
alignment = alignment,
1024+
dy = dy,
1025+
kind = kind,
1026+
supplement = supplement
1027+
}
1028+
end
1029+
1030+
-- Check for margin caption (figure in main column, caption in margin)
1031+
if hasMarginCaption(float) then
1032+
local content = quarto.utils.as_blocks(float.content or {})
1033+
-- Margin captions align with top of content (consistent with HTML visual behavior)
1034+
local alignment = "top"
1035+
1036+
return make_typst_margin_caption_figure {
1037+
content = content,
1038+
caption = float.caption_long,
1039+
identifier = float.identifier,
1040+
kind = kind,
1041+
supplement = supplement,
1042+
alignment = alignment,
1043+
}
1044+
end
1045+
1046+
-- Check for full-width classes (column-page-right, column-page, column-screen, etc.)
1047+
-- Note: For cell outputs, columns.lua wraps the cell-output-display div in wideblock.
1048+
-- For fenced divs, the FloatRefTarget has the class and needs to wrap itself.
1049+
local wideblock_side = getWideblockSide(float.classes)
1050+
if wideblock_side then
1051+
local content = quarto.utils.as_blocks(float.content or {})
1052+
-- Listings should not be centered inside the figure
1053+
if ref == "lst" then
1054+
content:insert(1, pandoc.RawBlock("typst", "#set align(left)"))
1055+
end
1056+
local caption_location = cap_location(float)
1057+
if caption_location ~= "top" and caption_location ~= "bottom" then
1058+
caption_location = "bottom"
1059+
end
1060+
1061+
-- Render standard figure first
1062+
local figure_blocks = make_typst_figure {
1063+
content = content,
1064+
caption_location = caption_location,
1065+
caption = float.caption_long,
1066+
kind = kind,
1067+
supplement = supplement,
1068+
numbering = info.numbering,
1069+
identifier = float.identifier
1070+
}
1071+
1072+
-- Wrap in wideblock
1073+
return make_typst_wideblock {
1074+
content = figure_blocks,
1075+
side = wideblock_side,
1076+
}
1077+
end
1078+
9771079
-- FIXME: custom numbering doesn't work yet
9781080
-- local numbering = ""
9791081
-- if float.parent_id then
@@ -984,8 +1086,12 @@ end, function(float)
9841086
local content = quarto.utils.as_blocks(float.content or {})
9851087
local caption_location = cap_location(float)
9861088

987-
if (caption_location ~= "top" and caption_location ~= "bottom") then
988-
-- warn this is not supported and default to bottom
1089+
if caption_location == "margin" then
1090+
-- Margin captions should have been caught by hasMarginCaption check above.
1091+
-- If we reach here, margin-layout may not be active. Fall back to bottom.
1092+
caption_location = "bottom"
1093+
elseif caption_location ~= "top" and caption_location ~= "bottom" then
1094+
-- Unknown caption location, warn and default to bottom
9891095
warn("Typst does not support this caption location: " .. caption_location .. ". Defaulting to bottom for '" .. float.identifier .. "'.")
9901096
caption_location = "bottom"
9911097
end

‎src/resources/filters/layout/columns-preprocess.lua‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
-- columns-preprocess.lua
22
-- Copyright (C) 2021-2022 Posit Software, PBC
33

4-
function columns_preprocess()
4+
function columns_preprocess()
55
return {
66
FloatRefTarget = function(float)
77
if float.parent_id ~= nil then
88
return nil
99
end
10+
-- Check for margin figure placement (.column-margin or .aside class)
11+
if hasMarginColumn(float) then
12+
noteHasColumns()
13+
end
14+
-- Check for full-width classes (column-page-*, column-screen-*)
15+
if getWideblockSide(float.classes) then
16+
noteHasColumns()
17+
end
18+
-- Check for margin caption class (added directly to element)
19+
if hasMarginCaption(float) then
20+
noteHasColumns()
21+
end
1022
local location = cap_location(float)
1123
if location == 'margin' then
1224
float.classes:insert('margin-caption')

‎src/resources/filters/layout/columns.lua‎

Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,109 @@ local function def_columns()
7474

7575
-- for html output that isn't reveal...
7676
if _quarto.format.isHtmlOutput() and not _quarto.format.isHtmlSlideOutput() then
77-
77+
7878
-- For HTML output, note that any div marked an aside should
79-
-- be marked a column-margin element (so that it is processed
80-
-- by post processors).
79+
-- be marked a column-margin element (so that it is processed
80+
-- by post processors).
8181
-- For example: https://github.com/quarto-dev/quarto-cli/issues/2701
8282
if el.classes and tcontains(el.classes, 'aside') then
8383
noteHasColumns()
84-
el.classes = el.classes:filter(function(attr)
84+
el.classes = el.classes:filter(function(attr)
8585
return attr ~= "aside"
8686
end)
8787
tappend(el.classes, {'column-margin', "margin-aside"})
8888
return el
8989
end
90-
90+
91+
elseif _quarto.format.isTypstOutput() then
92+
-- For Typst output, detect column classes to trigger margin layout setup
93+
-- Actual margin note rendering is handled in quarto-post/typst.lua
94+
if hasMarginColumn(el) or hasColumnClasses(el) then
95+
noteHasColumns()
96+
end
97+
-- Convert aside class to column-margin for consistency
98+
if el.classes and tcontains(el.classes, 'aside') then
99+
el.classes = el.classes:filter(function(attr)
100+
return attr ~= "aside"
101+
end)
102+
tappend(el.classes, {'column-margin'})
103+
return el
104+
end
105+
-- Handle full-width classes with wideblock
106+
local side, clz = getWideblockSide(el.classes)
107+
if side then
108+
noteHasColumns() -- Ensure margin layout is activated for wideblock
109+
el.classes = el.classes:filter(function(c) return c ~= clz end)
110+
return make_typst_wideblock {
111+
content = el.content,
112+
side = side,
113+
}
114+
end
115+
-- Handle intermediate width classes (body-outset, page-inset, screen-inset)
116+
local intermediateInfo, intermediateClz = getIntermediateWidthClass(el.classes)
117+
if intermediateInfo then
118+
noteHasColumns() -- Ensure margin layout is activated
119+
el.classes = el.classes:filter(function(c) return c ~= intermediateClz end)
120+
return make_typst_intermediate_width {
121+
content = el.content,
122+
func = intermediateInfo.func,
123+
side = intermediateInfo.side,
124+
}
125+
end
126+
-- Handle margin figures/tables: propagate .column-margin class to FloatRefTarget
127+
-- so they render with notefigure() instead of being wrapped in #note()
128+
if hasMarginColumn(el) then
129+
local floatRefTargets = el.content:filter(function(contentEl)
130+
return is_custom_node(contentEl, "FloatRefTarget")
131+
end)
132+
if #floatRefTargets > 0 then
133+
-- Propagate margin class and attributes to each FloatRefTarget and return unwrapped
134+
local result = pandoc.Blocks({})
135+
for _, contentEl in ipairs(el.content) do
136+
if is_custom_node(contentEl, "FloatRefTarget") then
137+
local custom = _quarto.ast.resolve_custom_data(contentEl)
138+
if custom ~= nil then
139+
-- Add column-margin class to the float
140+
if custom.classes == nil then
141+
custom.classes = pandoc.List({'column-margin'})
142+
else
143+
custom.classes:insert('column-margin')
144+
end
145+
-- Propagate margin-related attributes (shift, alignment, dy)
146+
if el.attributes then
147+
if custom.attributes == nil then
148+
custom.attributes = {}
149+
end
150+
if el.attributes.shift then
151+
custom.attributes.shift = el.attributes.shift
152+
end
153+
if el.attributes.alignment then
154+
custom.attributes.alignment = el.attributes.alignment
155+
end
156+
if el.attributes.dy then
157+
custom.attributes.dy = el.attributes.dy
158+
end
159+
end
160+
result:insert(contentEl)
161+
end
162+
else
163+
-- Non-float content stays wrapped in margin note
164+
local inner_div = pandoc.Div({contentEl}, pandoc.Attr("", {'column-margin'}))
165+
result:insert(inner_div)
166+
end
167+
end
168+
return result
169+
else
170+
-- For cell-output-display divs with column-margin, the parent FloatRefTarget
171+
-- will handle margin placement. Strip the class to prevent quarto-post from
172+
-- wrapping it in #note() (which would cause double-wrapping with notefigure).
173+
if el.classes:includes("cell-output-display") then
174+
removeColumnClasses(el)
175+
return el
176+
end
177+
end
178+
end
179+
91180
elseif el.identifier and el.identifier:find("^lst%-") then
92181
-- for listings, fetch column classes from sourceCode element
93182
-- and move to the appropriate spot (e.g. caption, container div)
@@ -175,7 +264,7 @@ local function def_columns()
175264
-- It also means that divs that want to be both a figure* and a table*
176265
-- will never work and we won't get the column-* treatment for
177266
-- everything, just for the table.
178-
el.classes = el.classes:filter(function(clz)
267+
el.classes = el.classes:filter(function(clz)
179268
return not isStarEnv(clz)
180269
end)
181270
end
@@ -185,14 +274,14 @@ local function def_columns()
185274
-- the general purpose `sidenote` processing from capturing this
186275
-- element (since floats know how to deal with margin positioning)
187276
local custom = _quarto.ast.resolve_custom_data(contentEl)
188-
if custom ~= nil then
277+
if custom ~= nil then
189278
floatRefTarget = true
190279
removeColumnClasses(el)
191280
add_column_classes_and_attributes(columnClasses, columnAttributes, custom)
192281
end
193-
end
282+
end
194283
end
195-
284+
196285
if not figOrTable and not floatRefTarget then
197286
processOtherContent(el.content)
198287
end
@@ -267,17 +356,29 @@ local function def_columns()
267356

268357
Span = function(el)
269358
-- a span that should be placed in the margin
270-
if _quarto.format.isLatexOutput() and hasMarginColumn(el) then
359+
if _quarto.format.isLatexOutput() and hasMarginColumn(el) then
271360
noteHasColumns()
272361
tprepend(el.content, {latexBeginSidenote(false)})
273362
tappend(el.content, {latexEndSidenote(el, false)})
274363
removeColumnClasses(el)
275364
return el
276-
else
365+
elseif _quarto.format.isTypstOutput() and hasMarginColumn(el) then
366+
-- For Typst, detect margin spans to trigger margin layout setup
367+
-- Actual margin note rendering is handled in quarto-post/typst.lua
368+
noteHasColumns()
369+
-- Convert aside class to column-margin for consistency
370+
if el.classes and tcontains(el.classes, 'aside') then
371+
el.classes = el.classes:filter(function(attr)
372+
return attr ~= "aside"
373+
end)
374+
tappend(el.classes, {'column-margin'})
375+
end
376+
return el
377+
else
277378
-- convert the aside class to a column-margin class
278379
if el.classes and tcontains(el.classes, 'aside') then
279380
noteHasColumns()
280-
el.classes = el.classes:filter(function(attr)
381+
el.classes = el.classes:filter(function(attr)
281382
return attr ~= "aside"
282383
end)
283384
tappend(el.classes, {'column-margin', 'margin-aside'})

0 commit comments

Comments
 (0)