Cell Formatting
Format individual cells with bold, italic, colors, and other LaTeX styling.
The Cell Class
The Cell class wraps content with formatting options:
from texer import Table, Tabular, Row, Cell, evaluate
table = Tabular(
columns="lcc",
header=Row("Name", "Value", "Status"),
rows=[
Row(
Cell("Important", bold=True),
Cell("42", italic=True),
Cell("Active", bold=True, italic=True),
),
],
toprule=True,
bottomrule=True,
)
print(evaluate(table, {}))
Bold Text
In a table:
Italic Text
Bold + Italic
With Dynamic Data
Use Cell with Ref and Iter:
from texer import Tabular, Row, Cell, Ref, Iter, Format, evaluate
table = Tabular(
columns="lcc",
header=Row(
Cell("Experiment", bold=True),
Cell("Result", bold=True),
Cell("P-value", bold=True),
),
rows=Iter(
Ref("experiments"),
template=Row(
Cell(Ref("name"), italic=True),
Format(Ref("result"), ".3f"),
Format(Ref("pvalue"), ".4f"),
)
),
toprule=True,
bottomrule=True,
)
data = {
"experiments": [
{"name": "Trial A", "result": 3.142, "pvalue": 0.0234},
{"name": "Trial B", "result": 2.718, "pvalue": 0.0012},
]
}
print(evaluate(table, data))
Conditional Formatting
Apply formatting based on conditions:
from texer import Cond, Cell, Ref, Iter
rows=Iter(
Ref("results"),
template=Row(
Ref("name"),
Ref("score"),
# Bold if score >= 90
Cond(
Ref("score") >= 90,
Cell(Ref("grade"), bold=True),
Ref("grade"),
),
)
)
Text Colors
Use LaTeX \textcolor directly:
from texer import Cell
# Basic color
Cell(r"\textcolor{red}{Error}")
# With standard colors
Cell(r"\textcolor{blue}{Information}")
Cell(r"\textcolor{green}{Success}")
Cell(r"\textcolor{red}{Failed}")
Color Package
Requires \usepackage{xcolor} in your LaTeX document.
Colored Cells with Conditions
from texer import Cond, Ref, Iter, Row
rows=Iter(
Ref("tests"),
template=Row(
Ref("name"),
Ref("status"),
Cond(
Ref("passed"),
r"\textcolor{green}{\checkmark}",
r"\textcolor{red}{\times}",
),
)
)
data = {
"tests": [
{"name": "Unit Tests", "status": "Complete", "passed": True},
{"name": "Integration", "status": "Failed", "passed": False},
]
}
Alignment within Cells
Use \multicolumn for cell-specific alignment:
from texer import MultiColumn
Row(
"Left aligned",
MultiColumn(1, "r", "Right aligned"), # Right align this cell
"Default alignment",
)
Background Colors
Use \cellcolor for cell backgrounds:
Colortbl Package
Requires \usepackage{colortbl} in your LaTeX document.
Font Sizes
from texer import Cell
Cell(r"\small Small text")
Cell(r"\large Large text")
Cell(r"\Large Even larger")
Cell(r"\tiny Tiny text")
Mathematical Content
Combining Formats
Stack multiple formatting options:
from texer import Cell
# Bold italic colored text
Cell(r"\textcolor{blue}{\textbf{\textit{Important Note}}}")
# Or with Cell attributes
Cell(
r"\textcolor{blue}{Important}",
bold=True,
italic=True,
)
Number Formatting
Use Format with Cell:
from texer import Cell, Format, Ref
Row(
Ref("name"),
Cell(Format(Ref("value"), ".2f"), bold=True), # Bold formatted number
Format(Ref("percent"), ".1%"), # Percentage
)
Advanced Number Formatting with FormatNumber
FormatNumber provides enhanced number formatting with support for:
- Significant figures
- Fixed decimal places
- Thousands separators
- Automatic handling of -0.00 (removes minus sign)
- String passthrough
from texer import FormatNumber, Ref, Iter, Row, Tabular, evaluate
# Significant figures
FormatNumber(Ref("value"), sig=2) # 1.234 -> "1.2"
# Fixed decimal places
FormatNumber(Ref("value"), decimals=2) # 1.234 -> "1.23"
# Thousands separator
FormatNumber(Ref("value"), thousands_sep=True) # 2000 -> "2,000"
# Custom separator
FormatNumber(Ref("value"), thousands_sep=" ") # 2000 -> "2 000"
# Combine decimals with thousands separator
FormatNumber(Ref("value"), decimals=2, thousands_sep=True) # 1234.567 -> "1,234.57"
Handling -0.00: By default, FormatNumber removes the minus sign from negative zero:
# value = -0.001
FormatNumber(Ref("value"), decimals=2) # "-0.001" -> "0.00" (not "-0.00")
# Disable this behavior if needed
FormatNumber(Ref("value"), decimals=2, strip_negative_zero=False) # -> "-0.00"
Complete example with financial data:
from texer import Tabular, Row, Ref, Iter, FormatNumber, evaluate
table = Tabular(
columns="lrrr",
header=Row("Item", "Amount (\\$)", "Count", "Rate (\\%)"),
rows=Iter(
Ref("transactions"),
template=Row(
Ref("item"),
FormatNumber(Ref("amount"), decimals=2, thousands_sep=True),
FormatNumber(Ref("count"), thousands_sep=True),
FormatNumber(Ref("rate"), decimals=1),
),
),
toprule=True,
midrule=True,
bottomrule=True,
)
data = {
"transactions": [
{"item": "Revenue", "amount": 1234567.89, "count": 15000, "rate": 3.5},
{"item": "Costs", "amount": 987654.32, "count": 12500, "rate": 2.8},
{"item": "Profit", "amount": 246913.57, "count": 2500, "rate": 0.7},
]
}
print(evaluate(table, data))
# Outputs:
# Revenue & 1,234,567.89 & 15,000 & 3.5 \\
# Costs & 987,654.32 & 12,500 & 2.8 \\
# Profit & 246,913.57 & 2,500 & 0.7 \\
Complete Example
from texer import Table, Tabular, Row, Cell, Ref, Iter, Format, Cond, evaluate
table = Table(
Tabular(
columns="llccc",
header=Row(
Cell("Student", bold=True),
Cell("Assignment", bold=True),
Cell("Score", bold=True),
Cell("Grade", bold=True),
Cell("Status", bold=True),
),
rows=Iter(
Ref("submissions"),
template=Row(
Cell(Ref("student"), italic=True),
Ref("assignment"),
Format(Ref("score"), ".1f"),
# Conditional bold for high grades
Cond(
Ref("score") >= 90,
Cell(Ref("letter_grade"), bold=True),
Ref("letter_grade"),
),
# Colored status
Cond(
Ref("passed"),
r"\textcolor{green}{PASS}",
r"\textcolor{red}{FAIL}",
),
)
),
toprule=True,
bottomrule=True,
),
caption="Student Grades",
label="tab:grades",
)
data = {
"submissions": [
{
"student": "Alice Johnson",
"assignment": "HW1",
"score": 95.5,
"letter_grade": "A",
"passed": True,
},
{
"student": "Bob Smith",
"assignment": "HW1",
"score": 67.3,
"letter_grade": "D",
"passed": False,
},
{
"student": "Charlie Brown",
"assignment": "HW1",
"score": 88.9,
"letter_grade": "B",
"passed": True,
},
]
}
print(evaluate(table, data))
LaTeX Special Characters
Tables do not auto-escape LaTeX special characters. You're expected to write valid LaTeX directly using raw strings:
# Write LaTeX directly with raw strings
Row("Formula", r"$x^2$") # Renders as: Formula & $x^2$ \\
# For literal special characters, escape them yourself
Row("Price", r"\$100") # Renders as: Price & \$100 \\
Row("Percent", r"50\%") # Renders as: Percent & 50\% \\
Raw Strings
Use Python raw strings (r"...") to avoid escaping backslashes.
Custom LaTeX Commands
Use raw strings for custom LaTeX commands:
from texer import Cell
# Custom command
Cell(r"\mycustomcommand{value}")
# With conditionals
Cond(
Ref("is_important"),
r"\important{Important!}",
Ref("text"),
)
Tips
- Keep it simple: Use
Cell(text, bold=True)for common cases - Use raw strings: Write
r"\textbf{bold}"instead of"\\textbf{bold}" - Escape special chars: For literal
$,%,&,_,#, use\$,\%,\&,\_,\# - Test colors: Different LaTeX distributions may support different colors
Next Steps
- Advanced Features - Multi-column and multi-row cells
- Data-Driven Tables - Combine formatting with dynamic data
- Raw Spec - Complete Raw specification