Latex formula extraction

From XuShuwenn/RedAct · View interactive version

Latex formula extraction is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Extract LaTeX formulas from PDF research papers, fixing display syntax errors while preserving original content.

How to use this skill

Skill file

LaTeX Formula Extraction from PDF

When to Use

Execution Priorities

Input

Runtime Checks

Extraction Steps

  1. Review the PDF page by page and identify every standalone display formula candidate, including unnumbered and multi-line formulas.

1a. Keep an explicit page inventory while scanning: for each page, record whether it has zero or more standalone display formulas and whether the page evidence was readable. Do not infer completeness from grep, equation numbering, regex hits, or a single flattened text dump.

1b. For each retained formula, record a stable source reference from the current run: page number plus the text extraction, OCR result, or crop/region that actually showed it.

1c. If a page read, crop, render, or OCR result is empty, wrong-region, unreadable, or truncated, treat that as insufficient evidence and switch methods rather than transcribing from memory, context, or prior guesses.

  1. Extract each formula as raw LaTeX in $$ ... $$.
  2. Preserve the exact displayed content from the PDF only when symbols and structure are clearly supported by readable extraction output or direct page inspection.

3a. Apply an evidence gate before keeping any formula: only write it if a specific page/region shows the full displayed expression in readable text extraction, OCR, or direct visual inspection.

3b. Do not identify formulas by symbol density, line length, centering, equation-number regexes, or similar heuristics unless a human-verifiable page view already confirmed those lines are actual display equations.

3c. If extraction is partial, fragmented, page-misaligned, truncated, merged with prose, drops superscripts/subscripts/operators, shows Unicode fragments/OCR noise, reorders symbols, breaks fractions, or is otherwise ambiguous, treat it only as a lead for where to inspect next; re-extract, switch methods, or omit the formula rather than reconstructing missing math from context.

  1. Remove equation tags, commas, and periods at the end of formulas only when they are not part of the math content.
  2. Check each extracted formula for syntax/display issues only after the displayed formula has been reliably identified from the PDF.
  3. Append corrected versions only for formulas with clear syntax/display problems.

6a. Before concluding that no fixes are needed, run an explicit formula-by-formula validation pass for delimiter balance and obvious LaTeX display syntax problems; bracket counting or successful rendering alone does not prove fidelity or completeness.

  1. If output is blank, truncated, garbled, fragmented, or mixes prose into equations, switch methods instead of inferring missing math.

7a. If a tool fails twice for the same structural reason (for example timeout, missing dependency, unreadable output, flattened math, or no inspectable files), stop retrying it and pivot to a materially different method.

7b. Do not keep rerunning near-identical converter or command variants, adding long wait loops, or primarily polling ps/pgrep/output directories once a method has not produced usable text quickly.

7c. Prefer direct page-level extraction, page rendering plus visual inspection, OCR, or manual transcription from verified page regions over continued process management.

7d. If delegation is used, require the subtask to try a materially different extraction path rather than retrying the same failing workflow.

  1. As soon as you have a workable draft, write /root/latex_formula_extraction.md, then verify the file exists and that each formula is in its own $$...$$ block.
  2. After writing, reopen and read back the saved file contents in manageable chunks if needed; confirm the contents match the intended formulas, the last formula closes with $$, and no line is cut off.
  3. If extraction is incomplete but evidence-backed, still write the verified formulas you have now rather than delaying file creation while debugging one failing toolchain.
  4. Do not declare completion until all pages have been inspected or explicitly ruled out and every page containing standalone formulas has been accounted for.

Evidence and Verification

Output Format

Markdown file /root/latex_formula_extraction.md:

$$original_formula_1$$
$$original_formula_2$$

$$fixed_formula_1$$
$$fixed_formula_2$$
...

Write the final result to exactly /root/latex_formula_extraction.md; do not substitute JSON, reports, or code files for this artifact.

Write one formula per line in $$...$$ markdown display form, with extracted originals first and fixed versions after them only when needed.

Before finishing, verify the file exists, is non-empty, and contains only the extracted display formulas in the required $$...$$ format.

Verification must inspect the actual /root/latex_formula_extraction.md contents on disk and confirm the saved formulas are the ones you intend to submit; reading the saved file verifies persistence only, not fidelity to the PDF.

Do not leave the final content in JSON, Python, scratch notes, or alternate output files. If you used intermediate structures, convert them into the required markdown-only $$...$$ lines in /root/latex_formula_extraction.md before finishing.

Fix Types

Tips

Completion Checklist

Completion Checklist

Execution Workflow

  1. Before writing helper scripts, confirm the approach is viable in this runtime and preserves formula fidelity on a small sample.
  2. Treat any new extraction tool as untrusted until it produces inspectable output on disk or in stdout/stderr; verify that first.
  3. Do not keep polling, rerunning, or extending timeouts on the same failing conversion pipeline once you have two clear failures or no inspectable output.
  4. For opaque converters or background jobs, allow at most one diagnostic foreground rerun to capture stderr or a traceback before abandoning that path.
  5. After each major step, ask whether it produced readable formula evidence or updated /root/latex_formula_extraction.md; if not, change approach.
  6. Keep a simple coverage log: page number, whether it contains standalone display math, and which extraction/viewing method verified each retained formula.
  7. Once any verified formulas are available, start writing /root/latex_formula_extraction.md immediately and continue filling it as you inspect remaining pages.
  8. When output is clipped by the shell or tool display, save the extraction to a file or inspect smaller page/region chunks; do not treat clipped console text as complete evidence.
  9. If multiple tools fail or only part of the paper is recoverable, still save every directly verified formula, verify the file on disk, and continue expanding coverage only if time permits.

Execution Workflow

  1. Start with a lightweight, inspectable method first (for example, pdfplumber or PyMuPDF).
  2. Validate on 1-2 sample pages before scaling up: confirm displayed formulas are readable and not corrupted prose, Unicode fragments, or flattened layout.
  3. If direct/full-file reading hits size limits, switch to page-by-page, chunked, or region-level extraction instead of retrying the same full read.
  4. If a tool produces empty output, stalls, times out, or gives unusable formula text after 1-2 attempts, stop retrying it and switch to a materially different method.
  5. If text extraction loses equation structure, use page rendering, OCR, or manual page-level transcription from verified PDF evidence rather than adding more heuristics.
  6. Track which pages have been inspected so completeness claims are evidence-based.
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.