diff --git a/Project.toml b/Project.toml index 23ce99c..6814df8 100644 --- a/Project.toml +++ b/Project.toml @@ -5,7 +5,6 @@ version = "1.5.3" [deps] LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" -Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" @@ -17,6 +16,7 @@ ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" FastDifferentiation = "eb9bf01b-bf85-4b60-bf87-ee5de06c00be" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" GenericLinearAlgebra = "14197337-ba66-59df-a3e3-ca00e7dcff7a" +Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" [extensions] @@ -24,6 +24,7 @@ QuaternionicChainRulesCoreExt = "ChainRulesCore" QuaternionicFastDifferentiationExt = "FastDifferentiation" QuaternionicForwardDiffExt = "ForwardDiff" QuaternionicSymbolicsExt = "Symbolics" +QuaternionicLatexifyExt = "Latexify" [compat] ChainRulesCore = "1" diff --git a/ext/QuaternionicFastDifferentiationExt.jl b/ext/QuaternionicFastDifferentiationExt.jl index 0e43725..f4dad2c 100644 --- a/ext/QuaternionicFastDifferentiationExt.jl +++ b/ext/QuaternionicFastDifferentiationExt.jl @@ -5,7 +5,7 @@ import Quaternionic: normalize, absvec, AbstractQuaternion, Quaternion, Rotor, QuatVec, quaternion, rotor, quatvec, QuatVecF64, RotorF64, QuaternionF64, - wrapper, components, _pm_ascii, _pm_latex + wrapper, components using PrecompileTools isdefined(Base, :get_extension) ? (using FastDifferentiation) : (using ..FastDifferentiation) diff --git a/ext/QuaternionicLatexifyExt.jl b/ext/QuaternionicLatexifyExt.jl new file mode 100644 index 0000000..d6a69fb --- /dev/null +++ b/ext/QuaternionicLatexifyExt.jl @@ -0,0 +1,30 @@ +module QuaternionicLatexifyExt + +import Quaternionic: AbstractQuaternion, QuatVec +isdefined(Base, :get_extension) ? (using Latexify) : (using ..Latexify) + +function _pm_latex_latexify(x) + # Utility function to print a component of a quaternion in LaTeX + s = Latexify.latexify(x, env=:raw, bracket=true) + if s[1] ∉ "+-" + s = "+" * s + end + if occursin(r"[+^/-]", s[2:end]) + s = " " * s[1] * " " * "\\left(" * s[2:end] * "\\right)" + else + s = " " * s[1] * " " * s[2:end] + end + s +end + +function Base.show(io::IO, ::MIME"text/latex", q::AbstractQuaternion) + s = Latexify.LaTeXStrings.latexstring( + q isa QuatVec ? "" : Latexify.latexify(q[1], env=:raw, bracket=true), + _pm_latex_latexify(q[2]), "\\,\\mathbf{i}", + _pm_latex_latexify(q[3]), "\\,\\mathbf{j}", + _pm_latex_latexify(q[4]), "\\,\\mathbf{k}" + ) + print(io, s) +end + +end # module diff --git a/ext/QuaternionicSymbolicsExt.jl b/ext/QuaternionicSymbolicsExt.jl index 2f08596..0496368 100644 --- a/ext/QuaternionicSymbolicsExt.jl +++ b/ext/QuaternionicSymbolicsExt.jl @@ -1,7 +1,6 @@ module QuaternionicSymbolicsExt using StaticArrays: SVector -using Latexify: latexify import Quaternionic: normalize, absvec, AbstractQuaternion, Quaternion, Rotor, QuatVec, quaternion, rotor, quatvec, @@ -196,9 +195,9 @@ function _pm_ascii(x::Symbolics.Num) end s end -function _pm_latex(x::Num) +function _pm_latex(x::Symbolics.Num) # Utility function to print a component of a quaternion in LaTeX - s = latexify(x, env=:raw, bracket=true) + s = Symbolics.Latexify.latexify(x, env=:raw, bracket=true) if s[1] ∉ "+-" s = "+" * s end diff --git a/src/Quaternionic.jl b/src/Quaternionic.jl index c55313f..24c2f0c 100644 --- a/src/Quaternionic.jl +++ b/src/Quaternionic.jl @@ -1,7 +1,7 @@ module Quaternionic using StaticArrays, LinearAlgebra, PrecompileTools -using Latexify, LaTeXStrings +import LaTeXStrings import Random: AbstractRNG, default_rng, randn! export AbstractQuaternion @@ -56,6 +56,7 @@ end @require FastDifferentiation="eb9bf01b-bf85-4b60-bf87-ee5de06c00be" include("../ext/QuaternionicFastDifferentiationExt.jl") @require ForwardDiff="f6369f11-7733-5829-9624-2563aa707210" include("../ext/QuaternionicForwardDiffExt.jl") @require Symbolics="0c5d862f-8b57-4792-8d23-62f2024744c7" include("../ext/QuaternionicSymbolicsExt.jl") + @require Latexify="23fbe1c1-3f47-55db-b15f-69d7ec21a316" include("../ext/QuaternionicLatexifyExt.jl") end # COV_EXCL_STOP diff --git a/src/base.jl b/src/base.jl index 5a8a9a6..ac6fdff 100644 --- a/src/base.jl +++ b/src/base.jl @@ -89,29 +89,29 @@ function Base.show(io::IO, q::Rotor) print(io, ")") end -function _pm_latex(x) - # Utility function to print a component of a quaternion in LaTeX - s = latexify(x, env=:raw, bracket=true) - if s[1] ∉ "+-" - s = "+" * s - end - if occursin(r"[+^/-]", s[2:end]) - s = " " * s[1] * " " * "\\left(" * s[2:end] * "\\right)" - else - s = " " * s[1] * " " * s[2:end] - end - s -end - -function Base.show(io::IO, ::MIME"text/latex", q::AbstractQuaternion) - s = latexstring( - q isa QuatVec ? "" : latexify(q[1], env=:raw, bracket=true), - _pm_latex(q[2]), "\\,\\mathbf{i}", - _pm_latex(q[3]), "\\,\\mathbf{j}", - _pm_latex(q[4]), "\\,\\mathbf{k}" - ) - print(io, s) -end +# function _pm_latex(x) +# # Utility function to print a component of a quaternion in LaTeX +# s = "$x" +# if s[1] ∉ "+-" +# s = "+" * s +# end +# if occursin(r"[+^/-]", s[2:end]) +# s = " " * s[1] * " " * "\\left(" * s[2:end] * "\\right)" +# else +# s = " " * s[1] * " " * s[2:end] +# end +# s +# end + +# function Base.show(io::IO, ::MIME"text/latex", q::AbstractQuaternion) +# s = LaTeXStrings.latexstring( +# q isa QuatVec ? "" : "$(q[1])", +# _pm_latex(q[2]), "\\,\\mathbf{i}", +# _pm_latex(q[3]), "\\,\\mathbf{j}", +# _pm_latex(q[4]), "\\,\\mathbf{k}" +# ) +# print(io, s) +# end function Base.read(s::IO, QT::Type{Q}) where {T<:Number, Q<:AbstractQuaternion{T}} w = read(s,T)