Skip to content

Commit cd20343

Browse files
committed
Added the option to show latex in juopyter notebookks, and some extra code to make sure graphics is the not incorrectky mangled.
1 parent c29251b commit cd20343

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/display.jl

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const graphics = (W"Graphics", W"Graphics3D", W"Legended")
1+
const graphics = (W"Graphics",W"GeoGraphics", W"Graphics3D", W"Legended")
22

33
Base.Multimedia.showable(::MIME"image/svg+xml", w::MathLink.WExpr) = in(w.head, graphics)
44
Base.show(io::IO, ::MIME"image/svg+xml", w::MathLink.WExpr) = print(io, weval(W"ExportString"(w, "SVG")))
@@ -8,3 +8,73 @@ function Base.show(io::IO, ::MIME"image/png", w::MathLink.WExpr)
88
x = weval(W"ExportByteArray"(w, "PNG"))
99
write(io, x.args[1].args)
1010
end
11+
12+
13+
export HasGraphicsHead, HasRecursiveGraphicsHead, W2Tex
14+
15+
16+
#### Code to produce LaTex strings
17+
W2Tex(x::WTypes) = weval(W`ToString@TeXForm[#]&`(x))
18+
19+
#### Allow latex string to be shown when supported. Relevant for the jupyter notebook.
20+
21+
HasRecursiveGraphicsHead(w::MathLink.WSymbol) = false
22+
function HasRecursiveGraphicsHead(w::MathLink.WExpr)
23+
if HasGraphicsHead(w)
24+
return true
25+
end
26+
for arg in w.args
27+
if typeof(arg) == MathLink.WExpr
28+
##Only check for MathLink Expressions
29+
if HasRecursiveGraphicsHead(arg)
30+
return true
31+
end
32+
end
33+
end
34+
return false
35+
end
36+
37+
function HeadsEndsWith(HeadString,Target)
38+
###Check if name ends with $Target
39+
if length(HeadString) >= length(Target)
40+
return HeadString[end-(length(Target)-1):end] == Target
41+
end
42+
return false
43+
end
44+
45+
46+
const graphics_heads = (W"Graphics", W"Graphics3D", W"Legended")
47+
48+
###Check if an expression has a grapics head
49+
HasGraphicsHead(w::MathLink.WSymbol) = false
50+
function HasGraphicsHead(w::MathLink.WExpr)
51+
HeadString = w.head.name
52+
###Check for graphics related names not based on ending on Plot* or Chart*
53+
if in(w.head, graphics)
54+
return true
55+
end
56+
57+
HeadsEndsWith(HeadString,"Plot") && return true
58+
HeadsEndsWith(HeadString,"Chart") && return true
59+
HeadsEndsWith(HeadString,"Plot3D") && return true
60+
HeadsEndsWith(HeadString,"Chart3D") && return true
61+
return false
62+
end
63+
64+
import Base.show
65+
Base.show(io,::MIME"text/latex",x::MathLink.WSymbol) = print(io,"\$"*W2Tex(x)*"\$")
66+
67+
import Base.Multimedia.showable
68+
function Base.Multimedia.showable(::MIME"text/latex", w::MathLink.WExpr)
69+
if HasRecursiveGraphicsHead(w)
70+
return false
71+
else
72+
return true
73+
end
74+
end
75+
function Base.show(io,::MIME"text/latex",x::MathLink.WExpr)
76+
print(io,"\$"*W2Tex(x)*"\$")
77+
end
78+
79+
80+

test/runtests.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,43 @@ end
182182
P14 * Mat
183183
P14 * Mat* P14
184184
end
185+
186+
187+
188+
189+
@testset "Find Graphiscs" begin
190+
@test !HasGraphicsHead(W"a")
191+
@test HasGraphicsHead(W`Plot[x,{x,0,1}]`)
192+
@test HasGraphicsHead(W`ListPlot[x,{x,0,1}]`)
193+
@test HasGraphicsHead(W`ListLinePlot3D[x,{x,0,1}]`)
194+
@test HasGraphicsHead(W`Plot3D[x,{x,0,1}]`)
195+
@test !HasGraphicsHead(W"a"+W"b")
196+
@test HasGraphicsHead(weval(W`Plot[x,{x,0,1}]`))
197+
@test !HasGraphicsHead(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`)
198+
@test !HasGraphicsHead(weval(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`))
199+
200+
@test !HasRecursiveGraphicsHead(W`{2,a+v,{4+d}}`)
201+
@test HasRecursiveGraphicsHead(W`Plot[x,{x,0,1}]`)
202+
@test HasRecursiveGraphicsHead(W`{2,Plot[x^2,{x,0,1}]}`)
203+
@test HasRecursiveGraphicsHead(W`{a+b,Plot[x^2,{x,0,1}]}`)
204+
@test HasRecursiveGraphicsHead(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`)
205+
@test HasRecursiveGraphicsHead(W`{1,{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}}`)
206+
@test HasRecursiveGraphicsHead(weval(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`))
207+
end
208+
209+
@testset "W2Tex - LaTex conversion" begin
210+
@test W2Tex(W`(a+b)^(b+x)`) == "(a+b)^{b+x}"
211+
@test W2Tex(W`a`) == "a"
212+
@test W2Tex(W`ab`) == "\\text{ab}"
213+
@test W2Tex(W`ab*cd`) == "\\text{ab} \\text{cd}"
214+
215+
###Testing that MIME form exists for the text/latex option of show.
216+
io = IOBuffer();
217+
show(IOContext(io, :limit => true, :displaysize => (10, 10)),
218+
"text/plain",W"a")
219+
@test String(take!(io)) == "W\"a\""
220+
show(IOContext(io, :limit => true, :displaysize => (10, 10)),
221+
"text/plain",W"a"+W"b")
222+
@test String(take!(io)) == "W\"Plus\"(W\"a\", W\"b\")"
223+
end
224+

0 commit comments

Comments
 (0)