-
Notifications
You must be signed in to change notification settings - Fork 1
/
latex-math.lua
130 lines (121 loc) · 4.14 KB
/
latex-math.lua
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
130
local system = require("pandoc.system")
function appendDepthToSVGFile(depth, svgPath)
local f = io.open(svgPath, "a")
f:write(string.format("<!-- depth=%spt -->\n", depth))
f:close()
end
function NewLatexRender()
return {
preamble = [[
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[T2A,T1]{fontenc}
\usepackage{colordvi}
\usepackage[active,tightpage]{preview}
]],
latexClass = "article",
fontEncoding = "utf8",
fontSize = 12,
bgcolor = "#FFFFFF",
latexPath = "latex",
dvisvgmPath = "dvisvgm"
}
end
function html2rgb(color)
return ''
end
function wrapFormula(lr, latexFormula)
local bgcolor = lr.bgcolor ~= "#FFFFFF" and string.format("\\background{%s}\n", html2rgb(lr.bgcolor)) or ''
local tex = string.format([[\documentclass[%dpt]{%s}
\usepackage[%s]{inputenc}
%s
\begin{document}
%s
\begin{preview}
%s
\end{preview}
\end{document}
]], lr.fontSize, lr.latexClass, lr.fontEncoding, lr.preamble, bgcolor, latexFormula)
-- io.write(string.format("tex: [[%s]]\n", tex))
return tex
end
function getDepth(out)
local depth = string.match(out, "depth=(%d*%.?%d*)")
return tonumber(depth)
end
function renderLatex(lr, latexFormula)
local latexDocument = wrapFormula(lr, latexFormula)
local currDir = system.get_working_directory()
local svgFileName = pandoc.sha1(latexDocument) .. ".svg"
local svgPath = currDir .. "/" .. svgFileName
local f = io.open(svgPath, "r")
if f ~= nil then
local depth = getDepth(f:read("a"))
f:close()
io.write(string.format("found SVG file=%s with depth=%spt\n", svgPath, depth))
return depth, svgFileName
end
-- SVG file does not exist
local depth = system.with_temporary_directory("latexmath", function(tmpDir)
return system.with_working_directory(tmpDir, function()
io.write(string.format("changed directory to (%s)\n", tmpDir))
local tmpFile = io.open("latexmath.tex", "w")
tmpFile:write(latexDocument)
tmpFile:close()
local out = command(lr, svgPath)
local depth = getDepth(out)
if depth == nil then
io.write(string.format("%s: depth not found\n", svgPath))
return nil
end
io.write(string.format("%s: depth=%spt\n", svgPath, depth))
appendDepthToSVGFile(depth, svgPath)
return depth
end)
end)
return depth, svgFileName
end
function command(lr, svgPath)
pandoc.pipe(lr.latexPath, {"--interaction=nonstopmode", "latexmath.tex"}, '')
-- local out = pandoc.pipe(lr.dvisvgmPath, {"--no-fonts", "-o", svgPath, "latexmath.dvi"}, '')
local f = io.popen(lr.dvisvgmPath .. " --no-fonts -o \"" .. svgPath .. "\" latexmath.dvi 2>&1")
local out = f:read("a")
f:close()
-- io.write(string.format("out: [[%s]]\n", out))
return out
end
function Math(elem)
local latexFormula1
local latexFormula = elem.text
if elem.mathtype == "InlineMath" then
latexFormula1 = string.format("\\(%s\\)", latexFormula)
else
-- DisplayMath
latexFormula1 = string.format("\\[%s\\]", latexFormula)
end
local lr = NewLatexRender()
local depth, svgFileName = renderLatex(lr, latexFormula1)
local attr = {
alt = latexFormula
}
if depth ~= nil then
attr["style"] = string.format("vertical-align:-%spt", depth)
end
-- io.write(string.format("%s\n", dump(attr)))
return pandoc.Image('', svgFileName, '', attr)
end
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k, v in pairs(o) do
if type(k) ~= 'number' then
k = '"' .. k .. '"'
end
s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end