# Can I display a list of equations aligned like latex eqnarray?

I have several sage equations stored in a python list (for use in solve() etc). I'd like to display them one per line, aligned on equal signs if possible. This is in the notebook.

Most things I've tried - view(), show(), pretty_print() just print something that looks like a python list of formatted equations, all on one line (although the documentation for view() claims it will print each element of a list on a separate line, that doesn't seem to be the case).

The only things I've come up with are to explicitly loop, like: for eqn in eqns: view(eqn) or to pass eqns to html.table() which does one per line with alternating line shading.

edit retag close merge delete

Sort by » oldest newest most voted

You can use html.table placing left hand side, equal sign and right hand side in separate columns.

var('x y z alpha')
eqns = [x == y^2, z+alpha^3 == 0]
table = [ (eq.lhs(),'=',eq.rhs()) for eq in eqns ]
html.table(table)

more

That's a good idea. If you add a %html cell you can improve the styling: avoid the alternating grey rows, tighten up the spacing, and right-align the leftmost column. I used this: %html <style> table.table_form * tr.row-a { background: #ffffff; } table.table_form * tr.row-b { background: #ffffff; } table.table_form * td { padding-top: 5px; padding-bottom: 5px; padding-left: 2px; padding-right: 2px; } table.table_form tr td:first-child { text-align: right; } </style>

( 2012-12-03 23:50:28 -0500 )edit

You don't need a %html cell, I tried: html(""" <style> .row-a td:first-child {text-align:right;} .row-b td:first-child {text-align:right;} </style> """) inside a sage cell and it worked well.

( 2012-12-04 05:21:25 -0500 )edit

Makes sense. Tho I've started doing all my text as %html so I can use the <sage> tags and avoid the pre-formatted (fixed width) text I seem to get with html()

( 2012-12-04 15:46:22 -0500 )edit

sage: var('x y z alpha')
sage: L = [x == y^2, z+alpha^3 == 0]
view(L, viewer='pdf')


Alignment on equals signs is trickier, but the following works in the notebook. With L as above, create a cell containing this:

%latex
Here are the equations:
\begin{align*}
\sage{L[0].left_hand_side()} &= \sage{L[0].right_hand_side()} \\
\sage{L[1].left_hand_side()} &= \sage{L[1].right_hand_side()}
\end{align*}


There is probably a way to autogenerate this code in the notebook.

The patch at http://trac.sagemath.org/sage_trac/ti... might also help.

more