Skip to content

Commit fa40374

Browse files
authored
allow changing latexify style (#101)
* RootedTrees.set_latexify_style * update docs * format
1 parent fb51bc3 commit fa40374

File tree

5 files changed

+345
-48
lines changed

5 files changed

+345
-48
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RootedTrees"
22
uuid = "47965b36-3f3e-11e9-0dcf-4570dfd42a8c"
33
authors = ["Hendrik Ranocha <mail@ranocha.de> and contributors"]
4-
version = "2.16.0"
4+
version = "2.17.0"
55

66
[deps]
77
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"

docs/src/tutorials/basics.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ This results in the following LaTeX output:
4242

4343
![latex-trees](https://user-images.githubusercontent.com/12693098/196148917-6e3cf000-5bc3-4798-8a82-d6e939bb6a8f.png)
4444

45+
To get a human-readable output, you can use
46+
[`RootedTrees.set_latexify_style`](@ref). This can be particularly helpful when
47+
working in Jupyter notebooks, e.g., by passing the output of `latexify` to
48+
`IPython.display.Latex`.
49+
50+
```@example basics
51+
RootedTrees.set_latexify_style("butcher")
52+
53+
for t in RootedTreeIterator(4)
54+
println(RootedTrees.latexify(t))
55+
end
56+
57+
RootedTrees.set_latexify_style("forest")
58+
59+
for t in RootedTreeIterator(4)
60+
println(RootedTrees.latexify(t))
61+
end
62+
```
63+
4564
If you want to visualize individual trees, you can also use our plot recipes
4665
for [Plots.jl](https://github.com/JuliaPlots/Plots.jl).
4766

@@ -131,6 +150,24 @@ for t in BicoloredRootedTreeIterator(3)
131150
end
132151
```
133152

153+
The style can be adapted as well via [`RootedTrees.set_latexify_style`](@ref).
154+
155+
```@example basics
156+
RootedTrees.set_latexify_style("butcher")
157+
158+
for t in BicoloredRootedTreeIterator(3)
159+
println(RootedTrees.latexify(t))
160+
end
161+
162+
RootedTrees.set_latexify_style("forest")
163+
164+
for t in BicoloredRootedTreeIterator(3)
165+
println(RootedTrees.latexify(t))
166+
end
167+
```
168+
169+
Plotting is of course also implemented for colored rooted trees.
170+
134171
```@example basics
135172
using Plots
136173
t = rootedtree([1, 2, 3, 3, 2, 2], Bool[0, 0, 1, 0, 1, 0])

src/RootedTrees.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ end
183183
#RootedTree{T<:Integer}(sequence::Vector{T}) = RootedTree{T}(sequence, false)
184184

185185
function Base.show(io::IO, t::RootedTree{T}) where {T}
186-
printing_style = @load_preference("printing_style")
186+
printing_style = @load_preference("printing_style", "sequence")
187187
if printing_style == "butcher"
188188
print(io, butcher_representation(t))
189189
else

src/latexify.jl

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ the following LaTeX code in the preamble.
3434
3535
```
3636
37+
To change the style of `latexify` to a human-readable Butcher-representation,
38+
you can use [`RootedTrees.set_latexify_style`](@ref).
39+
3740
# Examples
3841
3942
```jldoctest
@@ -48,32 +51,61 @@ function latexify(t::RootedTree)
4851
if isempty(t)
4952
return "\\varnothing"
5053
end
51-
list_representation = butcher_representation(t, false)
52-
s = "\\rootedtree" * replace(list_representation, "τ" => "[]")
53-
return replace(s, "[" => "[.")
54+
latexify_style = @load_preference("latexify_style", "forest")
55+
if latexify_style == "butcher"
56+
return butcher_representation(t)
57+
else # forest
58+
list_representation = butcher_representation(t, false)
59+
s = "\\rootedtree" * replace(list_representation, "τ" => "[]")
60+
return replace(s, "[" => "[.")
61+
end
5462
end
5563

5664
function latexify(t::BicoloredRootedTree)
5765
if isempty(t)
5866
return "\\varnothing"
5967
end
60-
list_representation = butcher_representation(rootedtree(t.level_sequence), false)
61-
s = "\\rootedtree" * replace(list_representation, "τ" => "[]")
62-
# The first entry of `substrings` is "\\rootedtree".
63-
substrings = split(s, "[")
64-
strings = String[]
65-
for (color, substring) in zip(t.color_sequence, substrings)
66-
if color == false
67-
push!(strings, substring * "[.")
68-
elseif color == true
69-
push!(strings, substring * "[o")
68+
latexify_style = @load_preference("latexify_style", "forest")
69+
if latexify_style == "butcher"
70+
return butcher_representation(t)
71+
else # forest
72+
list_representation = butcher_representation(rootedtree(t.level_sequence), false)
73+
s = "\\rootedtree" * replace(list_representation, "τ" => "[]")
74+
# The first entry of `substrings` is "\\rootedtree".
75+
substrings = split(s, "[")
76+
strings = String[]
77+
for (color, substring) in zip(t.color_sequence, substrings)
78+
if color == false
79+
push!(strings, substring * "[.")
80+
elseif color == true
81+
push!(strings, substring * "[o")
82+
end
7083
end
84+
# We still need to add the last part dropped by `zip`.
85+
push!(strings, last(substrings))
86+
return join(strings)
7187
end
72-
# We still need to add the last part dropped by `zip`.
73-
push!(strings, last(substrings))
74-
return join(strings)
7588
end
7689

7790
Latexify.@latexrecipe function _(t::Union{RootedTree, BicoloredRootedTree})
7891
return Latexify.LaTeXString(RootedTrees.latexify(t))
7992
end
93+
94+
"""
95+
RootedTrees.set_latexify_style(style::String)
96+
97+
Set the style of rooted trees when using [`latexify`](@ref). Possible options are
98+
99+
- "butcher": print the [`butcher_representation`](@ref) of rooted trees
100+
- "forest": use the LaTeX macro `\\rootedtree` described in the docstring of
101+
[`latexify`](@ref)
102+
103+
This system is based on [Preferences.jl](https://github.com/JuliaPackaging/Preferences.jl).
104+
"""
105+
function set_latexify_style(style::String)
106+
if !(style in ("butcher", "forest"))
107+
throw(ArgumentError("Invalid printing style: \"$(style)\""))
108+
end
109+
110+
@set_preferences!("latexify_style"=>style)
111+
end

0 commit comments

Comments
 (0)