# Latexify reverses order of terms

I have been using Julia for about a week and learning by making many many mistakes. I have a symbolic matrix generated using the Symbolics package -

Num[1 0 0; 0 r^2 0; 0 0 (r^2)*(sin(var"\\theta")^2)]


The output of latexify is then -

\begin{equation*}
\left[
\begin{array}{ccc}
1 & 0 & 0 \\
0 & r^{2} & 0 \\
0 & 0 & \sin^{2}\left( \theta \right) r^{2} \\
\end{array}
\right]
\end{equation*}


In the original array we have r^2\sin(\theta)^2, whereas the latexify output is \sin(\theta)^2 r^2. Is there anyway to prevent this from happening?

It’s the only way.

I don’t think you can get greater control over the LaTeX output, because you can’t control the order of the terms in Symbolics’ internal representation. But I hope someone who knows more than I do about it will correct me.

By default, Symbolics.jl simplifies expressions automatically on construction which does make many use cases more difficult.

Sometimes it helps to use LiteralReal

julia> @variables r::LiteralReal θ::LiteralReal


See discussion at

Here is example of my usage -

function symbol(s::String)
x = Symbol(s)
x, = @variables $x return x end r,th,phi = symbols(raw"r \theta \phi") println(latexify(th)) and output - \theta If I use - function symbol(s::String) x = Symbol(s) x, = @variables$x::LiteralReal
return x
end

I get the error -

ERROR: LoadError: AssertionError: @variables expects a tuple of expressions or an expression of a tuple (@variables x y z(t) v[1:3] w[1:2,1:4] or @variables x y z(t) v[1:3] w[1:2,1:4] k=1.0)
Stacktrace:
[1] _parse_vars(macroname::Symbol, type::Type, x::Tuple{Expr}, transform::Function)
@ Symbolics ~/.julia/packages/Symbolics/sITWZ/src/variable.jl:134
[2] _parse_vars(macroname::Symbol, type::Type, x::Tuple{Expr})
@ Symbolics ~/.julia/packages/Symbolics/sITWZ/src/variable.jl:94
[3] var"@variables"(source::LineNumberNode, module::Module, xs::Vararg{Any})
@ Symbolics ~/.julia/packages/Symbolics/sITWZ/src/variable.jl:286
in expression starting at /home/brombo/MyJulia/Bases.jl:12
in expression starting at /home/brombo/MyJulia/Bases.jl:10

What am I doing wrong?