# Latexify reverses order of terms

**URL:** <https://discourse.julialang.org/t/latexify-reverses-order-of-terms/99174>\
**Category:** New to Julia\
**Created:** [May 20, 2023, 10:01pm UTC](https://discourse.julialang.org/t/latexify-reverses-order-of-terms/99174 "2023-05-20T22:01:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [May 20, 2023, 10:01pm UTC](https://discourse.julialang.org/t/latexify-reverses-order-of-terms/99174/1 "2023-05-20T22:01:30Z")

</div>

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 -

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

```

The output of `latexify` is then -

```julia
\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?

---

<div class="post-metadata">

**Author:** ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)\
**Post date:** [May 22, 2023, 7:13pm UTC](https://discourse.julialang.org/t/latexify-reverses-order-of-terms/99174/2 "2023-05-22T19:13:02Z")

</div>

> [@brombo](#):
>
> learning by making many many mistakes

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.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 22, 2023, 7:21pm UTC](https://discourse.julialang.org/t/latexify-reverses-order-of-terms/99174/3 "2023-05-22T19:21:57Z")

</div>

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

Sometimes it helps to use `LiteralReal`

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

```

See discussion at

> <https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/327>
>
> Currently SymbolicUtils uses \[constructor-level simplification\](https://github.c…om/JuliaSymbolics/SymbolicUtils.jl/pull/154) and automatic evaluation of expressions. This may be convenient for numerical work, but makes SymbolicUtils inappropriate for purely symbolic manipulation. It also eliminates algorithmic speedup opportunities.
> 
> There is an asymmetry: a raw symbolic expression can always be simplified for numerical work, but once simplified an expression cannot be unsimplified for symbolic work. To support both kinds of users and ensure high performance, I would like to propose that SymbolicUtils adopt a posture of not simplifying or evaluating without explicit user request.
> 
> For example, with \`@syms a b\`:
> 
> \- \`a + 1\` would stay \`a + 1\`, not changing to \`1 + a\` as it currently does.
> \- \`substitute(a+b, Dict(a=\>1, b=\>2))\` would go to \`Term(+, \[1,2\])\`, not \`3\` as it currently does. 
> 
> The current SymPy maintainer has \[warned\](https://discourse.julialang.org/t/cas-best-practices/58092/59) of this issue's critical importance. He says \[emphasis added\]:
> 
> \> Personally I think that \*\*automatic evaluation is the major mistake made in sympy\*\* (and many other CAS). That is what currently \*\*slows down ordinary operations\*\* in sympy. Automatic evaluation is \*\*extremely hard to change\*\* when backwards compatibility comes into play because any change is not compatible.
> 
> 
> 
> I hope we can get these decisions built into the early foundation of Symbolics.jl so we don't follow the wrong path.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [May 23, 2023, 1:52pm UTC](https://discourse.julialang.org/t/latexify-reverses-order-of-terms/99174/4 "2023-05-23T13:52:52Z")

</div>

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 -

\begin{equation}  
\theta  
\end{equation}

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?
