| name | latex |
| description | Generate LaTeX documents and compile them to PDF for publication-quality reports. Use when creating academic papers, scientific reports, mathematical documents, or professionally typeset PDF reports with equations and references. |
LaTeX
Provides patterns for generating LaTeX documents for report generation.
Basic Document Structure
def create_latex_document(
title: str,
author: str,
content: str,
document_class: str = "article"
) -> str:
"""
Create a basic LaTeX document.
Usage:
doc = create_latex_document(
title="Quarterly Report",
author="Finance Team",
content="\\section{Summary}\\nReport content here..."
)
"""
return f'''\\documentclass{{{document_class}}}
\\usepackage[utf8]{{inputenc}}
\\usepackage{{graphicx}}
\\usepackage{{booktabs}}
\\usepackage{{hyperref}}
\\title{{{title}}}
\\author{{{author}}}
\\date{{\\today}}
\\begin{{document}}
\\maketitle
{content}
\\end{{document}}
'''
Sections and Formatting
def latex_section(title: str, content: str) -> str:
"""Create a section."""
return f"\\section{{{title}}}\n{content}\n"
def latex_subsection(title: str, content: str) -> str:
"""Create a subsection."""
return f"\\subsection{{{title}}}\n{content}\n"
def latex_bold(text: str) -> str:
"""Bold text."""
return f"\\textbf{{{text}}}"
def latex_italic(text: str) -> str:
"""Italic text."""
return f"\\textit{{{text}}}"
def latex_list(items: list[str], numbered: bool = False) -> str:
"""
Create a list.
Usage:
latex_list(["Item 1", "Item 2", "Item 3"])
"""
env = "enumerate" if numbered else "itemize"
items_str = "\n".join(f" \\item {item}" for item in items)
return f"\\begin{{{env}}}\n{items_str}\n\\end{{{env}}}"
Tables
def latex_table(
headers: list[str],
rows: list[list],
caption: str = None,
label: str = None
) -> str:
"""
Create a LaTeX table.
Usage:
table = latex_table(
headers=["Name", "Amount", "Date"],
rows=[
["Invoice 1", "$100.00", "2024-01-15"],
["Invoice 2", "$250.00", "2024-01-20"]
],
caption="Invoice Summary"
)
"""
cols = "l" * len(headers)
header_row = " & ".join(headers)
data_rows = "\n".join(" & ".join(str(cell) for cell in row) + " \\\\" for row in rows)
table = f'''\\begin{{table}}[h]
\\centering
\\begin{{tabular}}{{{cols}}}
\\toprule
{header_row} \\\\
\\midrule
{data_rows}
\\bottomrule
\\end{{tabular}}'''
if caption:
table += f"\n\\caption{{{caption}}}"
if label:
table += f"\n\\label{{{label}}}"
table += "\n\\end{table}"
return table
def dict_to_latex_table(data: list[dict], caption: str = None) -> str:
"""Convert list of dictionaries to LaTeX table."""
if not data:
return ""
headers = list(data[0].keys())
rows = [[row.get(h, "") for h in headers] for row in data]
return latex_table(headers, rows, caption)
Figures
def latex_figure(
image_path: str,
caption: str = "",
label: str = "",
width: str = "0.8\\textwidth"
) -> str:
"""
Include a figure.
Usage:
fig = latex_figure(
image_path="charts/sales.png",
caption="Sales Trends",
label="fig:sales"
)
"""
return f'''\\begin{{figure}}[h]
\\centering
\\includegraphics[width={width}]{{{image_path}}}
\\caption{{{caption}}}
\\label{{{label}}}
\\end{{figure}}'''
Mathematical Equations
def latex_equation(equation: str, label: str = None) -> str:
"""
Create a numbered equation.
Usage:
eq = latex_equation("E = mc^2", label="eq:einstein")
"""
if label:
return f"\\begin{{equation}}\n{equation}\n\\label{{{label}}}\n\\end{{equation}}"
return f"\\begin{{equation}}\n{equation}\n\\end{{equation}}"
def latex_inline_math(expression: str) -> str:
"""Create inline math."""
return f"${expression}$"
Report Template
def create_report_template(
title: str,
author: str,
sections: list[dict]
) -> str:
"""
Create a full report template.
Usage:
report = create_report_template(
title="Q4 Financial Report",
author="Finance Department",
sections=[
{"title": "Executive Summary", "content": "..."},
{"title": "Financial Analysis", "content": "..."}
]
)
"""
sections_content = "\n\n".join(
latex_section(s["title"], s["content"])
for s in sections
)
return create_latex_document(title, author, sections_content)
Compiling LaTeX
import subprocess
from pathlib import Path
def compile_latex_to_pdf(tex_file: str, output_dir: str = None) -> str:
"""
Compile LaTeX file to PDF using pdflatex.
Returns path to generated PDF.
Usage:
pdf_path = compile_latex_to_pdf("report.tex")
"""
tex_path = Path(tex_file)
output_dir = output_dir or str(tex_path.parent)
result = subprocess.run(
["pdflatex", "-output-directory", output_dir, str(tex_path)],
capture_output=True,
text=True
)
if result.returncode != 0:
raise RuntimeError(f"LaTeX compilation failed: {result.stderr}")
return str(Path(output_dir) / tex_path.stem) + ".pdf"
Helper Script
Use helper.py for the LaTeXGenerator class with comprehensive document generation features.