我正在尝试在Sympy中使用pprint()打印一个被我称为barphi的变量。我想要的是:
当作
我尝试...
但是它不起作用。需要帮助吗?提前感谢。
$\bar{\phi}$
当作
pprint(barphi)
打印时。我尝试...
barphy = Symbol('\bar{phi}')
但是它不起作用。需要帮助吗?提前感谢。
这个问题已经在SymPy邮件列表上回答了。
你写的代码有两个问题:
首先,Python会将字符串中的\
+ 字符解释为转义字符。你字符串中的\b
会被转换成退格符(详见https://en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart)。
你需要要么对\
进行转义,例如使用'\\bar{\\phi}$'
,或者更简单的方法是使用原始字符串,只需要在引号前面加上一个 r
,如r'\bar{\phi}'
。
其次,如果你想显示LaTeX,使用pprint()
是无法实现的(pprint
只能美化文本)。你应该使用init_printing()
来启用笔记本中的LaTeX打印功能。
phibar
,SymPy将自动将其呈现为\bar{\phi}
,如您在此处甚至可以在Unicode中看到的那样。In [11]: Symbol('phibar')
Out[11]: φ̅
In [2]: from sympy.printing.latex import latex, translate
In [3]: latex(translate('phibar'),mode='inline')
Out[3]: '$\\bar{\\phi}$'
Check for a modifier ending the string. If present, convert the
modifier to latex and translate the rest recursively.
Given a description of a Greek letter or other special character,
return the appropriate latex.
Let everything else pass as given.
>>> from sympy.printing.latex import translate
>>> translate('alphahatdotprime')
"{\\dot{\\hat{\\alpha}}}'"
init_printing()
。 - asmeurer