Skip to main content

Python library for rendering latex tables in an easy way

Project description

etabs

Python library for building and rendering latex tables in an easy way.

Instalation

pip install etabs

Simple example

Code:

from etabs import RuleType, TexTable

# Example values
values = [
    [60, 20, 20],
    [50, 40, 10],
    [30, 10, 60],
    [5, 90, 5],
]

row_labels = ["A", "B", "C", "D"]
col_labels = ["C1", "C2", "C3"]

# TexTabel is the main structure for creating tables
table = TexTable(centered=True, caption="Example table")

# Add rows
table.add_row(None, *col_labels) # First value empty, rest are column labels
for label, row in zip(row_labels, values):
    table.add_row(label, *row)

# You can optionally add rules at specific rows
table.add_rule(0, RuleType.TOP)
table.add_rule(1)  # By default, the rule is a midrule

# If no row is specified, it will be added at the end
table.add_rule(rule_type=RuleType.BOTTOM)

# Finally to get the latex code use the render method
print(table.render())

Output:

\begin{figure}[h!]
    \centering
    \caption{Example table}
    \vspace{0.5em}
    \begin{tabular}{cccc}
        \toprule
         & C1 & C2 & C3 \\
        \midrule
        A & 60 & 20 & 20 \\
        B & 50 & 40 & 10 \\
        C & 30 & 10 & 60 \\
        D & 5 & 90 & 5 \\
        \bottomrule
    \end{tabular}
\end{figure}

Result:

simple

A little more styled example

from etabs import RuleType, TexTable

# Example values
values = [
    [60, 20, 20],
    [50, 40, 10],
    [30, 10, 60],
    [5, 90, 5],
]

col_labels = ["C1", "C2", "C3"]

table = TexTable(centered=True, caption="Example table")

# Add values
table.add_row(*col_labels, start=2)  # Colum 0 and 1 are empty
for row in values:
    # Preprocess each value to show as percentage
    table.add_row(*row, prep=lambda x: f"{x} \\%", start=2)

# Add a vertical separator before column 2
table.seps[2] = "|"

# Assign values directly
table[1, 1] = table[3, 1] = "A1"
table[2, 1] = table[4, 1] = "B2"

# Merge cells using slices
table[0, 0:2] = "Types"
table[1:3, 0] = "Typa A"
table[3:, 0] = "Typa B"

# Add some style using slices too
table[0, :].set_bold(True)
table[:, 0:2].set_bold(True)
table[1:, 0:2].set_italic(True)

# Add rules
table.add_rule(0, RuleType.TOP)
table.add_rule(1)
table.add_rule(rule_type=RuleType.BOTTOM)

print(table.render())

Output:

\begin{figure}[h!]
    \centering
    \caption{Example table}
    \vspace{0.5em}
    \begin{tabular}{cc|ccc}
        \toprule
        \multicolumn{2}{c|}{\textbf{Types}} & \textbf{C1} & \textbf{C2} & \textbf{C3} \\
        \midrule
        \multirow{2}{*}{\textit{\textbf{Typa A}}} & \textit{\textbf{A1}} & 60 \% & 20 \% & 20 \% \\
         & \textit{\textbf{B2}} & 50 \% & 40 \% & 10 \% \\
        \multirow{2}{*}{\textit{\textbf{Typa B}}} & \textit{\textbf{A1}} & 30 \% & 10 \% & 60 \% \\
         & \textit{\textbf{B2}} & 5 \% & 90 \% & 5 \% \\
        \bottomrule
    \end{tabular}
\end{figure}

Result:

simple

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

etabs-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

etabs-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file etabs-0.1.0.tar.gz.

File metadata

  • Download URL: etabs-0.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.6

File hashes

Hashes for etabs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9bff431359cde33fc75a5dd570ea1e7a349ba528080cce61c0dc98d188492499
MD5 e22225f811f2a81168fd3f3e7a1b6115
BLAKE2b-256 0316b17ebc68aa004356506570b821e39132767d514476acad24ff3a3fcc4932

See more details on using hashes here.

File details

Details for the file etabs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: etabs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.6

File hashes

Hashes for etabs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a486c7f2bc4d372303566f8eecb2d59064e64048d906aeab110cd12b43c8abb
MD5 953118a0b60c8867b2b45bf01e4ee53e
BLAKE2b-256 276866acab5dabc21c8d4919f9df0e26c8599df08c1d6ce90e16cdb4ffbaf71f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page