How to Adjust Printing in Sympy for Jupyter-Lab?

  • Python
  • Thread starter Arman777
  • Start date
  • Tags
    Printing
In summary: It says TypeError: unsupported format string passed to Symbol.__format__.I think I have been able to reproduce that, together with two solutions:>>> 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
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
  • #2
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 “# #”)
 
  • #3
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 ?
 
  • #4
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?
 
  • #5
jedishrfu said:
You know latex right?
yeah. But there is also an equality sign.
 
  • #6
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.
 
  • #7
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 Arman777
  • #8
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__
 
  • #9
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
 

FAQ: How to Adjust Printing in Sympy for Jupyter-Lab?

How can I change the default printing format in Sympy?

To change the default printing format in Sympy, you can use the init_printing() function. This function takes in a few optional parameters that allow you to adjust the printing format, such as use_unicode for displaying special characters and wrap_line for controlling line wrapping.

Can I customize the output of specific mathematical expressions in Sympy?

Yes, you can use the pprint() function to customize the output of specific mathematical expressions in Sympy. This function takes in an expression as an argument and allows you to adjust the printing format for that specific expression. This is useful for displaying complex expressions in a more readable format.

How do I change the number of significant digits displayed in Sympy?

To change the number of significant digits displayed in Sympy, you can use the set_float_precision() function. This function takes in an integer value that represents the number of significant digits to display for floating-point numbers. By default, Sympy displays 15 significant digits, but you can adjust this to your preference.

Is it possible to change the printing format for specific objects, such as matrices or symbols?

Yes, it is possible to change the printing format for specific objects in Sympy. You can use the init_printing() function with the order parameter to specify the printing format for specific classes of objects. For example, you can use order='old' to print matrices in the traditional format or order='none' to disable printing for symbols.

How can I adjust the spacing and alignment in Sympy's output?

To adjust the spacing and alignment in Sympy's output, you can use the init_printing() function with the use_latex parameter. This allows you to use LaTeX formatting to control the spacing and alignment of expressions. Additionally, you can use the pretty() function to customize the formatting of specific expressions, such as adding horizontal or vertical spacing between terms.

Similar threads

Back
Top