Ask Your Question
4

latex.table() function?

asked 2011-12-31 10:40:09 +0200

Eugene gravatar image

Can Sage construct latex code of table? Something like html.table, but with tex-output instead of html-output.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2011-12-31 12:18:01 +0200

Eugene gravatar image

Well... I could not wait for response, so I wrote a simple implementation:

def latex_table(content, caption, label, header_rows = 1, header_columns = 0, position = 'htb'):
    code = """\\begin{table}[%s]
    \\centering
    \\caption{\\label{tab:%s} %s}
    """ % (position, label, caption)    
    code += '\\begin{tabular}{|%s|} \\hline\n' % '|'.join('c' for i in range(len(content[0])))    
    bold_format = '\\textbf{%s}'

    for line_no, line in enumerate(content):
        row_format_string = bold_format if line_no < header_rows else '%s'
        code += '        ' + ' & '.join([(bold_format if column_no < header_columns else row_format_string ) % i for column_no,i in enumerate(line)]) + ' \\\\ \\hline \n'    
    code += '    \\end{tabular}\n\end{table}\n'    
    return code

This code generates something similar to html.table output for latex. I'll be happy to know someone already wrote a better implementation.

edit flag offensive delete link more
1

answered 2013-10-06 17:43:51 +0200

Eviatar Bach gravatar image

Note that now Sage has a table object which can be printed as LaTeX. As per the example in the documentation:

  sage: rows = [['a', 'b', 'c'], [100,2,3], [4,5,60]]
  sage: table(rows)
    a     b   c
    100   2   3
    4     5   60
  sage: latex(table(rows))
  begin{tabular}{lll}
  a & b & c 
  $100$ & $2$ & $3$ 
  $4$ & $5$ & $60$ 
  end{tabular}
edit flag offensive delete link more
0

answered 2012-01-03 01:06:59 +0200

Jason Grout gravatar image

If you can put it in a matrix m, then latex(m) will give roughly a latex table.

sage: var('x,y,z')
(x, y, z)
sage: latex(matrix([[x*y,x+y,z],[z+y,x-y,x*y*z]]))
\left(\begin{array}{rrr}
x y & x + y & z \\
y + z & x - y & x y z
\end{array}\right)
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

Stats

Asked: 2011-12-31 10:40:09 +0200

Seen: 1,423 times

Last updated: Oct 06 '13