From 9cba87405672413505495969bd989d563ff3f7ae Mon Sep 17 00:00:00 2001 From: Maximilian HUEBL Date: Sun, 25 Aug 2024 20:51:01 +0200 Subject: [PATCH] fix latexify on complex expressions --- src/latexify_recipes.jl | 6 ++++-- test/latexify.jl | 1 + test/latexify_refs/complex4.txt | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 test/latexify_refs/complex4.txt diff --git a/src/latexify_recipes.jl b/src/latexify_recipes.jl index b056252fb..2b42953d1 100644 --- a/src/latexify_recipes.jl +++ b/src/latexify_recipes.jl @@ -168,7 +168,9 @@ function _toexpr(O) end end - if isempty(numer) || !isone(abs(m.coeff)) + if !isreal(m.coeff) + numer_expr = Expr(:call, :*, m.coeff, numer...) + elseif isempty(numer) || !isone(abs(m.coeff)) numer_expr = Expr(:call, :*, abs(m.coeff), numer...) else numer_expr = length(numer) > 1 ? Expr(:call, :*, numer...) : numer[1] @@ -181,7 +183,7 @@ function _toexpr(O) frac_expr = Expr(:call, :/, numer_expr, denom_expr) end - if m.coeff < 0 + if isreal(m.coeff) && m.coeff < 0 return Expr(:call, :-, frac_expr) else return frac_expr diff --git a/test/latexify.jl b/test/latexify.jl index 9480adc91..d5249abc8 100644 --- a/test/latexify.jl +++ b/test/latexify.jl @@ -53,6 +53,7 @@ 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/complex4.txt" latexify(im * Symbolics.Term(sqrt, [2])) @test_reference "latexify_refs/indices1.txt" latexify(h[10,10]) @test_reference "latexify_refs/indices2.txt" latexify(h[10,10], index=:bracket) diff --git a/test/latexify_refs/complex4.txt b/test/latexify_refs/complex4.txt new file mode 100644 index 000000000..d7a210d25 --- /dev/null +++ b/test/latexify_refs/complex4.txt @@ -0,0 +1,3 @@ +\begin{equation} +\mathit{i} \sqrt{2} +\end{equation}