RefSeg-CA / source /legacy_project /code /build_latex.py
nmsofficial's picture
Add verified public availability and human-evaluation guide
9126e0d verified
Raw History Blame Contribute Delete
2.84 kB
"""Compile vector figures and the unmodified Springer Nature journal template.
Uses an existing TeX installation; the fallback only creates a local format/map
when a workspace runtime has TeX sources but no configured format database.
"""
from pathlib import Path
import os,subprocess,argparse
ROOT=Path(__file__).resolve().parents[1]
def environment():
env=os.environ.copy()
if subprocess.run(['kpsewhich','article.cls'],capture_output=True,text=True).stdout.strip():return env
tex=Path('/usr/share/texlive/texmf-dist')
if not (tex/'tex/latex/base/article.cls').exists():raise RuntimeError('Install a standard TeX Live distribution with pgfplots, standalone and Springer dependencies')
aux=ROOT/'build/texlocal';aux.mkdir(parents=True,exist_ok=True)
env.update(TEXMF=str(tex),TEXMFVAR=str(aux),TEXMFCONFIG=str(aux),TEXFORMATS=str(aux)+'//:',TEXFONTMAPS=str(aux)+'//:'+str(tex/'fonts/map')+'//:')
if not (aux/'pdflatex.fmt').exists():
run(['pdftex','-ini','-interaction=batchmode','-halt-on-error','-jobname=pdflatex','-progname=pdflatex','-output-directory='+str(aux),'-etex','pdflatex.ini'],ROOT,env)
maps=tex/'fonts/map/dvips';files=list((maps/'amsfonts').glob('*.map'))+[maps/'helvetic/uhv.map',maps/'psnfss/psnfss.map']
(aux/'pdftex.map').write_text('\n'.join(f.read_text() for f in files))
return env
def run(cmd,cwd,env):
p=subprocess.run(cmd,cwd=cwd,env=env,capture_output=True,text=True)
if p.returncode:
print(p.stdout[-4000:]);print(p.stderr[-1000:])
if cmd[0]=='pdflatex':
log=Path(cwd)/Path(cmd[-1]).with_suffix('.log')
if log.exists():print(log.read_text(errors='replace')[-5000:])
raise RuntimeError('Command failed: '+' '.join(cmd))
return p
def main():
a=argparse.ArgumentParser();a.add_argument('--figures-only',action='store_true');a.add_argument('--manuscript-only',action='store_true');args=a.parse_args();env=environment()
for f in ([] if args.manuscript_only else sorted((ROOT/'figures').glob('Fig*.tex'))):
run(['pdflatex','-interaction=batchmode','-halt-on-error',f.name],f.parent,env);print('Compiled',f.name,flush=True)
# Standard EPS export retains vector paths and embedded outlines.
eps=f.with_suffix('.eps')
try:run(['pdftops','-eps',str(f.with_suffix('.pdf')),str(eps)],ROOT,env)
except (FileNotFoundError,RuntimeError):pass
main=ROOT/'latex/RefSeg_CACP_MVA.tex'
if not args.figures_only and main.exists():
run(['pdflatex','-interaction=batchmode','-halt-on-error',main.name],main.parent,env)
run(['bibtex',main.stem],main.parent,env)
for _ in range(2):run(['pdflatex','-interaction=batchmode','-halt-on-error',main.name],main.parent,env)
print('Compiled manuscript',flush=True)
if __name__=='__main__':main()