Ask Your Question
4

latex.table() function?

asked 13 years ago

Eugene gravatar image

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

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
3

answered 13 years ago

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.

Preview: (hide)
link
1

answered 11 years ago

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}
Preview: (hide)
link
0

answered 13 years ago

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)
Preview: (hide)
link

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: 13 years ago

Seen: 1,590 times

Last updated: Oct 06 '13