Sabian - 10 months ago 77

LaTeX Question

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

`init_printing(use_latex=True)`

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,

`R[i]`

`R.x`

`R.y`

`R.z`

When I try to take a look at

`curl(curl(rpsi, R), R)`

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.

Cheers,

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 http://docs.sympy.org/latest/modules/printing.html#sympy.printing.latex.LatexPrinter.printmethod).

```
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 )}'`

).