# latex.table() function?

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

latex.table() function?

add a comment

2

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.

1

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}
```

0

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)
```

Asked: **
2011-12-31 03:40:09 -0500
**

Seen: **648 times**

Last updated: **Oct 06 '13**

Undefined control sequence when showing multiplication table

getting blank cells in html.table

latex form of product of non-commuting variables

How to get latex expression in exponential notation?

Multiline equation write form in text cell?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.