from keecas.display import format_decimal_numbers
# Basic usage with shorthand notation
latex = r"\sigma_{Sd} = 1.23456 \text{ MPa}"
formatted = format_decimal_numbers(latex, ".2f")
print(formatted)\sigma_{Sd} = 1.23 \text{ MPa}
Format all decimal numbers in a LaTeX string with specified precision.
Searches for decimal numbers in LaTeX strings and applies Python format specifications to control precision and display. Used internally by show_eqn() for cell-level formatting but available for custom LaTeX string manipulation.
NOTE: Only matches standard decimal notation (e.g., “1.234”, “-0.567”). Does not match scientific notation or integers without decimal points.
| Name | Type | Description | Default |
|---|---|---|---|
| text | str | None |
LaTeX string containing decimal numbers to format. If None, returns None unchanged. | required |
| format_string | str | None |
Python format specification for float formatting. Supports flexible notation: - “.3f” (shorthand, common usage) - “{:.3f}” (full format string) - “:0.3f” (with zero-padding) If None, returns text unchanged. Defaults to None. | None |
| Name | Type | Description |
|---|---|---|
str | None |
LaTeX string with all decimal numbers formatted according to | |
str | None |
format_string, or None if text is None. |
| Name | Type | Description |
|---|---|---|
ValueError |
If format_string is invalid or cannot format floats. |
\sigma_{Sd} = 1.23 \text{ MPa}
F = 100.6 \text{ kN}, A_{load} = 20.1 \text{ cm}^2
\alpha_{max} = 3.142
\alpha_{max} = 3.14e+00
\alpha_{max} = 003.14
# Integration with show_eqn workflow
from keecas import symbols, u, show_eqn
from sympy import latex
# tip: use for custom post-processing of LaTeX strings
sigma_Rd = symbols(r"\sigma_{Rd}")
value_latex = latex(5.123456 * u.MPa)
# Format specific parts before display
formatted_latex = format_decimal_numbers(value_latex, ".2f")
print(formatted_latex)\mathtt{\text{5.12 MPa}}
show_eqn: Main display function with built-in float formattingDisplayConfig: Display configuration (see default_float_format attribute)