如何在ipython-notebook中获取sympy表达式的乳胶表? [英] How to get a latex table of sympy expressions in ipython-notebook?
问题描述
我正在使用sympy从多个表达式中收集术语,并希望将结果的格式(在ipython-notebook中)格式化为表格,其中术语位于最左侧一列,随后的每一列代表一个表达式.列中的条目来自sympy.collect(syms, evaluate=False)
I'm using sympy to collect terms from several expressions and would like to format the results (within ipython-notebook) in a table with the terms down the left most column and each subsequent column representing one expression. The entries in the column are from the dict
returned by sympy.collect(syms, evaluate=False)
到目前为止,我有:
from IPython.display import display, Latex
import pandas as pd
import sympy as sym
sym.init_printing()
x,y,z = sym.symbols('x,y,z')
da,db,dc = sym.symbols('{\Delta}a {\Delta}b {\Delta}c ' )
e_list = []
d_list = []
e_list.append(da*2*x + da*(y - 2) + db*3*z + dc*(x+y))
e_list.append(dc*z + dc*x + da*x + db*(z+2))
for e in e_list:
display(e.expand().collect((x,y,z)))
d_list.append(e.expand().collect((x,y,z),evaluate=False))
df = pd.DataFrame(d_list).T
数据帧显示为我想要的,除了条目位于原始乳胶中.
The dataframe displays as I would like except the entries are in raw latex.
我可以执行以下操作:
Latex(df.to_latex())
但是我所得到的只是一个被盒子包围的乳胶代码.
but all I get is the latex code surrounded by a box.
这似乎是ipython和Latex表的已知问题,请参见此处:
This seems to be a known problem with ipython and latex tables, see here:
I've got two different workarounds. Use unicode:
sym.init_printing(use_latex=False)
...
da,db,dc = sym.symbols('∆a ∆b ∆c' )
或将表格显示为sympy矩阵:
Or display the table as a sympy matrix:
terms = [x,y,z]
d_list = [(e.expand().collect((terms),evaluate=False)) for e in e_list]
mterms = sym.zeros(M.shape[0],len(terms)+1)
key1 = d_list[0].keys()[0]
terms.insert(0,key1)
for i in range(mterms.shape[0]):
for j in range(mterms.shape[1]):
try:
mterms[i,j] = d_list[i][terms[j]]
except:
mterms[i,j] = 0
mterms
推荐答案
毫无疑问,部分问题是您从df.to_latex()
中获得的乳胶实际上不是有效的乳胶.例如,如果仅获取该函数的输出并将其粘贴到专用的乳胶文档中,它将无法编译.
Undoubtedly, part of the problem is that the latex you get out of df.to_latex()
is not actually valid latex. For example, if you just take the output of that function and paste it into a dedicated latex document, it won't compile.
特别是,表中的某些条目包含诸如\Delta
之类的内容,但它们在数学环境之外(没有美元符号).
In particular, some of the entries in the table contain things like \Delta
, but these are outside of a math environment (no dollar signs).
所以这绝对是一个问题.另一个可能的问题是使用\toprule
等.它们是非标准的乳胶结构(尽管它们可以与正确的乳胶包装一起使用),但是我不知道这些rule
是否被<识别. c6>函数.
So that's definitely a problem. Another possible problem is the use of \toprule
, etc. Those are non-standard latex constructions (though they can be used with the right latex package), but I don't know if those rule
s are recognized by the Latex
function.
所有这些都说明,我什至无法获得最简单的tabular
环境来正确显示.例如,即使我这样做
All of that said, I can't even get the simplest tabular
environment to display properly. For example, even if I do
Latex(r"""\begin{tabular}{l} 1 \\ 2 \end{tabular}""")
正如您所说,我得到了盒装结果. (尽管该字符串的内容确实可以在专用的乳胶文档中正确编译.)
I get a boxed result, just as you say. (Though the contents of that string do compile properly in a dedicated latex document.)
这篇关于如何在ipython-notebook中获取sympy表达式的乳胶表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!