Python How to Adjust Printing in Sympy for Jupyter-Lab?

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Printing
AI Thread Summary
The discussion revolves around formatting mathematical expressions in Jupyter Lab using LaTeX syntax. The original code prints expressions like "Gamma^ r _ theta theta = r theta**2" but the user seeks a more LaTeX-friendly format, specifically something like "$$\Gamma^{\phi}_{\phi \theta} = r\theta^2$$". Participants suggest using the `IPython.display` module to utilize the `Latex` function for proper formatting. They highlight the need for string manipulation to correctly format subscripts and superscripts, noting that Python's string formatting requires double braces to include literal braces in the output. A solution is provided that successfully formats the expression as desired, demonstrating how to integrate LaTeX with Python code effectively. The final code snippet shared allows for the correct display of the mathematical expressions, addressing earlier issues with formatting and errors encountered.
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
 
Technology 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?
 
jedishrfu said:
You know latex right?
yeah. But there is also an equality sign.
 
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 \Gamma^r_{\theta r} 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 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 \Gamma^r_{\theta r} 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 Arman777
  • #10
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
 
Back
Top