Compile main.tex to PDF using pdflatex + bibtex. main.tex typically references ``\\input{sections/abstract}`` etc., so we recreate the expected directory layout inside a temp folder: tmp/ main.tex PRIMEarxiv.sty *.bib (if any, at top level)
(sections_dir: Path)
| 327 | |
| 328 | |
| 329 | def _compile_pdf(sections_dir: Path) -> Optional[Path]: |
| 330 | """Compile main.tex to PDF using pdflatex + bibtex. |
| 331 | |
| 332 | main.tex typically references ``\\input{sections/abstract}`` etc., so we |
| 333 | recreate the expected directory layout inside a temp folder: |
| 334 | |
| 335 | tmp/ |
| 336 | main.tex |
| 337 | PRIMEarxiv.sty |
| 338 | *.bib (if any, at top level) |
| 339 | sections/ |
| 340 | abstract.tex |
| 341 | ... |
| 342 | |
| 343 | Returns the path to the resulting PDF (or None on failure). |
| 344 | """ |
| 345 | main_tex = sections_dir / "main.tex" |
| 346 | if not main_tex.is_file(): |
| 347 | print(f"[PDF] No main.tex found in {sections_dir}") |
| 348 | return None |
| 349 | |
| 350 | tmp = Path(tempfile.mkdtemp(prefix="aiscientists_pdf_")) |
| 351 | try: |
| 352 | # main.tex and .sty go at the top level |
| 353 | shutil.copy2(main_tex, tmp / "main.tex") |
| 354 | # Always use PRIMEarxiv.sty from demo source (sections dir may be read-only) |
| 355 | if PRIMEARXIV_STY.is_file(): |
| 356 | shutil.copy2(PRIMEARXIV_STY, tmp / "PRIMEarxiv.sty") |
| 357 | |
| 358 | # Everything else goes into sections/ (matching \input{sections/...}) |
| 359 | sub = tmp / "sections" |
| 360 | sub.mkdir() |
| 361 | for f in sections_dir.iterdir(): |
| 362 | if f.is_file() and f.name != "main.tex": |
| 363 | shutil.copy2(f, sub / f.name) |
| 364 | |
| 365 | # Also copy .bib files to top level as fallback for bibtex |
| 366 | for bib in sections_dir.glob("*.bib"): |
| 367 | shutil.copy2(bib, tmp / bib.name) |
| 368 | |
| 369 | def _run(cmd: list[str]) -> subprocess.CompletedProcess: |
| 370 | r = subprocess.run( |
| 371 | cmd, cwd=str(tmp), |
| 372 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=120, |
| 373 | ) |
| 374 | return r |
| 375 | |
| 376 | # pdflatex pass 1 |
| 377 | r1 = _run(["pdflatex", "-interaction=nonstopmode", "main.tex"]) |
| 378 | print(f"[PDF] pdflatex pass 1 exit={r1.returncode}") |
| 379 | |
| 380 | # bibtex (if any .bib exists at top level or in sections/) |
| 381 | bib_files = list(tmp.glob("*.bib")) + list(sub.glob("*.bib")) |
| 382 | if bib_files: |
| 383 | rb = _run(["bibtex", "main"]) |
| 384 | print(f"[PDF] bibtex exit={rb.returncode}") |
| 385 | |
| 386 | # pdflatex pass 2 & 3 |