pyltk/pyltk/table.py

63 lines
1.5 KiB
Python

# -*- encoding: utf-8 -*-
from .element import element
from .tabular import tabulardf
class table(element):
""" Class representing a latex figure. """
templates = {
"caption": "\\caption{{{content}}}",
"content": """\\begin{{table}}[{options}]
{centered}
{inner}
{caption}{label}
\\end{{table}}""",
}
autolabel = False
def __init__(
self,
childrens=None,
parent=None,
caption=None,
options="!ht",
centered=True,
label=None,
):
""" Create a table with given options.
Parameters:
- options Options for the table.
- centered Set to true to add a centering command.
"""
super().__init__(parent, childrens, label=label)
self.options = options
self.caption = caption
self.centered = ""
if centered:
self.centered = "\\centering"
def content(self):
caption = ""
if self.caption:
caption = self.fmt().format("caption", {"content": self.caption})
return self.fmt().format(
"content",
{
"options": self.options,
"centered": self.centered,
"caption": caption,
"label": self.format_label(self.label),
"inner": super().content(),
},
)
class tabledf(table):
def __init__(self, df, header=None, multilevels=False, fmt=None, **kargs):
super().__init__(**kargs)
self.add(tabulardf(df, header, multilevels, fmt))