Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Render Math Table Properly in IPython Notebook

The math problem that I'm solving gives different analytical solutions in different scenarios, and I would like to summarize the result in a nice table. IPython Notebook renders the list nicely: for example:

import sympy
from pandas import DataFrame
from sympy import *
init_printing()
a, b, c, d = symbols('a b c d')
t = [[a/b, b/a], [c/d, d/c]]
t

enter image description here

However, when I summarize the answers into a table using DataFrame, the math cannot be rendered any more:

df = DataFrame(t, index=['Situation 1', 'Situation 2'], columns=['Answer1','Answer2'])
df

enter image description here

"print df.to_latex()" also gives the same result. I also tried "print(latex(t))" but it gives this after compiling in LaTex, which is alright, but I still need to manually convert it to a table:

enter image description here

How should I use DataFrame properly in order to render the math properly? Or is there any other way to export the math result into a table in Latex? Thanks!

Update: 01/25/14 Thanks again to @Jakob for solving the problem. It works perfectly for simple matrices, though there are still some minor problems for more complicated math expressions. But I guess like @asmeurer said, perfection requires an update in IPython and Pandas.

enter image description here

Update: 01/26/14 If I render the result directly, i.e. just print the list, it works fine: enter image description here

like image 267
Titanic Avatar asked Jan 22 '14 20:01

Titanic


1 Answers

MathJax is currently not able to render tables, hence the most obvious approach (pure latex) does not work.

However, following the advise of @asmeurer you should use an html table and render the cell content as latex. In your case this could be easily achieved by the following intermediate step:

from sympy import latex
tl = map(lambda tc: '$'+latex(tc)+'$',t)
df = DataFrame(tl, index=['Situation 1', 'Situation 2'], columns=['Answer'])
df

which gives:
enter image description here


Update:

In case of two dimensional data, the simple map function will not work directly. To cope with this situation the numpy shape, reshape and ravel functions could be used like:

import numpy as np
t = [[a/b, b/a],[a*a,b*b]]
tl=np.reshape(map(lambda tc: '$'+latex(tc)+'$',np.ravel(t)),np.shape(t))
df = DataFrame(tl, index=['Situation 1', 'Situation 2'], columns=['Answer 1','Answer 2'])
df

This gives:
enter image description here


Update 2:

Pandas crops cell content if the string length exceeds a certain number. E.g a more complicated expression like

t1 = [a/2+b/2+c/2+d/2]
tl=np.reshape(map(lambda tc: '$'+latex(tc)+'$',np.ravel(t1)),np.shape(t1))
df = DataFrame(tl, index=['Situation 1'], columns=['Answer 1'])
df

gives:
enter image description here

To cope with this issue a pandas package option has to be altered, for details see here. For the present case the max_colwidth has to be changed. The default value is 50, hence let's change it to 100:

import pandas as pd
pd.options.display.max_colwidth=100
df

gives:
enter image description here

like image 85
Jakob Avatar answered Oct 27 '22 20:10

Jakob