-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
utils.jl
239 lines (206 loc) · 7.07 KB
/
utils.jl
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
add_brackets(ex::Expr, vars) = postwalk(x -> x in vars ? "\\left[ $(convert_subscript(x)) \\right]" : x, ex)
add_brackets(arr::AbstractArray, vars) = [add_brackets(element, vars) for element in arr]
add_brackets(s::Any, vars) = s
default_packages(s) = vcat(["amssymb", "amsmath", "unicode-math"], occursin("\\ce{", s) ? ["mhchem"] : [])
function _writetex(s::LaTeXString;
name=tempname(),
command="\\Large",
documentclass=("standalone", "varwidth=true"),
packages=default_packages(s),
preamble=""
)
doc = """
\\documentclass$(_packagename(documentclass))
""" * prod(map(p -> "\\usepackage$(_packagename(p))\n", packages)) * preamble * """
\\begin{document}
{
$command
$s
}
\\end{document}
"""
doc = replace(doc, "\\begin{align}"=>"\\[\n\\begin{aligned}")
doc = replace(doc, "\\end{align}"=>"\\end{aligned}\n\\]")
doc = replace(doc, "\\require{mhchem}\n"=>"")
texfile = name * ".tex"
write(texfile, doc)
texfile
end
function _openfile(name; ext="pdf")
if Sys.iswindows()
run(`cmd /c "start $name.$ext"`, wait=false)
elseif Sys.islinux()
run(`xdg-open $name.$ext`, wait=false)
elseif Sys.isapple()
run(`open $name.$ext`, wait=false)
elseif Sys.isbsd()
run(`xdg-open $name.$ext`, wait=false)
end
return nothing
end
"""
render(::LaTeXString[, ::MIME"mime"]; debug=false, name=tempname(), command="\\Large", callshow=true, open=true)
Display a standalone document with the given input. Supported MIME-type strings are
"application/pdf" (default), "application/x-dvi", "image/png" and "image/svg".
"""
render(s::LaTeXString; kwargs...) = render(s, best_displayable(); kwargs...)
function best_displayable()
priority_list = [
MIME("juliavscode/html"),
MIME("application/pdf"),
MIME("application/x-dvi"),
MIME("image/svg"),
MIME("image/png"),
]
for mime_type in priority_list
displayable(mime_type) && return mime_type
end
return MIME("application/pdf")
end
function html_wrap(s::LaTeXString; scale=1.1, kwargs...)
import_str = """
<script>
MathJax = {
tex: {
displayMath: [['\$', '\$'], ['\\\\[', '\\\\]']]
},
chtml: {
scale: $scale
},
svg: {
scale: $scale
}
};
</script>
<script src="/js/mathjax/tex-chtml.js" id="MathJax-script" async></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
"""
return import_str * s.s
end
# render(s::LaTeXString, mime::MIME"juliavscode/html"; kwargs...) = render(stdout, mime; kwargs...)
render(s::LaTeXString, mime::MIME"juliavscode/html"; kwargs...) = display(mime, html_wrap(s; kwargs...))
function render(s::LaTeXString, ::MIME"application/pdf";
debug=false,
name=tempname(),
open=true,
kw...
)
_writetex(s; name=name, kw...)
cd(dirname(name)) do
cmd = `lualatex --interaction=batchmode $name.tex`
debug || (cmd = pipeline(cmd, devnull))
run(cmd)
end
# `display(MIME("application/pdf")` is generally not defined even though
# `displayable(MIME("application/pdf")` returns `true`.
#
# if callshow && displayable(MIME("application/pdf"))
# display(MIME("application/pdf"), read("$name.pdf"))
# end
if open
_openfile(name; ext="pdf")
end
return nothing
end
function render(s::LaTeXString, ::MIME"application/x-dvi";
debug=false,
name=tempname(),
open=true,
kw...
)
_writetex(s; name=name, kw...)
cd(dirname(name)) do
cmd = `dvilualatex --interaction=batchmode $name.tex`
debug || (cmd = pipeline(cmd, devnull))
run(cmd)
end
# `display(MIME("application/x-dvi")` is generally not defined even though
# `displayable(MIME("application/x-dvi")` returns `true`.
#
# if callshow && displayable(MIME("application/x-dvi"))
# display(MIME("application/x-dvi"), read("$name.dvi"))
# end
if open
_openfile(name; ext="dvi")
end
return nothing
end
function render(s::LaTeXString, ::MIME"image/png";
debug=false,
convert = :gs,
name=tempname(),
callshow=true,
open=true,
dpi=DEFAULT_DPI[],
kw...
)
# tex -> dvi -> png is notoriously bad for fonts (not OTF support), see e.g. tex.stackexchange.com/q/537281
# prefer tex -> pdf -> png instead
if convert === :gs
mime = MIME("application/pdf")
cmd = `gs -sDEVICE=pngalpha -dTextAlphaBits=4 -r$dpi -o $name.png $name.pdf`
elseif convert === :dvipng
mime = MIME("application/x-dvi")
deb = debug ? [] : ["-q"]
cmd = `dvipng $(deb...) -bg Transparent -D $dpi -T tight $name.dvi -o $name.png`
else
error("$convert program not understood")
end
render(s, mime; debug=debug, name=name, open=false, kw...)
debug || (cmd = pipeline(cmd, devnull))
run(cmd)
if callshow && displayable(MIME("image/png"))
display(MIME("image/png"), read("$name.png"))
elseif open
_openfile(name; ext="png")
end
return nothing
end
function render(s::LaTeXString, ::MIME"image/svg";
debug=false,
convert = :dvisvgm,
name=tempname(),
callshow=true,
open=true,
kw...
)
if convert === :dvisvgm
verb = debug ? 7 : 0
cmd = `dvisvgm --no-fonts --pdf -v $verb $name.pdf -o $name.svg`
elseif convert === :pdf2svg
cmd = `pdf2svg $name.pdf $name.svg`
else
error("$convert program not understood")
end
render(s, MIME("application/pdf"); debug=debug, name=name, open=false, kw...)
debug || (cmd = pipeline(cmd, devnull))
run(cmd)
# `displayable(MIME("image/svg"))` returns `true` even in a textual
# context (e.g., in the REPL), but `display(MIME("image/svg+xml"), ...)`
# is the one normally defined.
if callshow && displayable(MIME("image/svg"))
display(MIME("image/svg+xml"), read("$name.svg"))
elseif open
_openfile(name; ext="svg")
end
return nothing
end
safereduce(f, args) = length(args) == 1 ? f(args[1]) : reduce(f, args)
function expr_to_array(ex)
ex.head === :typed_vcat && (ex = Expr(:vcat, ex.args[2:end]...))
ex.head === :typed_hcat && (ex = Expr(:hcat, ex.args[2:end]...))
ex.head === :ref && (ex = Expr(:vect, ex.args[2:end]...))
## If it is a matrix
if Meta.isexpr(ex.args[1], :row)
return eval(ex.head)(map(y -> permutedims(y.args), ex.args)...)
else
if ex.head == :hcat
return safereduce(hcat, ex.args)
elseif ex.head in (:vcat, :vect)
return safereduce(vcat, ex.args)
end
end
end
_packagename(x::AbstractString) = "{$x}"
_packagename(x::Tuple) = "[$(join(x[2:end], ", "))]{$(first(x))}"