使用 Latex+symbols 在笔记本中 IPython.display 中串联

concatenation in IPython.display in a notebook using Latex+symbols

假设我有一个表达式,我想以 LateX 形式显示,还有一个表达式是分析计算的结果,其中 theta 之类的变量出现并最终打印出来。我想在一行中打印。举个例子:

from IPython.display import display, Math, Latex
import numpy as np
from sympy import *

init_printing()

# In[1]:
name='\Gamma^'+str(1)+'_{'+str(2)+str(3)+'}'+'='

# In[2]:
theta = symbols('theta')

# In[3]:
display(Math(name),theta)

最后一个命令以漂亮的形式 (LateX) 和 theta 打印 name。但是,添加了一个换行符,我想忽略它。如何实现?

您需要先形成乳胶字符串。使用 sympy.latex() 从任何可打印的 sympy 变量生成字符串:

display(Math(name + latex(theta)))

我还为此类输出编写了参数数量可变的简单函数。此处无需使用 latex() 包装变量:

def prlat(*args):
    str = ''
    for arg in args:
        str += latex(arg)
    display(Math(str))

prlat(name, theta, '= 0')