YouvenZ/Latex-table-python-tutorial
Create latex table from python using pandas and simple csv
๐ Exporting Pandas DataFrames to LaTeX with Style
This project demonstrates how to export pandas.DataFrame objects to LaTeX format with styling, captions, labels, and formatting. It's perfect for generating publication-quality tables for scientific papers, reports, or documentation.
๐ ๏ธ Requirements
pip install pandasA LaTeX distribution (like TeX Live or MikTeX) is required to compile
.texfiles into PDFs.
๐ Loading the Data
Make sure your CSV files are available in your working directory:
import pandas as pd
df_employee_data = pd.read_csv('employee_data.csv')
df_experiment_data = pd.read_csv('experiment_data.csv')
df_large_table_1 = pd.read_csv('large_table_1.csv')
df_large_table_2 = pd.read_csv('large_table_2.csv')๐ Exporting a Basic LaTeX Table
from your_module import save_latex_table
save_latex_table(
df_employee_data,
filename="table.tex",
float_format="%.2f",
caption="Employee Data",
label="tab:employee_data",
center=True
)๐จ Highlighting Specific Values in LaTeX
Apply custom formatting:
def highlight_high_values(val):
return "\\textbf{" + str(val) + "}" if val > 90 else str(val)
df_styled = df_experiment_data.style.format({
"Temperature (ยฐC)": highlight_high_values,
"Success Rate ($\\%$)": highlight_high_values
})Save styled table:
from your_module import save_latex_table_styled
save_latex_table_styled(
df_styled,
filename="styled_table.tex",
caption="Styled table with bolded values",
label="tab:styled"
)๐ Creating Multi-Column Tables
df_employee = pd.DataFrame({
"ID": [101, 102, 103],
"Name": ["Alice", "Bob", "Charlie"],
"Department": ["HR", "IT", "Finance"],
"Joining Date": ["2020-01-10", "2019-07-23", "2021-06-15"]
})
df_employee.columns = pd.MultiIndex.from_tuples([
("Employee Info", "ID"),
("Employee Info", "Name"),
("Work Details", "Department"),
("Work Details", "Joining Date"),
])
save_latex_table(
df_employee,
filename="employee_table.tex",
caption="Employee Details",
label="tab:employee"
)๐ฌ Controlling Float Precision
print(df_experiment_data.to_latex(float_format="%.1f"))
save_latex_table(
df_experiment_data,
filename="precision_table1.tex",
float_format="%.4f",
caption="Scientific Notation Example",
label="tab:precision_table1"
)
save_latex_table(
df_large_table_1,
filename="precision_table2.tex",
float_format="%.3f",
caption="Presentation of employee data.",
label="tab:data_emp"
)๐ Command Line Interface (CLI)
You can also export tables using the command line!
โถ๏ธ Usage
python export_table_cli.py \
--input employee_data.csv \
--output employee_table.tex \
--caption "Employee Overview" \
--label tab:employee \
--float_format "%.2f" \
--center \
--position h!๐ง CLI Options
| Option | Description | Required | Default |
|---|---|---|---|
--input |
Path to the input CSV file | โ | โ |
--output |
Output .tex filename |
โ | table.tex |
--caption |
Table caption | โ | "Table Caption" |
--label |
LaTeX reference label | โ | tab:label |
--float_format |
Format for float numbers | โ | "%.2f" |
--center |
Add \\centering for table alignment |
โ | False |
--position |
Float position (e.g., h!, t, b, etc.) |
โ | "h!" |
๐ File Output
All exports will generate:
- A standalone
.texfile (compilable with LaTeX) - Includes required packages (
booktabs,longtable,graphicx,adjustbox) - Centered tables with optional float formatting
To compile:
pdflatex table.texโจ Advanced Ideas to Extend
- Add support for longtables
- Wrap wide tables using
adjustbox - Auto-compile to PDF or image format
- Combine multiple tables into one report
- Highlight using
Styler.applymap()orhighlight_max() - Create a Python package (
pip install latex-table-exporter)
๐ License
MIT License โ Free to use, modify, and distribute.
Let me know if you'd like the CLI script bundled in a module with installable CLI support via setup.py or pyproject.toml.