Skip to content

Commit 50c8b29

Browse files
authored
Merge pull request #59 from fremling/master
Allow latex support in Jupyter notebooks
2 parents c29251b + 6da4c9f commit 50c8b29

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ julia> P12 * [W"a" W"b" ; W`a+b` 2] == [ W"b" 2-W"b" ; W"a" W"b"]
132132
true
133133
```
134134

135+
## LateX printing in JuPyter Notebooks
136+
Printing in Juypter notebooks is by defaults done in latex.
137+
This can be turned off with the command `MathLink.set_texOutput(false)`
138+
135139
## Notes
136140

137141
- Mathematica, Wolfram, MathLink are all trademarks of Wolfram Research.

src/display.jl

Lines changed: 87 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,89 @@ 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+
####If the flag for tex-output evaluation does not exist
14+
## create it and set it to true.
15+
if !@isdefined(MLtexOutput)
16+
MLtexOutput=true
17+
end
18+
19+
export set_texOutput
20+
function set_texOutput(x::Bool)
21+
global MLtexOutput
22+
MLtexOutput=x
23+
end
24+
25+
export HasGraphicsHead, HasRecursiveGraphicsHead, W2Tex
26+
27+
28+
#### Code to produce LaTex strings
29+
W2Tex(x::WTypes) = weval(W`ToString@TeXForm[#]&`(x))
30+
31+
#### Allow latex string to be shown when supported. Relevant for the jupyter notebook.
32+
33+
HasRecursiveGraphicsHead(w::MathLink.WSymbol) = false
34+
function HasRecursiveGraphicsHead(w::MathLink.WExpr)
35+
if HasGraphicsHead(w)
36+
return true
37+
end
38+
for arg in w.args
39+
if typeof(arg) == MathLink.WExpr
40+
##Only check for MathLink Expressions
41+
if HasRecursiveGraphicsHead(arg)
42+
return true
43+
end
44+
end
45+
end
46+
return false
47+
end
48+
49+
function HeadsEndsWith(HeadString,Target)
50+
###Check if name ends with $Target
51+
if length(HeadString) >= length(Target)
52+
return HeadString[end-(length(Target)-1):end] == Target
53+
end
54+
return false
55+
end
56+
57+
58+
const graphics_heads = (W"Graphics", W"Graphics3D", W"Legended")
59+
60+
###Check if an expression has a grapics head
61+
HasGraphicsHead(w::MathLink.WSymbol) = false
62+
function HasGraphicsHead(w::MathLink.WExpr)
63+
HeadString = w.head.name
64+
###Check for graphics related names not based on ending on Plot* or Chart*
65+
if in(w.head, graphics)
66+
return true
67+
end
68+
69+
HeadsEndsWith(HeadString,"Plot") && return true
70+
HeadsEndsWith(HeadString,"Chart") && return true
71+
HeadsEndsWith(HeadString,"Plot3D") && return true
72+
HeadsEndsWith(HeadString,"Chart3D") && return true
73+
return false
74+
end
75+
76+
import Base.show
77+
Base.Multimedia.showable(::MIME"text/latex", w::MathLink.WSymbol) = MLtexOutput
78+
Base.show(io,::MIME"text/latex",x::MathLink.WSymbol) = print(io,"\$"*W2Tex(x)*"\$")
79+
80+
import Base.Multimedia.showable
81+
function Base.Multimedia.showable(::MIME"text/latex", w::MathLink.WExpr)
82+
if !MLtexOutput
83+
return false
84+
end
85+
if HasRecursiveGraphicsHead(w)
86+
return false
87+
else
88+
return true
89+
end
90+
end
91+
function Base.show(io,::MIME"text/latex",x::MathLink.WExpr)
92+
print(io,"\$"*W2Tex(x)*"\$")
93+
end
94+
95+
96+

test/runtests.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ using Test
33

44
import MathLink: WExpr, WSymbol
55

6+
7+
68
@testset "integers" begin
79
w = W"Factorial"(30)
810
@test_throws MathLink.MathLinkError weval(Int, w)
@@ -182,3 +184,52 @@ end
182184
P14 * Mat
183185
P14 * Mat* P14
184186
end
187+
188+
189+
190+
191+
@testset "Find Graphiscs" begin
192+
@test !HasGraphicsHead(W"a")
193+
@test HasGraphicsHead(W`Plot[x,{x,0,1}]`)
194+
@test HasGraphicsHead(W`ListPlot[x,{x,0,1}]`)
195+
@test HasGraphicsHead(W`ListLinePlot3D[x,{x,0,1}]`)
196+
@test HasGraphicsHead(W`Plot3D[x,{x,0,1}]`)
197+
@test !HasGraphicsHead(W"a"+W"b")
198+
@test HasGraphicsHead(weval(W`Plot[x,{x,0,1}]`))
199+
@test !HasGraphicsHead(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`)
200+
@test !HasGraphicsHead(weval(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`))
201+
202+
@test !HasRecursiveGraphicsHead(W`{2,a+v,{4+d}}`)
203+
@test HasRecursiveGraphicsHead(W`Plot[x,{x,0,1}]`)
204+
@test HasRecursiveGraphicsHead(W`{2,Plot[x^2,{x,0,1}]}`)
205+
@test HasRecursiveGraphicsHead(W`{a+b,Plot[x^2,{x,0,1}]}`)
206+
@test HasRecursiveGraphicsHead(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`)
207+
@test HasRecursiveGraphicsHead(W`{1,{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}}`)
208+
@test HasRecursiveGraphicsHead(weval(W`{Plot[x,{x,0,1}],Plot[x^2,{x,0,1}]}`))
209+
end
210+
211+
212+
213+
@testset "W2Tex - LaTex conversion" begin
214+
@test W2Tex(W`(a+b)^(b+x)`) == "(a+b)^{b+x}"
215+
@test W2Tex(W`a`) == "a"
216+
@test W2Tex(W`ab`) == "\\text{ab}"
217+
@test W2Tex(W`ab*cd`) == "\\text{ab} \\text{cd}"
218+
219+
220+
###Testing that MIME form exists for the text/latex option of show.
221+
io = IOBuffer();
222+
ioc = IOContext(io, :limit => true, :displaysize => (10, 10))
223+
show(ioc,"text/plain",W"a")
224+
@test String(take!(io)) == "W\"a\""
225+
show(ioc,"text/plain",W"a"+W"b")
226+
@test String(take!(io)) == "W\"Plus\"(W\"a\", W\"b\")"
227+
228+
set_texOutput(true)
229+
show(ioc,"text/latex",W"a"+W"b")
230+
@test String(take!(io)) == "\$a+b\$"
231+
@test showable("text/latex",W"a"+W"b")
232+
set_texOutput(false)
233+
@test !showable("text/latex",W"a"+W"b")
234+
end
235+

0 commit comments

Comments
 (0)