Sabian Sabian - 10 months ago 77
LaTeX Question

SymPy print only function name

I'm trying to do some symbolic calculations in SymPy but I'm unable to use latex printing and get the graphical output I want. That has always troubled me and have spent several hours (perhaps days) trying to fine a way to customize how objects are printed (in LaTeX, in pprint representation it's well documented).

However, in this case I'm trying to make symbolic calculations on an Undefined Function, psi, which depends on x, y, z. However, those calculations imply terms which have up to three times the curl of psi* \vec r.

I have the following code


R = ReferenceFrame(r"E", variables=["x", "y", "z"])
psi = Function(r"\psi")(R[0], R[1], R[2])
rpsi = (R[0]*R.x + R[1]*R.y + R[2]*R.z)*psi

Where, in case you aren't familiar with it,
is the i-th variable (x, y or z) and
are the unitary cartesian vectors, instantiated in the call to ReferenceFrame (a function from sympy.physics).

When I try to take a look at
curl(curl(rpsi, R), R)
for example, the output is so filled with "\psi(x, y, z)" (compiled in latex and showed as an image) that it gets tedious to follow the expression.

Is there a way to customize the latex string that gets compiled (which would apply to other calculations that I'm doing). If that is not possible, how can solve this particular problem (i.e. independent variables displaying every time the function is printed).

I'm using jupyter with qtconsole.


Answer Source

The simplest way would be to create a custom function, which prints as you want. You can achieve this by subclassing Function and defining _latex (see

In [33]: class psi(Function):
   ....:     def _latex(self, printer):
   ....:         return r'\psi'

In [34]: latex(psi(x, y, z))
Out[34]: '\\psi'

If you want to print the arguments, access them with self.args and use printer._print (e.g., the usual printer would be something like r'\phi{\left (' + ', '.join(printer._print(i) for i in self.args) + '\right )}').