Python API¶
Beyond the command line, clat is a small library. Import it to format LaTeX
source from your own scripts, editors, or build tools.
from clat import texfmt, load_config
config = load_config() # nearest .clat.toml, or defaults
result = texfmt(open("main.tex").read(), filename="main.tex", config=config)
print(result.text) # the formatted source
for rule, n_hits in result.clangs: # what was auto-fixed
print(f"clang: {rule.name} ({n_hits})")
for rule, fname, line, msg in result.clunks:
print(f"clunk: {fname}:{line}: {msg}")
If you omit config, texfmt uses the built-in defaults (threshold
5, every rule at its default weight). To run with a custom
threshold without a file on disk, build the dict yourself:
By default, texfmt runs fixable rules to a text fixed point, up to 5 sweeps.
Pass max_iter=1 for single-pass behaviour:
Formatting¶
clat.texfmt
¶
Apply formatting rules according to config.
Returns a ClatResult with the formatted text and categorised issues.
Fixable rules are applied repeatedly until a full sweep makes no text
changes, or until max_iter sweeps have run. Detect-only rules are then
evaluated once against the final text.
Source code in src/clat/rules.py
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 | |
clat.ClatResult
dataclass
¶
Result of running clat on a file.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str — the (possibly modified) source text
|
|
clangs |
list[tuple] — (rule, count) for auto-fixed rules above threshold
|
|
clunks |
list[tuple] — (rule, filename, line, msg) for unfixable issues above threshold
|
|
splats |
list[tuple] — (rule, filename, line, msg) for issues below threshold
|
|
iterations |
int — number of fixable-rule sweeps performed
|
|
converged |
bool — True if a sweep completed with no text changes
|
|
Source code in src/clat/rules.py
Configuration¶
clat.load_config
¶
Load config from .clat.toml or fallback locations.
Returns a dict with 'threshold' (int), 'weights' (dict[str, int]), 'protected_environments' (list[str]), and 'unprotected_rules' (list[str]).
Source code in src/clat/rules.py
clat.save_config
¶
Write config dict back to a .clat.toml file, preserving all settings.
Source code in src/clat/rules.py
clat.generate_default_config
¶
Return a .clat.toml string with all rules and their default weights.
Source code in src/clat/rules.py
The rule registry¶
clat.Rule
dataclass
¶
A single clat rule.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str — unique key, used in config overrides (e.g. 'labels_inline')
|
|
name |
str — human-readable description
|
|
fn |
callable — fix function f(text) -> text (fixable=True)
|
or warn function f(text, filename) -> [(file, line, msg)] |
weight |
int — default severity 1–10; 0 disables the rule
|
|
fixable |
bool — True if clat can auto-fix this
|
|
order |
int — execution order (lower = earlier); fixes run before warns
|
|
Source code in src/clat/rules.py
clat.RULES
module-attribute
¶
RULES = [Rule(1, 'labels_inline', 'Merge \\label onto the same line as \\section', rule1_labels_inline, weight=8, fixable=True, order=10), Rule(2, 'decorative_comments', 'Strip decorative comment separators (%%===, %%--- etc.)', rule7_strip_decorative_comments, weight=6, fixable=True, order=20), Rule(3, 'heading_spacing', 'Two blank lines before headings, none after', rule5_heading_spacing, weight=7, fixable=True, order=30), Rule(4, 'equation_separators', 'Insert % lines around display-math environments', rule2_equation_separators, weight=7, fixable=True, order=40), Rule(5, 'equation_punctuation', 'Add trailing comma or period to display equations', rule4_equation_punctuation, weight=6, fixable=True, order=50), Rule(6, 'float_indentation', 'Tab-indent content inside figure/table/list environments', rule6_figure_indentation, weight=5, fixable=True, order=60), Rule(7, 'one_sentence_per_line', 'Split sentences onto individual lines', rule3_one_sentence_per_line, weight=8, fixable=True, order=70), Rule(8, 'math_delimiters_inline', 'Replace \\(...\\) with $...$', rule8_math_delimiters_inline, weight=5, fixable=True, order=80), Rule(9, 'math_delimiters_display', 'Replace \\[...\\] with $$...$$', rule9_math_delimiters_display, weight=0, fixable=True, order=85), Rule(10, 'math_delimiters_equation', 'Replace \\[...\\] or $$...$$ with equation environment', rule10_math_delimiters_equation, weight=0, fixable=True, order=35), Rule(11, 'tilde_before_refs', 'Ensure non-breaking space before \\ref, \\cite etc.', rule11_tilde_before_refs, weight=7, fixable=True, order=90), Rule(12, 'number_unit_spacing', 'Normalise number-unit spacing (100\\,kN)', rule12_number_unit_spacing, weight=6, fixable=True, order=100), Rule(13, 'old_font_commands', 'Replace {\\bf text} with \\textbf{text} etc.', rule13_old_font_commands, weight=5, fixable=True, order=110), Rule(14, 'ellipsis', 'Replace ... with \\dots', rule14_ellipsis, weight=4, fixable=True, order=120), Rule(15, 'ordinal_suffixes', 'Convert superscript ordinals to plain text (1st, 2nd)', rule15_ordinal_suffixes, weight=8, fixable=True, order=130), Rule(16, 'table_line_endings', 'Table \\\\ on row line, \\hline/\\toprule on own line', rule16_table_line_endings, weight=7, fixable=True, order=140), Rule(17, 'abbreviation_spacing', 'Force interword space after e.g., i.e., et al.', rule17_abbreviation_spacing, weight=7, fixable=True, order=145), Rule(22, 'join_wrapped_lines', 'Join hard-wrapped lines so each sentence is one line', rule18_join_wrapped_lines, weight=8, fixable=True, order=65), Rule(18, 'long_file', 'Warn if file exceeds 2000 lines', warn_long_file, weight=3, fixable=False, order=200), Rule(19, 'hardcoded_refs', 'Detect "Figure 3" instead of \\cref{...}', warn_hardcoded_refs, weight=6, fixable=False, order=210), Rule(20, 'manual_sizing', 'Detect \\big, \\Big etc. (prefer \\left/\\right)', warn_manual_sizing, weight=3, fixable=False, order=220), Rule(21, 'float_after_heading', 'Detect float placed directly after a heading', warn_float_after_heading, weight=4, fixable=False, order=230)]
Multi-file discovery¶
For multi-file documents, clat.cli.discover_tex_files expands a list of root
files into the full, ordered, de-duplicated set of .tex files reachable
through \input/\include-style commands — the same traversal the -r flag
uses. See Multi-file documents.
clat.cli.discover_tex_files
¶
Return files plus recursively discovered LaTeX inputs/includes.
Roots are visited in the order provided. Dependencies are depth-first, de-duplicated, and resolved relative to the file that references them. Missing .tex dependencies are included in the returned list so the normal formatter path reports them as missing.