The output of diff fails to render in with enabled LaTeX

Viewed 52

I want to differentiate an expression as follows in sympy. I am using jupyter qtconsole with enabled latex:

from sympy import *
init_printing()
p0,nu,lamb,k,epsp, dp0 = symbols(r'p_0, nu, lambda, kappa, \delta\epsilon_p^p, \deltap_0')
test = Eq(epsp,((lamb-k)/nu)*dp0/p0)
p0expr = solve(test,p0)[0]
diff(p0expr, epsp)
diff(p0expr,k)
diff(p0expr,lamb)

I'd like to focus on the last three lines of code, lines 6, 7 and 8. Lines 7 and 8 output latex style output while line 6 does not. Why does line 6 not get output in latex?

1 Answers

From looking at the text output for diff(p0expr, epsp),

-\deltap_0*(-kappa + lambda)/(\delta\epsilon_p^p**2*nu)

the problem is in squaring the variable you named \delta\epsilon_p^p. (The other two derivatives don't have it squared.) If this square gets converted in LaTeX markup as

\delta\epsilon_p^p^2

the result is invalid LaTeX due to multiple superscripts. Suggestion: put curly braces around this symbol name,

symbols(r'p_0, nu, lambda, kappa, {\delta\epsilon_p^p}, \deltap_0')

which will prevent such interference between the name and the formula containing it.

Tested with http://live.sympy.org/ :

live sympy

Top output is from your version, bottom is with curly braces as above. It renders except for \deltap which of course is an undefined LaTeX command. Unless it's a custom thing you defined elsewhere, it's something that should be fixed with {\delta}p_0.

Related