Skip to content

Commit 3fb398e

Browse files
claude: Support margin citations with citeproc enabled
When citeproc is enabled, the native Typst #cite(form: "full") call fails because citeproc removes the bibliography. This adds a pre- processing pass that runs pandoc.utils.citeproc() to build a lookup table of formatted bibliography entries, then inserts those directly into margin notes instead of using #cite() calls. Also adds comprehensive tests for citeproc margin citations with locators, prefix/suffix, author suppression, and multiple citations. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8544e83 commit 3fb398e

4 files changed

Lines changed: 237 additions & 23 deletions

File tree

‎src/resources/filters/quarto-post/typst.lua‎

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
-- FIXME Ideally this would go directly on init.lua, but
77
-- the module path set up doesn't appear to be working there.
8-
8+
99
local typst = require("modules/typst")
1010
_quarto.format.typst = typst
1111

@@ -19,6 +19,10 @@ local function formatShiftParam(shift)
1919
end
2020
end
2121

22+
-- Lookup table for citeproc-rendered bibliography entries
23+
-- Maps citation_id -> Inlines (formatted bibliography entry)
24+
local citeprocBibliography = {}
25+
2226
function render_typst()
2327
if not _quarto.format.isTypstOutput() then
2428
return {}
@@ -27,6 +31,40 @@ function render_typst()
2731
local number_depth
2832

2933
return {
34+
-- Pass 0: Pre-process citeproc to build bibliography lookup table
35+
-- This must run before the Cite handler so entries are available
36+
{
37+
Pandoc = function(doc)
38+
-- Only build lookup if using citeproc AND margin citations
39+
if marginCitations() and quarto.doc.cite_method() == 'citeproc' then
40+
-- Run citeproc on a copy to get formatted bibliography
41+
local processed = pandoc.utils.citeproc(doc)
42+
43+
-- Find the refs div and extract entries
44+
processed:walk({
45+
Div = function(div)
46+
-- Each bibliography entry has id like "ref-citationkey"
47+
local match = div.identifier:match("^ref%-(.+)$")
48+
if match then
49+
-- Extract the formatted content (typically a Para inside the div)
50+
-- Flatten to Inlines for use in margin notes
51+
local inlines = pandoc.Inlines({})
52+
for _, block in ipairs(div.content) do
53+
if block.t == "Para" or block.t == "Plain" then
54+
if #inlines > 0 then
55+
inlines:insert(pandoc.Space())
56+
end
57+
inlines:extend(block.content)
58+
end
59+
end
60+
citeprocBibliography[match] = inlines
61+
end
62+
end
63+
})
64+
end
65+
return nil -- Don't modify the document
66+
end
67+
},
3068
{
3169
Meta = function(m)
3270
-- This should be a number, but we must represent it as a string,
@@ -120,22 +158,36 @@ function render_typst()
120158
if marginCitations() then
121159
noteHasColumns() -- Activate margin layout
122160

123-
-- Build margin note content with full citations (no locators - those stay inline)
124-
local margin_entries = pandoc.List({})
125-
for _, c in ipairs(cite.citations) do
126-
margin_entries:insert("#cite(<" .. c.id .. ">, form: \"full\")")
127-
end
161+
local use_citeproc = quarto.doc.cite_method() == 'citeproc'
128162

129-
-- Keep original Cite element (Pandoc renders it as Typst citation with locator inline)
130-
-- Append margin note with full bibliographic entries only
163+
-- Keep original Cite element (Pandoc renders it with locator inline)
164+
-- Append margin note with full bibliographic entries
131165
local result = pandoc.Inlines({})
132-
result:insert(cite) -- Original Cite element - Pandoc converts to @key[locator]
166+
result:insert(cite)
167+
168+
-- Open margin note
133169
result:insert(pandoc.RawInline("typst",
134-
"#note(alignment: \"baseline\", shift: auto, counter: none)[" ..
135-
"#set text(size: 0.85em)\n" ..
136-
table.concat(margin_entries, "\n") ..
137-
"]"
138-
))
170+
"#note(alignment: \"baseline\", shift: auto, counter: none)[#set text(size: 0.85em)\n"))
171+
172+
-- Add bibliography entries for each citation
173+
local first = true
174+
for _, c in ipairs(cite.citations) do
175+
if not first then
176+
result:insert(pandoc.RawInline("typst", "\n"))
177+
end
178+
first = false
179+
180+
if use_citeproc and citeprocBibliography[c.id] then
181+
-- Use pre-rendered citeproc content (Pandoc will convert to Typst)
182+
result:extend(citeprocBibliography[c.id])
183+
else
184+
-- Use native Typst citation
185+
result:insert(pandoc.RawInline("typst", "#cite(<" .. c.id .. ">, form: \"full\")"))
186+
end
187+
end
188+
189+
-- Close margin note
190+
result:insert(pandoc.RawInline("typst", "]"))
139191
return result
140192
end
141193
end,
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: "The Universe (Which Others Call the Library)"
3+
subtitle: "A Meditation on Infinite Texts and Their Margin Notes"
4+
author: "A Hexagon Dweller"
5+
papersize: us-letter
6+
citation-location: margin
7+
bibliography: borges-refs.bib
8+
citeproc: true
9+
format:
10+
html: default
11+
typst:
12+
keep-typ: true
13+
_quarto:
14+
tests:
15+
typst:
16+
ensureTypstFileRegexMatches:
17+
# With citeproc, margin notes contain pre-rendered bibliography text
18+
- ['#note\(', 'Borges.*1941', 'Bifurcan']
19+
- ['#cite\(<[^>]+>, form: "full"\)'] # Should NOT have native Typst cite calls
20+
ensurePdfRegexMatches:
21+
- - 'Universe'
22+
- 'Library'
23+
- 'hexagon'
24+
- 'Vindication'
25+
- 'Borges'
26+
# Locators appear inline (citeproc drops "p." prefix)
27+
- '1941, 51'
28+
- 'ch\. 1'
29+
# Author-suppressed shows year + locator
30+
- '1936, 12'
31+
# Suffix appears inline (may be line-wrapped)
32+
- 'throughout'
33+
- []
34+
ensurePdfTextPositions:
35+
- # Citations appear in margin (rightOf body text)
36+
- subject: "Bifurcan"
37+
relation: rightOf
38+
object: "ventilation shaft"
39+
- subject: "Ficciones"
40+
relation: rightOf
41+
object: "architecture"
42+
# Sequential citations are stacked vertically in margin
43+
- subject: "Ficciones"
44+
relation: below
45+
object: "Bifurcan"
46+
- subject: "Universalbibliothek"
47+
relation: below
48+
object: "Ficciones"
49+
# "Siamese Press" is unique to Quain citation in margin
50+
- subject: "Siamese Press"
51+
relation: below
52+
object: "Universalbibliothek"
53+
- []
54+
noErrors: default
55+
---
56+
57+
# Prolegomena
58+
59+
The universe (which others call the Library) is composed of an indefinite, perhaps infinite, number of hexagonal galleries [@borges1941library, p. 51]. In the center of each gallery is a ventilation shaft, bounded by a low railing. From any hexagon one can see the floors above and below---one after another, endlessly.
60+
61+
This essay concerns itself not with the Library's architecture, which has been adequately described elsewhere [see @borges1944ficciones, ch. 1], but with a peculiar feature of its books: the margin notes.
62+
63+
# On Marginalia in the Total Library
64+
65+
If the Library contains all possible books of 410 pages, it must contain books with margins. And if it contains books with margins, it must contain margin notes. The German philosopher @kurd1933hexagonal did not consider this recursive complication in his calculations.
66+
67+
Consider: for each book in the Library, there exists another book containing the first book's text plus commentary in the margins. And for each of these annotated volumes, there exists a meta-commentary. The recursion is, like the Library itself, infinite.
68+
69+
## Herbert Quain's Marginal Method
70+
71+
The Anglo-Irish writer Herbert Quain [-@quain1936labyrinth, pp. 12-15] reportedly composed his novels in reverse, writing the margin notes first. His masterwork, *April March*, can be read in nine different ways, each determined by which margin notes the reader chooses to follow.
72+
73+
## The Margins of the Vindication
74+
75+
The seekers of the Vindication [cf. @vindication1738, p. 7 and throughout] have long debated: if the Vindication exists, do its margins contain additional vindications? The First Librarian [@librarians1638first] argued that the true Vindication would have no margins at all---that the text itself would fill every available space, justifying even the whiteness of the paper.
76+
77+
# Literary Descendants
78+
79+
Borges's influence on subsequent literature is well documented [@casares1940morel; @eco1980name; @calvino1979winter]. Bioy Casares, Borges's collaborator and friend, created machines that record and replay reality. Eco built a library that murders. Calvino wrote about a reader unable to finish any book because each book keeps becoming another book.
80+
81+
What these authors share is an understanding that texts are not stable objects but rather provisional arrangements of signs that point to other signs. This is the lesson of the Library: meaning is always deferred, always in the margins.
82+
83+
# Conclusion
84+
85+
In the Library, I search for the book that will explain my life. Somewhere among the twenty-five symbols lies my vindication. Until then, I write these margin notes, adding to the infinite commentary.
86+
87+
As @tzinacana1521jaguar discovered in his prison, sometimes the most important text is not written in books at all but inscribed on the skin of a jaguar, fourteen words that make their speaker omnipotent. But Tzinacana chose not to speak them.
88+
89+
Perhaps the Library's true meaning is in the margins we choose not to read, the books we pass by in our endless search for the one book that will make sense of all the others. Perhaps meaning itself is marginal.

‎tests/docs/smoke-all/typst/margin-layout/citation-margin-elaborate.qmd‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,71 @@ _quarto:
1717
- ['#note\(', 'form: "full"']
1818
- []
1919
ensurePdfRegexMatches:
20-
- ['Universe', 'Library', 'hexagon', 'Vindication', 'Borges']
20+
- - 'Universe'
21+
- 'Library'
22+
- 'hexagon'
23+
- 'Vindication'
24+
- 'Borges'
25+
# Locators appear inline
26+
- '\[1, p\. 51\]'
27+
- '\[2, ch\. 1\]'
28+
# Author-suppressed shows just year
29+
- '\(1936\)'
30+
# Suffix appears inline
31+
- 'and throughout'
2132
- []
2233
ensurePdfTextPositions:
23-
- # Citation in margin - "bifurcan" is unique to borges1941library booktitle
34+
- # Citations appear in margin (rightOf body text)
2435
- subject: "bifurcan"
2536
relation: rightOf
2637
object: "ventilation shaft"
38+
- subject: "Ficciones"
39+
relation: rightOf
40+
object: "architecture"
41+
# Sequential citations are stacked vertically in margin
42+
- subject: "Ficciones"
43+
relation: below
44+
object: "bifurcan"
45+
- subject: "Universalbib"
46+
relation: below
47+
object: "Ficciones"
48+
- subject: "Siamese Press"
49+
relation: below
50+
object: "Universalbib"
2751
- []
2852
noErrors: default
2953
---
3054

3155
# Prolegomena
3256

33-
The universe (which others call the Library) is composed of an indefinite, perhaps infinite, number of hexagonal galleries [@borges1941library]. In the center of each gallery is a ventilation shaft, bounded by a low railing. From any hexagon one can see the floors above and below---one after another, endlessly.
57+
The universe (which others call the Library) is composed of an indefinite, perhaps infinite, number of hexagonal galleries [@borges1941library, p. 51]. In the center of each gallery is a ventilation shaft, bounded by a low railing. From any hexagon one can see the floors above and below---one after another, endlessly.
3458

35-
This essay concerns itself not with the Library's architecture, which has been adequately described elsewhere [@borges1944ficciones], but with a peculiar feature of its books: the margin notes.
59+
This essay concerns itself not with the Library's architecture, which has been adequately described elsewhere [see @borges1944ficciones, ch. 1], but with a peculiar feature of its books: the margin notes.
3660

3761
# On Marginalia in the Total Library
3862

39-
If the Library contains all possible books of 410 pages, it must contain books with margins. And if it contains books with margins, it must contain margin notes. The German philosopher Kurd Lasswitz [@kurd1933hexagonal] did not consider this recursive complication in his calculations.
63+
If the Library contains all possible books of 410 pages, it must contain books with margins. And if it contains books with margins, it must contain margin notes. The German philosopher @kurd1933hexagonal did not consider this recursive complication in his calculations.
4064

4165
Consider: for each book in the Library, there exists another book containing the first book's text plus commentary in the margins. And for each of these annotated volumes, there exists a meta-commentary. The recursion is, like the Library itself, infinite.
4266

4367
## Herbert Quain's Marginal Method
4468

45-
The Anglo-Irish writer Herbert Quain [@quain1936labyrinth] reportedly composed his novels in reverse, writing the margin notes first. His masterwork, *April March*, can be read in nine different ways, each determined by which margin notes the reader chooses to follow.
69+
The Anglo-Irish writer Herbert Quain [-@quain1936labyrinth, pp. 12-15] reportedly composed his novels in reverse, writing the margin notes first. His masterwork, *April March*, can be read in nine different ways, each determined by which margin notes the reader chooses to follow.
4670

4771
## The Margins of the Vindication
4872

49-
The seekers of the Vindication [@vindication1738] have long debated: if the Vindication exists, do its margins contain additional vindications? The First Librarian [@librarians1638first] argued that the true Vindication would have no margins at all---that the text itself would fill every available space, justifying even the whiteness of the paper.
73+
The seekers of the Vindication [cf. @vindication1738, p. 7 and throughout] have long debated: if the Vindication exists, do its margins contain additional vindications? The First Librarian [@librarians1638first] argued that the true Vindication would have no margins at all---that the text itself would fill every available space, justifying even the whiteness of the paper.
5074

5175
# Literary Descendants
5276

53-
Borges's influence on subsequent literature is well documented. Bioy Casares [@casares1940morel], Borges's collaborator and friend, created machines that record and replay reality. Eco [@eco1980name] built a library that murders. Calvino [@calvino1979winter] wrote about a reader unable to finish any book because each book keeps becoming another book.
77+
Borges's influence on subsequent literature is well documented [@casares1940morel; @eco1980name; @calvino1979winter]. Bioy Casares, Borges's collaborator and friend, created machines that record and replay reality. Eco built a library that murders. Calvino wrote about a reader unable to finish any book because each book keeps becoming another book.
5478

5579
What these authors share is an understanding that texts are not stable objects but rather provisional arrangements of signs that point to other signs. This is the lesson of the Library: meaning is always deferred, always in the margins.
5680

5781
# Conclusion
5882

5983
In the Library, I search for the book that will explain my life. Somewhere among the twenty-five symbols lies my vindication. Until then, I write these margin notes, adding to the infinite commentary.
6084

61-
As Tzinacana discovered in his prison [@tzinacana1521jaguar], sometimes the most important text is not written in books at all but inscribed on the skin of a jaguar, fourteen words that make their speaker omnipotent. But Tzinacana chose not to speak them.
85+
As @tzinacana1521jaguar discovered in his prison, sometimes the most important text is not written in books at all but inscribed on the skin of a jaguar, fourteen words that make their speaker omnipotent. But Tzinacana chose not to speak them.
6286

6387
Perhaps the Library's true meaning is in the margins we choose not to read, the books we pass by in our endless search for the one book that will make sense of all the others. Perhaps meaning itself is marginal.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: "Margin Citation with Locators (Citeproc)"
3+
papersize: us-letter
4+
citation-location: margin
5+
bibliography: borges-refs.bib
6+
citeproc: true
7+
format:
8+
typst:
9+
keep-typ: true
10+
_quarto:
11+
tests:
12+
typst:
13+
ensureTypstFileRegexMatches:
14+
# With citeproc, margin notes contain pre-rendered bibliography text (not #cite calls)
15+
- ['#note\(', 'Borges.*1941', 'Bifurcan']
16+
- ['#cite\(<[^>]+>, form: "full"\)'] # Should NOT have native Typst cite calls
17+
ensurePdfRegexMatches:
18+
- - 'LOCATOR-MARKER'
19+
- 'Borges'
20+
- 'Quain'
21+
# Citeproc drops "p." prefix - locators appear INLINE after author-date
22+
# Use year + locator to avoid line-wrap issues with author names
23+
- '1941, 42\)' # borges1941library, p. 42
24+
- '1944, 100' # borges1944ficciones, pp. 100-115
25+
- '1936, ch\. 3\)' # quain1936labyrinth, ch. 3
26+
- '1980, 200' # eco1980name, p. 200, fig. 5
27+
- []
28+
ensurePdfTextPositions:
29+
- # Full citation in margin - "Bifurcan" is from borges1941library booktitle
30+
- subject: "Bifurcan"
31+
relation: rightOf
32+
object: "LOCATOR-MARKER"
33+
# Locator appears inline (left of margin content) - citeproc drops "p." prefix
34+
- subject: "42"
35+
relation: leftOf
36+
object: "Bifurcan"
37+
- []
38+
noErrors: default
39+
---
40+
41+
# Testing Citation Locators in Margins
42+
43+
LOCATOR-MARKER: The Library was first described with specific page references [@borges1941library, p. 42].
44+
45+
The expanded edition contains more details [@borges1944ficciones, pp. 100-115].
46+
47+
Chapter-specific references also work [@quain1936labyrinth, ch. 3].
48+
49+
Multiple locator types: see [@eco1980name, p. 200, fig. 5] for the library map.

0 commit comments

Comments
 (0)