英文:
How to an extra string to the display of an object with "display" in Jupyter, using Python?
问题
I have a number of mathematical objects created with SymPy, which display fine in Jupyter using "display". For example, there are a number of functions which I can display with:
Question 1: f1(x)
Question 2: f2(x)
and so on. But what I want is to prefix each line with some explanatory text, so the output is, for example:
Question 1: f1(x)
Question 2: f2(x)
with the functions f1(x)
, f2(x)
displayed properly typeset (which "display" provides).
The nearest I've got is with matrices:
M = sympy.Matrix([["(a)",f1(x)],["(b)",f2(x)]])
display(M)
but this has two problems: (1) it shows the matrix delimiters, and (2) it strips the parentheses from "(a)" and "(b)".
So I'm still hoping for help: how to add a string to a line that uses "display". Thanks!
英文:
I have a number of mathematical objects created with SymPy, which display fine in Jupyter using "display". For example, there are a number of functions which I can display with:
display(f1(x))
display(f2(x))
and so on. But what I want is to prefix each line with some explanatory text, so the output is, for example:
Question 1: f1(x)
Question 2: f2(x)
with the functions f1(x), f2(x) displayed properly typeset (which "display" provides).
The nearest I've got is with matrices:
M = sympy.Matrix([["(a)",f1(x)],["(b)",f2(x)]])
display(M)
but this has two problems: (1) it shows the matrix delimiters, and (2) it strips the parentheses from "(a)" and "(b)".
So I'm still hoping for help: how to add a string to a line that uses "display". Thanks!
答案1
得分: 1
Option 1: 使用 Math()
我认为最简单的方法是结合使用 Math()
,就像这里以及相关源代码中所示。
import sympy as sym
from IPython.display import Math
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))
y = 2
z = 4
Math(rf'\text{{按1计算 }} {x}^{y} \text{{或按2计算 }} \frac{y}{z} \text{{或按其他方式计算}} {sym.latex(I)}')
在你的例子中,该方法略微转化为:
import sympy as sym
from IPython.display import Math
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))
y = 2
z = 4
display(Math(rf'\text{{问题1: }} {x}^{y}'))
display(Math(rf'\text{{问题2: }} {sym.latex(I)}'))
Option 2: 使用带有美元符号指定Latex的Markdown
你可以使用一个我在这里构建的示例的变体,其中包含一些结合Markdown和f-strings的sympy使用:
from IPython.display import Markdown, display
def printmd(string):
display(Markdown(string))
import sympy as sym
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))
equation = r'$\int_{t=0}^1 \frac{1}{1+t^2}\,\text{d}t$'
printmd('**方程式:**')
printmd(f'问题2: {equation}')
printmd('**结果:**')
printmd(f'问题1 $ = {sym.latex(I)}$')
printmd('**或者,结合起来:**')
printmd(f'问题3: '+equation[:-2] + f' = {sym.latex(I)}$$')
我看到这是这个StackOverflow帖子的变体。
英文:
Option 1: Use Math()
I think the easiest would be to combine in the use of Math()
, like here and sources therein.
import sympy as sym
from IPython.display import Math
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))
y = 2
z = 4
Math(rf'\text{{Press 1 to compute }} {x}^{y} \text{{or 2 to compute }} \frac{y}{z} \text{{or something else all togeher different to computer}} {sym.latex(I)}')
Related to that is this post and this post.
That approach somewhat more translated to your example, that would be:
import sympy as sym
from IPython.display import Math
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))
y = 2
z = 4
display(Math(rf'\text{{Question 1: }} {x}^{y}'))
display(Math(rf'\text{{Question 2: }} {sym.latex(I)}'))
Option 2: Use markdown with dollar signs to specify Latex
You can get very fancy with a variation on an example I built here that includes some sympy use combining markdown and f-strings:
from IPython.display import Markdown, display
def printmd(string):
display(Markdown(string))
import sympy as sym
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))
equation = r'$\int_{{t=0}}^1 \frac{{1}}{{1+t^2}}\,\text{d}t$'
printmd('**THE EQUATION:**')
printmd(f'Question 2: {equation}')
printmd('**THE RESULT:**')
printmd(f'Question 1 $ = {sym.latex(I)}$')
printmd('**OR, COMBINED:**')
printmd(f'Question 3: '+equation[:-2] + f' = {sym.latex(I)}$$')
I see that is a variation on this StackOverflow post.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论