Skip to content

Commit 58c8228

Browse files
authored
Merge pull request #333 from gustaphe/colon
Add range operator
2 parents 70d3c3c + 5166ddc commit 58c8228

File tree

7 files changed

+61
-1
lines changed

7 files changed

+61
-1
lines changed

src/Latexify.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ include("error.jl")
5959
include("numberformatters.jl")
6060

6161
include("latexify_function.jl")
62+
include("internal_recipes.jl")
6263

6364
### Add support for additional packages without adding them as dependencies.
6465
### Requires on <1.9 and weakdeps/extensions on >=1.9

src/internal_recipes.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
@latexrecipe function f(x::UnitRange; expand_ranges=false)
3+
expand_ranges && return collect(x)
4+
return :($(x.start) : $(x.stop))
5+
end
6+
7+
@latexrecipe function f(x::StepRange; expand_ranges=false, expand_step_ranges=true)
8+
(expand_ranges || expand_step_ranges) && return collect(x)
9+
return :($(x.start) : $(step(x)) : $(x.stop))
10+
end
11+
12+
@latexrecipe function f(x::StepRangeLen{T, <:Any, <:Any}; expand_ranges=false, expand_step_ranges=true) where {T}
13+
(expand_ranges || expand_step_ranges) && return collect(x)
14+
return :($(T(x.ref + (x.offset-1)*step(x))) : $(T(x.step)) : $(T(x.ref + (x.len-1)*x.step)))
15+
end
16+
17+
18+

src/latexarray.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ function _latexarray(
6767
return latexstr
6868
end
6969

70-
7170
_latexarray(args::AbstractArray...; kwargs...) = _latexarray(safereduce(hcat, args); kwargs...)
7271
_latexarray(arg::AbstractDict; kwargs...) = _latexarray(collect(keys(arg)), collect(values(arg)); kwargs...)
7372
_latexarray(arg::Tuple...; kwargs...) = _latexarray([collect(i) for i in arg]...; kwargs...)

src/latexoperation.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ function latexoperation(ex::Expr, prevOp::AbstractArray; kwargs...)::String
114114
return "$(args[1]) = $(args[2])"
115115
elseif op == :(!)
116116
return "\\neg $(args[2])"
117+
elseif op == :(:) && length(args) == 4
118+
return "$(args[2]) \\underset{$(args[3])}{$(binary_operators[:(:)])} $(args[4])"
117119
end
118120

119121
if ex.head == :.
@@ -320,6 +322,7 @@ function precedence(op::Symbol)
320322
startswith(string(op), "unary") && return Base.prec_power # Putting unary on par with :^, because there are no integers between 14 and 15. Should consider putting it with :<< instead
321323
op [:comparison, :issubset] && return Base.prec_comparison
322324
#op == :∀ && return Base.prec_control_flow
325+
op == :(:) && return 10
323326
prec = Base.operator_precedence(op)
324327
prec == 0 && return 100 # Base treats unknown as parenthesizable, we want no parenthesis if uncertain
325328
return prec
@@ -328,5 +331,6 @@ function associativity(op::Symbol)
328331
startswith(string(op), "unary") && return :right
329332
op == :comparison && return :none
330333
op == :issubset && return :none
334+
331335
return Base.operator_associativity(op)
332336
end

src/symbol_translations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ const binary_operators = Dict(
152152
arithmetic_operators...,
153153
:(=>) => "\\Rightarrow",
154154
: => "\\Longrightarrow",
155+
:(:) => "\\mathrel{\\ldotp\\mkern-2.5mu\\ldotp}"
155156
)
156157

157158
const unary_operators = Dict(

test/latexarray_test.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,38 @@ x & \cdot \\
300300
\right]
301301
\end{equation}
302302
", "\r\n"=>"\n")
303+
304+
@test latexify(:((1:3) .+ (1:2:5) .+ 3*(1:3))) == raw"$\left( 1 \mathrel{\ldotp\mkern-2.5mu\ldotp} 3 \right) + \left( 1 \underset{2}{\mathrel{\ldotp\mkern-2.5mu\ldotp}} 5 \right) + 3 \cdot \left( 1 \mathrel{\ldotp\mkern-2.5mu\ldotp} 3 \right)$"
305+
@test latexify(:($(1:3) .+ $(1:2:5) .+ $(3*(1:3)))) == replace(raw"$1 \mathrel{\ldotp\mkern-2.5mu\ldotp} 3 + \left[
306+
\begin{array}{c}
307+
1 \\
308+
3 \\
309+
5 \\
310+
\end{array}
311+
\right] + \left[
312+
\begin{array}{c}
313+
3 \\
314+
6 \\
315+
9 \\
316+
\end{array}
317+
\right]$", "\r\n"=>"\n")
318+
@test latexify(:($(1:3) .+ $(1:2:5) .+ $(3*(1:3))), expand_step_ranges=false) == raw"$1 \mathrel{\ldotp\mkern-2.5mu\ldotp} 3 + 1 \underset{2}{\mathrel{\ldotp\mkern-2.5mu\ldotp}} 5 + 3 \underset{3}{\mathrel{\ldotp\mkern-2.5mu\ldotp}} 9$"
319+
@test latexify(:($(1:3) .+ $(1:2:5) .+ $(3*(1:3))), expand_ranges=true) == latexify(:($(1:3) .+ $(1:2:5) .+ $(3*(1:3))), expand_ranges=true, expand_step_ranges=false) == replace(raw"$\left[
320+
\begin{array}{c}
321+
1 \\
322+
2 \\
323+
3 \\
324+
\end{array}
325+
\right] + \left[
326+
\begin{array}{c}
327+
1 \\
328+
3 \\
329+
5 \\
330+
\end{array}
331+
\right] + \left[
332+
\begin{array}{c}
333+
3 \\
334+
6 \\
335+
9 \\
336+
\end{array}
337+
\right]$", "\r\n"=>"\n")

test/latexraw_test.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ raw"a < b \leq c < d \leq e > f \leq g \leq h < i = j = k \neq l \neq m"
194194
@test latexraw(:(3 * (a .< b .<= c < d <= e > f <= g .<= h .< i == j .== k != l .!= m))) ==
195195
raw"3 \cdot \left( a < b \leq c < d \leq e > f \leq g \leq h < i = j = k \neq l \neq m \right)"
196196

197+
@test latexraw(:(2+3 : b)) == raw"2 + 3 \mathrel{\ldotp\mkern-2.5mu\ldotp} b"
198+
@test latexraw(:(a:3*4)) == raw"a \mathrel{\ldotp\mkern-2.5mu\ldotp} 3 \cdot 4"
197199

198200
#### Test the imaginary_unit keyword option
199201
@test latexraw(5im; imaginary_unit="\\textit{i}") == raw"5\textit{i}"

0 commit comments

Comments
 (0)