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

Discussion Overview

The discussion focuses on how to adjust the printing of mathematical expressions in SymPy for Jupyter Lab, particularly aiming to format outputs in LaTeX style. Participants explore various methods for achieving desired formatting, including the use of string manipulation and the display function.

Discussion Character

  • Technical explanation
  • Exploratory
  • Mathematical reasoning

Main Points Raised

  • One participant shares a code snippet for printing expressions and requests a way to format it in LaTeX style.
  • Some participants suggest using LaTeX code directly in print statements, questioning if it can be rendered by MathJax.
  • There are discussions about the need for raw strings to prevent backslashes from being evaluated.
  • Concerns are raised about formatting issues, such as the inability to include multiple subscripts and how commas affect line breaks in output.
  • Participants propose using the IPython display function with LaTeX formatting, highlighting the need for careful string manipulation due to Python's formatting syntax.
  • One participant encounters a TypeError when trying to format SymPy symbols directly and shares their findings on how to properly format these symbols for LaTeX output.
  • A later reply presents an upgraded solution that successfully formats the expression as desired, thanking others for their assistance.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method but share various approaches and solutions, indicating that multiple strategies may be valid for achieving the desired output.

Contextual Notes

Participants express limitations related to formatting and rendering, particularly concerning the handling of special characters and the syntax of Python's string 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
880
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K