How to Adjust Printing in Sympy for Jupyter-Lab?

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Printing
Click For Summary
SUMMARY

This discussion focuses on adjusting the output format of mathematical expressions in Jupyter Lab using SymPy. Users aim to render expressions like the Christoffel symbols in LaTeX format, specifically using the IPython.display.Latex function. The solution involves string manipulation with Python's format method to correctly display subscripts and superscripts. The final working code provided demonstrates how to format and display the desired LaTeX output effectively.

PREREQUISITES
  • Familiarity with Python programming
  • Understanding of LaTeX syntax for mathematical expressions
  • Experience with Jupyter Lab environment
  • Knowledge of SymPy library for symbolic mathematics
NEXT STEPS
  • Explore advanced string formatting techniques in Python
  • Learn more about the IPython.display module and its functions
  • Investigate the sympy.latex function for converting SymPy expressions to LaTeX
  • Practice rendering complex mathematical expressions in Jupyter Lab using LaTeX
USEFUL FOR

Mathematicians, data scientists, educators, and anyone using Jupyter Lab for mathematical computations who wants to enhance the presentation of their outputs with LaTeX formatting.

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   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 \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   Reactions: 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
 

Similar threads

  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 24 ·
Replies
24
Views
2K
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 0 ·
Replies
0
Views
860
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K