2014-04-03 19 views
2

做一個花式打印我想在Sympy中打印一個變量,我稱之爲barphi。我想得到的是如何在ipython中使用sympy.pprint()

$\bar{\phi}$ 

當打印爲pprint(barphi)

我嘗試

barphy = Symbol('\bar{phi}') 

,但它不工作。任何幫助?提前致謝。

回答

2

這回答在SymPy mailing list

有你寫什麼

前兩個問題,Python的轉換\ +字符的字符串作爲逃避。字符串中的\b成爲退格(請參閱https://en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart)。

你需要或者逃避\,即使用'\\bar{\\phi}$',或者容易得多,如果你不關心轉義,使用原始的字符串,這只是意味着把一個r在報價前,像r'\bar{\phi}'。第二,如果你想得到乳膠,pprint()不會這樣做(pprint漂亮的打印到二維文字)。您應該使用init_printing()在筆記本中啓用LaTeX打印。

最後,在郵件列表中所指出的朱利安RIOUX,你可以將該元件命名爲phibar,並SymPy會自動呈現爲\bar{\phi},因爲你可以在這裏看到,即使在Unicode的

In [11]: Symbol('phibar') 
Out[11]: φ̅ 
+0

非常感謝你@asmeurer你的答案。但是在我的系統中沒有像在你的系統中那樣工作,例如我沒有在phi中獲得phi [12]:pprint(Symbol('phibar')) phibar In [13]:pprint ('phi')) φ – JuanM

+0

您需要在支持Unicode的環境中執行此操作,包括組合字符。不要忘記運行'init_printing()'。 – asmeurer

1

如果你還想要得到的乳膠代碼,而不是打印出來,你可以這樣做:

In [2]: from sympy.printing.latex import latex, translate 

In [3]: latex(translate('phibar'),mode='inline') 
Out[3]: '$\\bar{\\phi}$' 

可以看到文檔乳膠功能here

文檔翻譯功能是

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}}}'"