Ask Your Question

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

asked 2012-12-03 09:30:07 -0500

PatrickSurry gravatar image

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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-12-03 21:32:43 -0500

ndomes gravatar image

updated 2012-12-04 05:22:47 -0500

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 ]
edit flag offensive delete link 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>

PatrickSurry gravatar imagePatrickSurry ( 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.

ndomes gravatar imagendomes ( 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()

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

answered 2012-12-03 14:28:41 -0500

This doesn't look too bad:

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:

Here are the equations:
\sage{L[0].left_hand_side()} &= \sage{L[0].right_hand_side()} \\
\sage{L[1].left_hand_side()} &= \sage{L[1].right_hand_side()}

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

The patch at might also help.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-12-03 09:30:07 -0500

Seen: 1,071 times

Last updated: Dec 04 '12