Can I capture sympy output in weave document?

I’m interested if I can render SymPy.jl-s output with Weave just like in jupyter notebooks? I know I can weave into tex and process further, but would be easier (at least for me) if the output could directly captured in html as well.
(For an MWE) in a notebook the following renders a nice fraction.

using SymPy
Sym(1//2)

Meanwhile with Weave it’s rendered as in the repl (as expected).

julia> using SymPy

julia> Sym(1//2)
1/2

I surely don’t know how jupyter/IJulia works, I just had this idea that if it’s possible to do in jupyter, it’s may be possible to do it with Weave as well. Sorry if I didn’t word my question right.

2 Likes

I just stumbled on a similar problem. Sym(1//2) works fine for me, i.e. compiled to \frac{1}/{2} but greek symbols are not converted to the latex format. I do not have a solution yet, but some thoughts:

With a jupyter notebook, it works nively due to a smart show function in
JuliaPy/SymPy.jl/src/types.jl

print(sympy.pretty(x))  # gives a nice ascii line rendered by the MathJax

The latex version of the command seems to ignore greek variables leaving them as they are:

print(sympy.latex(α, mode="equation*"))
# outputs \begin{equation*}α\end{equation*}, not \alpha

Latex cannot compile them.

Any ideas on how to fix it?

1 Like

kind of a bug

using SymPy
@vars α
[α α] # 1)
[α; α] # 2)
(α, α) # 3)

The weave2pdf would produce:

# 1) does not compile
\[\left[ \begin{array}{rr}α&α\end{array}\right]\] 
# 2) does not compile
\[ \left[ \begin{array}{r}α\\α\end{array} \right] \]
# 3) compiles
\begin{lstlisting}
((*@\ensuremath{\alpha}@*), (*@\ensuremath{\alpha}@*))
\end{lstlisting}
1 Like