Mathjax 本身没有table
/tabular
环境,但仍然可以为此使用array
环境(link1 ; link2 ; link3)。
因此,基于此,我编写了以下格式化函数fmt_table
,称为格式化您提到的公式:
from sympy import Ynm, simplify, symbols, latex
theta, phi = symbols('\Theta \phi')
from IPython.display import HTML, display, Math, Latex
def fmt_table(data,center_data=False,add_row_nums=False):
from math import ceil
buf='''
\\newcommand\\T{\\Rule{0pt}{1em}{.3em}}
\\begin{array}{%s}
'''
max_cols = max(len(r) for r in data)
column_spec = '|' + '|'.join(['c']*max_cols) + '|'
buf = buf % column_spec
row_idx = 0
for row_data in data:
row = ''
if add_row_nums and row_idx > 0:
row += str(row_idx) + " & "
if center_data and row_idx > 0:
to_add = ceil( (max_cols - len(row_data))/2 )
row += ' & '.join([''] * to_add)
row += ' & '.join(row_data)
if row_idx == 0:
row = '''\\hline''' + row + '''\\\\\hline'''
else:
row += '''\\\\\hline'''
row += "\n"
buf +=row
row_idx += 1
buf += '''\\end{array}'''
# print(buf)
return buf
tbl = []
tbl.append(['\\texttt{m/l}','-2','-1','0','1','2'])
for l in range (0, 3):
new_row = []
for m in range(-l, l+1):
h = simplify(Ynm(l, m, theta, phi).expand(func=True))
new_row.append(latex(h))
tbl.append(new_row)
display(Math(fmt_table(tbl,add_row_nums=True,center_data=True)))
输出:

此答案中的所有代码也可在此处获得。