user3637963
user3637963

Reputation: 21

SymPy: LaTeX printing of functions with many arguments

My question pertains to the output of the LaTeX printer of SymPy. Specifically I have a function with multiple arguments that I wish to take the derivative of. By way of example, I have the following script:

from sympy import *
a1, a2, a3, a4, a5 = symbols("a1 a2 a3 a4 a5")
f = Function("f")(a1, a2, a3, a4, a5)
g = Function("g")(f)
print latex(Derivative(g, a2))

The stdout of this is:

\frac{\partial}{\partial a_{2}} g{\left (f{\left (a_{1},a_{2},a_{3},a_{4},a_{5} \right )} \right )}

I, however, would like my output to look like this:

\frac{\partial g}{\partial{ a_{2}}}

Is this possible using SymPy? Thanks in advance.

PS: Is there a way to render LaTeX in these posts?

Upvotes: 2

Views: 920

Answers (1)

smichr
smichr

Reputation: 19093

The latex printing of a Derivative chooses partial-d or d depending on the number of variables in the object being differentiated. In your case there is only the single variable a_2 so the d will be used. It also depends on whether the function being differentiated has more than one variable in it. In your case, you do have such a function but you don't want it (apparently) to show the variables on which it depends. The closest hack I can think of is to use something like this:

>>> dots=var('...')
>>> a2 = var('a2')
>>> (Derivative(g(a2,dots),a2))
 ∂             
───(g(a₂, ...))
∂a₂     

or

>>> (Derivative(g(dots,a2,dots),a2))
 ∂                  
───(g(..., a₂, ...))
∂a₂  

Note: I am using live.sympy to generate the latex output. I think you can use isympy, too.

Upvotes: 1

Related Questions