interplane.Get Omega
Read in your language

hive-not-swarm

aien-dev/hive-not-swarm

Brought by drakestapleton

branch master Branches Tags History

hive-not-swarm / build_latex.py

134 lines · 5822 bytes Raw Blame History
1"""Build the LaTeX edition (paper.tex, paper.pdf) from Hive_Not_Swarm.docx.
2
3Run build_article.py first. This converts the DOCX with pandoc, then applies the
4house style used by the author's other papers (article 11pt, 1in margins,
5booktabs, thebibliography), restores the five display equations as numbered
6LaTeX equations, turns [n] citations into \\cite, and compiles with tectonic.
7"""
8import json, re, subprocess, sys
9from pathlib import Path
10
11ROOT = Path(__file__).resolve().parent
12PUB = json.loads((ROOT / 'publication.json').read_text()) if (ROOT / 'publication.json').exists() else {}
13ORDER = json.loads((ROOT / 'reference_order.json').read_text())
14KEY = {r['n']: r['key'] for r in ORDER}
15ORCID = '0009-0002-6890-7111'
16
17raw = subprocess.run(['pandoc', str(ROOT / 'Hive_Not_Swarm.docx'), '-t', 'latex', '--wrap=none'],
18 check=True, capture_output=True, text=True).stdout
19
20def fail(msg):
21 sys.exit('build_latex: ' + msg)
22
23# Split: title block | abstract | body | references
24m = re.search(r'\\section\{Abstract\}\\label\{abstract\}\n', raw) or fail('no Abstract heading')
25after = raw[m.end():]
26m2 = re.search(r'\n\\section\{1 ', after) or fail('no section 1')
27abstract, body = after[:m2.start()].strip(), after[m2.start():]
28m3 = re.search(r'\\section\{References\}\\label\{references\}\n', body) or fail('no References heading')
29body, refs = body[:m3.start()], body[m3.end():]
30
31# Equations: replace pandoc's inline renderings, in document order, with numbered equations.
32EQS = [
33 r'T(n) = S + \frac{A}{n} + b\,(n-1) + \frac{c\,n(n-1)}{2} + r_0 + r\,n',
34 r'n = 1, 2, \ldots, P; \qquad r_0 + r\,n \le H, \qquad 1 - (1-p)^{n} \le \varepsilon',
35 r"T'(n) = -\frac{A}{n^{2}} + b + c\left(n - \tfrac{1}{2}\right) + r",
36 r"T''(n) = \frac{2A}{n^{3}} + c > 0",
37 r'T(n+1) - T(n) = -\frac{A}{n(n+1)} + b + c\,n + r',
38]
39eq_lines = re.findall(r'^\\\(.*\\\) \((\d)\)$', body, flags=re.M)
40if eq_lines != ['1', '2', '3', '4', '5']:
41 fail(f'expected equations 1-5, found {eq_lines}')
42it = iter(EQS)
43body = re.sub(r'^\\\(.*\\\) \((\d)\)$',
44 lambda mm: '\\begin{equation}\n' + next(it) + '\n\\label{eq:' + mm.group(1) + '}\n\\end{equation}',
45 body, flags=re.M)
46
47# Headings: LaTeX numbers them; numberless headings stay unnumbered.
48def heading(mm):
49 level, text = mm.group(1), mm.group(2)
50 num = re.match(r'(\d+(?:\.\d+)?)\s+(.*)', text)
51 return f'\\{level}{{{num.group(2)}}}' if num else f'\\{level}*{{{text}}}'
52body = re.sub(r'\\((?:sub)*section)\{([^}]*)\}\\label\{[^}]*\}', heading, body)
53
54# Figures: house paths, full text width, captions without the manual prefix.
55def figure(mm):
56 k = int(mm.group(1))
57 path = next((ROOT / 'figures').glob(f'0{k}_*.png'), None) or fail(f'no figure {k}')
58 return f'\\includegraphics[width=\\linewidth]{{figures/{path.name}}}'
59body, nfig = re.subn(r'\\includegraphics\[[^\]]*alt=\{Figure (\d)\.[^\n]*?\]\{[^}]*\}', figure, body)
60if nfig != 6:
61 fail(f'expected 6 figures, converted {nfig}')
62body = body.replace('\\begin{figure}', '\\begin{figure}[tbp]')
63body = re.sub(r'\\caption\{(Figure|Table) \d\. ', r'\\caption{', body)
64
65# Citations.
66def cite(mm):
67 nums = [int(x) for x in mm.group(1).split(',')]
68 return '\\cite{' + ','.join(KEY[n] for n in nums) + '}'
69body, ncite = re.subn(r'\{\[\}(\d+(?:,\d+)*)\{\]\}', cite, body)
70
71# Bibliography, in first-citation order.
72items = re.findall(r'^\{\[\}(\d+)\{\]\} (.*)$', refs, flags=re.M)
73if [int(n) for n, _ in items] != list(range(1, len(ORDER) + 1)):
74 fail('reference list does not match reference_order.json')
75bib = '\n'.join(f'\\bibitem{{{KEY[int(n)]}}} {text}' for n, text in items)
76# Identifiers (DOIs, arXiv ids, file paths) may break across lines like URLs.
77bib = re.sub(r'\\href\{([^}]*)\}\{([^ }]+)\}', r'\\href{\1}{\\nolinkurl{\2}}', bib)
78bib = bib.replace('sources/operational\\_record\\_extract.md', '\\nolinkurl{sources/operational_record_extract.md}')
79
80MATH = {'−': '$-$', 'ε': '$\\varepsilon$', '₀': '$_0$', '≤': '$\\le$', '≥': '$\\ge$',
81 '≈': '$\\approx$', 'λ': '$\\lambda$', '½': '$\\tfrac{1}{2}$'}
82def text(s):
83 for a, b in MATH.items():
84 s = s.replace(a, b)
85 return s
86
87doi = PUB.get('doi')
88date = 'Revision 3, 29 October 2026' + (f'\\\\ \\small DOI \\href{{https://doi.org/{doi}}}{{{doi}}}' if doi else '')
89tex = rf"""\documentclass[11pt]{{article}}
90\usepackage{{iftex}}
91\ifPDFTeX
92 \usepackage[T1]{{fontenc}}
93 \usepackage[utf8]{{inputenc}}
94 \usepackage{{lmodern}}
95\else
96 \usepackage{{fontspec}}
97\fi
98\usepackage[margin=1in]{{geometry}}
99\usepackage{{amsmath,amssymb}}
100\usepackage{{graphicx}}
101\usepackage{{booktabs,longtable,array,calc,etoolbox}}
102\usepackage{{microtype}}
103\usepackage{{parskip}}
104\usepackage[hidelinks]{{hyperref}}
105\usepackage{{xurl}}
106\setlength{{\emergencystretch}}{{1.5em}}
107\providecommand{{\tightlist}}{{\setlength{{\itemsep}}{{0pt}}\setlength{{\parskip}}{{0pt}}}}
108\newcommand{{\orcid}}{{\href{{https://orcid.org/{ORCID}}}{{\raisebox{{-0.15em}}{{\includegraphics[height=0.95em]{{assets/orcid_id_icon.png}}}}}}}}
109\setlength{{\LTcapwidth}}{{\linewidth}}
110\renewcommand{{\arraystretch}}{{1.15}}
111\AtBeginEnvironment{{longtable}}{{\small}}
112
113\title{{Hive, not swarm: control, flow and collapse in\\ human-directed coding-agent teams}}
114\author{{Drake Stapleton\,\orcid\\
115\small Independent Researcher, AIEN, Springfield, Missouri, USA\\
116\small \url{{https://orcid.org/{ORCID}}}}}
117\date{{{date}}}
118
119\begin{{document}}
120\maketitle
121
122\begin{{abstract}}
123{text(abstract)}
124\end{{abstract}}
125{text(body)}
126\begin{{thebibliography}}{{99}}
127{text(bib)}
128\end{{thebibliography}}
129
130\end{{document}}
131"""
132(ROOT / 'paper.tex').write_text(tex)
133subprocess.run(['tectonic', '-X', 'compile', 'paper.tex'], cwd=ROOT, check=True)
134print(f'paper.tex: {ncite} citations, {len(items)} references, {nfig} figures, 5 equations')