Skip to content

Commit

Permalink
Merge pull request #1185 from gustaphe/multiplesubscriptslatex
Browse files Browse the repository at this point in the history
Latexify indices correctly
  • Loading branch information
ChrisRackauckas authored Jul 3, 2024
2 parents 374b7ac + bcab562 commit dd848d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/latexify_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ recipe(n) = latexify_derivatives(cleanup_exprs(_toexpr(n)))
env --> :equation
cdot --> false
fmt --> FancyNumberFormatter(5)
index --> :subscript

return recipe(n)
end

@latexrecipe function f(z::Complex{Num})
env --> :equation
cdot --> false
index --> :subscript

iszero(z.im) && return :($(recipe(z.re)))
iszero(z.re) && return :($(recipe(z.im)) * $im)
Expand All @@ -67,12 +69,14 @@ end
@latexrecipe function f(n::ArrayOp)
env --> :equation
cdot --> false
index --> :subscript
return recipe(n.term)
end

@latexrecipe function f(n::Function)
env --> :equation
cdot --> false
index --> :subscript

return nameof(n)
end
Expand All @@ -81,18 +85,21 @@ end
@latexrecipe function f(n::Arr)
env --> :equation
cdot --> false
index --> :subscript

return unwrap(n)
end

@latexrecipe function f(n::Symbolic)
env --> :equation
cdot --> false
index --> :subscript

return recipe(n)
end

@latexrecipe function f(eqs::Vector{Equation})
index --> :subscript
has_connections = any(x->x.lhs isa Connection, eqs)
if has_connections
env --> :equation
Expand All @@ -105,6 +112,7 @@ end

@latexrecipe function f(eq::Equation)
env --> :equation
index --> :subscript

if eq.lhs isa Connection
return eq.rhs
Expand All @@ -114,6 +122,7 @@ end
end

@latexrecipe function f(c::Connection)
index --> :subscript
return Expr(:call, :connect, map(nameof, c.systems)...)
end

Expand Down Expand Up @@ -236,12 +245,7 @@ function getindex_to_symbol(t)
@assert iscall(t) && operation(t) === getindex && symtype(sorted_arguments(t)[1]) <: AbstractArray
args = sorted_arguments(t)
idxs = args[2:end]
try
sub = join(map(map_subscripts, idxs), "ˏ")
return Symbol(_toexpr(args[1]), sub)
catch
return :($(_toexpr(args[1]))[$(idxs...)])
end
return :($(_toexpr(args[1]))[$(idxs...)])
end

function diffdenom(e)
Expand Down
5 changes: 4 additions & 1 deletion test/latexify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using ReferenceTests

using DomainSets: Interval

@variables x y z u(x) dx
@variables x y z u(x) dx h[1:10,1:10]
Dx = Differential(x)
Dy = Differential(y)

Expand Down Expand Up @@ -53,3 +53,6 @@ Dy = Differential(y)
@test_reference "latexify_refs/complex1.txt" latexify(x^2-y^2+2im*x*y)
@test_reference "latexify_refs/complex2.txt" latexify(3im*x)
@test_reference "latexify_refs/complex3.txt" latexify(1 - x + (1+2x)*im; imaginary_unit="\\mathbb{i}")

@test_reference "latexify_refs/indices1.txt" latexify(h[10,10])
@test_reference "latexify_refs/indices2.txt" latexify(h[10,10], index=:bracket)
3 changes: 3 additions & 0 deletions test/latexify_refs/indices1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\begin{equation}
h_{10,10}
\end{equation}
3 changes: 3 additions & 0 deletions test/latexify_refs/indices2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\begin{equation}
h\left[10, 10\right]
\end{equation}

0 comments on commit dd848d9

Please sign in to comment.