How to Adjust Printing in Sympy for Jupyter-Lab?

  • Topic: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Printing
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
9 replies · 2K views
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have a code that is something like this

Code:
print("Gamma^",syms[i],"_",syms[j],syms[k], "=", syms[1], syms[2]**2)

When I print in jupyter-lab it looks like this

Code:
Gamma^ r _ theta theta = r theta**2

Gamma^ r _ phi phi = r theta**2

Gamma^ theta _ r theta = r theta**2

Gamma^ theta _ theta r = r theta**2

Gamma^ theta _ phi phi = r theta**2

Gamma^ phi _ r phi = r theta**2

Gamma^ phi _ theta phi = r theta**2

Gamma^ phi _ phi r = r theta**2

Gamma^ phi _ phi theta = r theta**2

Is there a way to make it something like this

$$\Gamma^{\phi}_{\phi \theta} = r\theta^2$$

or very similar at least
 
Physics news on Phys.org
Does the print accept latex code like \Gamma ?

It may pass it as is the webpage and the mathjax will render it. Also you may need to use a raw string so the backslash isn't evaluated.

Might need to bracket it with ‘$ $’ or ‘# #’ to get it recognized by mathjax:

print(“# #\Gamma... rest of expression and then “# #”)
 
jedishrfu said:
Does the print accept latex code like \Gamma ?

It may pass it as is the webpage and the mathjax will render it.
I did not understand getitrecognized...

Could you put it in the code segment ?
 
My apology i was trying to provide an example but the PF mathjax processed it. Youll need to experiment to understand.

You know latex right?
 
1617562501008.png


display works a bit but not much. First the major problem is that I cannot put both subcripts such as $$\Gamma^{r}_{\theta r}$$
etc.

Second each comma moves it to the next line but ıf I remove the comma the code gives an error.
 
Arman777 said:
View attachment 280921

display works a bit but not much. First the major problem is that I cannot put both subcripts such as $$\Gamma^{r}_{\theta r}$$
etc.

Second each comma moves it to the next line but ıf I remove the comma the code gives an error.

Do some string manipulation. For example,
Python:
from IPython.display import Latex, display

display(Latex('$\Gamma^{{{0:s}}}{{}}_{{{1:s} {2:s}}}$'.format('r','\\theta','r')))
produces [itex]\Gamma^r_{\theta r}[/itex] if run in a jupyer workbook cell.

It's unfortunate that {} has a syntactic meaning for python's String.format function, so if you want a literal brace in the argument passed to Latex you need to enter a double brace in the string on which you invoke format.

You can obtain the latex code of a sympy expression by passing it to sympy.latex.

So I guess you want to try
Python:
display(Latex('$\\Gamma^{{{0:s}}}{{}}_{{{1:s} {2:s}}} = {3:s}$'.format(
syms[i], syms[j], syms[k], latex(Christoffel_symbol[i,j,k])
)
))
 
  • Like
Likes   Reactions: Arman777
pasmith said:
Do some string manipulation. For example,
Python:
from IPython.display import Latex, display

display(Latex('$\Gamma^{{{0:s}}}{{}}_{{{1:s} {2:s}}}$'.format('r','\\theta','r')))
produces [itex]\Gamma^r_{\theta r}[/itex] if run in a jupyer workbook cell.

It's unfortunate that {} has a syntactic meaning for python's String.format function, so if you want a literal brace in the argument passed to Latex you need to enter a double brace in the string on which you invoke format.

You can obtain the latex code of a sympy expression by passing it to sympy.latex.

So I guess you want to try
Python:
display(Latex('$\\Gamma^{{{0:s}}}{{}}_{{{1:s} {2:s}}} = {3:s}$'.format(
syms[i], syms[j], syms[k], latex(Christoffel_symbol[i,j,k])
)
))
It says
Code:
TypeError: unsupported format string passed to Symbol.__format__
 
I think I have been able to reproduce that, together with two solutions:
Python:
>>> from sympy import Symbol, latex
>>> "{0:s}".format(Symbol('\\theta'))
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    "{0:s}".format(Symbol('\\theta'))
TypeError: unsupported format string passed to Symbol.__format__
>>> "{0}".format(Symbol('\\theta'))
'\\theta'
>>> "{0:s}".format(latex(Symbol('\\theta')))
'\\theta'

So I would try
Python:
display(Latex('$\\Gamma^{{{0}}}{{}}_{{{1} {2}}} = {3}$'.format(
syms[i], syms[j], syms[k], Christoffel_symbol[i,j,k]
))
 
  • Like
Likes   Reactions: Arman777
I have upgraded your solution. This works just as I have wanted.

Code:
display(Latex('$\\Gamma^{{{0}}}{{}}_{{{1}{2}}} = {3}$'.format(latex(syms[i]), latex(syms[j]), latex(syms[k]), latex(Christoffel_symbol[i,j,k]))))

Thanks for the help. I really appreciate it