Naming sympy object for LaTeX

16 views
Skip to first unread message

Alan Bromborsky

unread,
Nov 16, 2009, 9:39:55 AM11/16/09
to sympy
In sympy you can give a symbol both an ascii and a latex name by:

x = Symbol('xup2','x^{2}')

so that :

print x -> xup2

and :

print latex(x) -> x^{2}

but for general functions you can only do:

f = Function('fup2')(x)

There is no option:

f = Function('fup2','f^{2}')(x)

Also if one does:

x = Symbol('xup2','x^{2}_{3}^{4}')

the latex(x) string in malformed.

Have I stated the current situation correctly and should it be corrected?

Daniel Jensen

unread,
Nov 17, 2009, 7:02:30 PM11/17/09
to sympy
Not to complicate matters further but I've often needed to print
variables in a function as sub/superscripts and wished I could type
something like:

ylm = Function('Y_{%s}^{%s}')
print latex(ylm(1, -1, theta)) -> Y_{1}^{-1}(\theta)

I would be willing to work on this if others think it is useful.

Daniel Jensen

unread,
Nov 17, 2009, 7:01:20 PM11/17/09
to sympy
Not to complicate matters further but I've often needed to print
variables in a function as sub/superscripts and wished I could type
something like:

ylm = Function('Y_{%s}^{%s}')
print latex(ylm(1, -1, theta)) -> Y_{1}^{-1}(\theta)

I would be willing to work on this if others think it is useful.

On Nov 16, 7:39 am, Alan Bromborsky <abro...@verizon.net> wrote:

Alan Bromborsky

unread,
Nov 18, 2009, 9:02:48 AM11/18/09
to sy...@googlegroups.com
> --
>
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=.
>
>
>
>
I am modifying latex.py for other reasons but it is easy to include such
an option, but I need to know how one distinguishes between a general
function such as 'f(x)' and a specific function such as 'sin(x)'. Is
there an easy test for this condition?

Also I was able to add a class function to the "Function" class
"set_latex_name(latex_name)", but am unsure as how to do something like
"F = Fuction('f','f^{x_{1}}')(x)" because of the python nomenclature
used "__new__" to instantciate a "Function". I could use an explanation
of how "__new__" in "class Function" works (my experience has always
been with "__init__". Also have no experience with the decorators used
is that instance.

Aaron S. Meurer

unread,
Nov 18, 2009, 11:41:19 AM11/18/09
to sy...@googlegroups.com
I think there isn't, other than to just test the function against all known predefined functions. There is a suggested fix for this on issue 1688.

Aaron Meurer
>
> Also I was able to add a class function to the "Function" class
> "set_latex_name(latex_name)", but am unsure as how to do something like
> "F = Fuction('f','f^{x_{1}}')(x)" because of the python nomenclature
> used "__new__" to instantciate a "Function". I could use an explanation
> of how "__new__" in "class Function" works (my experience has always
> been with "__init__". Also have no experience with the decorators used
> is that instance.
>

Alan Bromborsky

unread,
Nov 19, 2009, 8:11:17 AM11/19/09
to sy...@googlegroups.com
> --
>
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=.
>
>
>
>
Proposal for inputting latex strings in Symbol and Functions:

Instanciate a symbol of function as follows:

A = Symbol('a|b') or F = Function('f|g') where b is a "latex" string.

Then you would get:

print A -> a or print F -> f

and:

print latex(A) -> b or print latex(F) -> g

b or g could be strings that with post processing give a standard latex
string.

I would suggest that instead of letting g = Y_{%s}^{%s} one would use g
= Y_{#1}^{#2}(#3)
where #1, #2, and #3 refer to the positional arguments of the function.

Vinzent Steinberg

unread,
Nov 29, 2009, 2:26:14 PM11/29/09
to sympy
On Nov 19, 2:11 pm, Alan Bromborsky <abro...@verizon.net> wrote:
> Proposal for inputting latex strings in Symbol and Functions:
>
> Instanciate a symbol of function as follows:
>
>     A = Symbol('a|b') or F = Function('f|g') where b is a "latex" string.
>
> Then you would get:
>
>     print A -> a  or print F -> f
>
> and:
>
>     print latex(A) -> b or print latex(F) -> g
>
> b or g could be strings that with post processing give a standard latex
> string.
>
> I would suggest that instead of letting  g = Y_{%s}^{%s} one would use g
> = Y_{#1}^{#2}(#3)
> where #1, #2, and #3 refer to the positional arguments of the function.

I would rather add another keyword argument to Symbol(), something
like

a = Symbol('a', latex_name='b')


Vinzent

Alan Bromborsky

unread,
Nov 29, 2009, 2:43:17 PM11/29/09
to sy...@googlegroups.com
> --
>
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
>
>
>
How do I do that with Function also?

Alan Bromborsky

unread,
Nov 29, 2009, 2:45:53 PM11/29/09
to sy...@googlegroups.com
> --
>
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
>
>
>
I should have added to the previous reply that I did not understand the
way arguments are passed in Function so I did not know how to add a new
named argument of the constructor Fuction(name)(tuple).
Reply all
Reply to author
Forward
0 new messages