MrYouMath
MrYouMath

Reputation: 603

Sympy latex export using python

I am trying to use the following python code

Solved (Hint by @TheFool): By putting latex() into the print function it works.

from sympy import *
from sympy.printing.mathml import mathml
init_printing(use_unicode=True) # allow LaTeX printing

# independent variables
x, y, z = symbols('x y z', real = True)

# parameters
nu, rho = symbols('nu rho', real = True, constant = True, positive = True)

# dependent variables
u, v, w = symbols('u v w', real = True, cls = Function)

print(latex(diff(u(x,y,z),x)))

The output looks like '\\frac{\\partial}{\\partial x} u{\\left (x,y,z \\right )}'.

How can I remove the additional backslashes and the quotation marks at the beginning and at the end of the output?

Upvotes: 14

Views: 22553

Answers (3)

financial_physician
financial_physician

Reputation: 1978

from sympy import Matrix, print_latex

X = Matrix([1,2,3])
print_latex(X)

out: \left[\begin{matrix}1\\2\\3\end{matrix}\right]

(only prints out like that if your in a notebook environment)

Upvotes: 11

Marco Disce
Marco Disce

Reputation: 244

When you write the output on a file with

out_file = open("output.txt","w")
out_file.write(latex(diff(u(x,y,z),x)))
out_file.close()

They will look like you want them.

Upvotes: 2

The Fool
The Fool

Reputation: 20430

Have you tried to include:.. ?

from sympy import init_printing
init_printing() 

check out the ducumentation on sympy`s print options. It looks like the init_printing is the way to go for proper output.

http://docs.sympy.org/latest/tutorial/printing.html

Upvotes: 4

Related Questions