Repository navigation
Expand file tree
/
Copy pathmath-xrefs.lua
More file actions
129 lines (124 loc) · 4.29 KB
/
Copy pathmath-xrefs.lua
File metadata and controls
129 lines (124 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- Resolve cross-references written inside math.
--
-- Quarto resolves @id references in prose, but not inside math, so an
-- annotation such as \text{(@thm-spectral)} in an aligned derivation used to
-- reach the reader as the literal text "@thm-spectral". This filter numbers
-- theorem-type divs and equations in document order, the way Quarto numbers
-- them on a page, and rewrites each @id inside math to its label
-- ("Theorem 59"), linked to the target in HTML output.
local labels = {
thm = "Theorem", lem = "Lemma", cor = "Corollary", prp = "Proposition",
cnj = "Conjecture", def = "Definition", exm = "Example", exr = "Exercise",
rem = "Remark", sol = "Solution", eq = "Equation",
}
local resolved = {}
local counts = {}
-- Targets on other pages, with their labels and URLs, from page-xrefs.lua.
local remote = {}
local function note(id)
local prefix = id and id:match("^(%a+)%-")
if prefix and labels[prefix] and not resolved[id] then
counts[prefix] = (counts[prefix] or 0) + 1
resolved[id] = labels[prefix] .. "~" .. counts[prefix]
end
end
-- Rewrite "@id" inside a TeX string. Inside \text{...} the reference closes
-- the text, emits the label and reopens the text, so that the label is set
-- in math mode where \href works.
local function rewrite(tex)
local html = quarto.doc.is_format("html")
local out, i, depth, text_depths = {}, 1, 0, {}
while i <= #tex do
local c = tex:sub(i, i)
if tex:sub(i, i + 5) == "\\text{" then
table.insert(out, "\\text{")
depth = depth + 1
text_depths[depth] = true
i = i + 6
elseif c == "{" then
depth = depth + 1
text_depths[depth] = false
table.insert(out, c)
i = i + 1
elseif c == "}" then
text_depths[depth] = nil
depth = depth - 1
table.insert(out, c)
i = i + 1
elseif c == "@" then
local id = tex:match("^@(%a+%-[%w%-]*%w)", i)
local prefix = id and id:match("^(%a+)%-")
local is_latex = quarto.doc.is_format("pdf") or quarto.doc.is_format("latex")
local far = id and not resolved[id] and remote[id]
if far then
-- A target on another page of a split chapter (page-xrefs.lua).
local shown = "\\text{" .. far.label .. "}"
if html or is_latex then
local url = is_latex and far.url:gsub("#", "\\#") or far.url
shown = "\\href{" .. url .. "}{" .. shown .. "}"
end
if text_depths[depth] then
table.insert(out, "}" .. shown .. "\\text{")
else
table.insert(out, shown)
end
i = i + 1 + #id
elseif is_latex and prefix and labels[prefix] then
local shown = "\\text{" .. labels[prefix] .. "~\\ref{" .. id .. "}}"
if text_depths[depth] then
table.insert(out, "}" .. shown .. "\\text{")
else
table.insert(out, shown)
end
i = i + 1 + #id
elseif id and resolved[id] then
local shown = "\\text{" .. resolved[id] .. "}"
if html then shown = "\\href{#" .. id .. "}{" .. shown .. "}" end
if text_depths[depth] then
table.insert(out, "}" .. shown .. "\\text{")
else
table.insert(out, shown)
end
i = i + 1 + #id
else
table.insert(out, c)
i = i + 1
end
else
table.insert(out, c)
i = i + 1
end
end
return table.concat(out)
end
return {
-- Pass 1: number the targets in document order. At this stage Quarto
-- holds theorem-type divs as Theorem (or Proof) nodes, and an equation label is still
-- the literal string "{#eq-...}" after its display math.
{
Meta = function(meta)
for id, t in pairs(meta["page-xrefs-math"] or {}) do
remote[id] = {
label = pandoc.utils.stringify(t.label):gsub(utf8.char(160), "~"),
url = pandoc.utils.stringify(t.url),
}
end
end,
Theorem = function(el) note(el.identifier) end,
-- Remarks and solutions are Proof nodes, numbered like theorems.
Proof = function(el) note(el.identifier) end,
Str = function(el)
local id = el.text:match("^{#(eq%-[%w%-]*%w)}$")
if id then note(id) end
end,
},
-- Pass 2: rewrite the references inside math.
{
Math = function(el)
if el.text:find("@", 1, true) then
el.text = rewrite(el.text)
return el
end
end,
},
}