Section Writing Agent (Step 4)
Faithful implementation of the Section Writing Agent from PaperOrchestra
(Song et al., 2026, arXiv:2604.05018, §4 Step 4, App. F.1 pp. 47–49).
Cost: ONE LLM call (App. B: "Section Writing Agent (1 call): A single,
comprehensive multimodal call to draft and compile the complete LaTeX
manuscript"). Do NOT split this into per-section calls — the paper
explicitly designs it as one comprehensive call so the model can maintain
global coherence across sections.
Inputs
workspace/outline.json — the master plan
workspace/inputs/idea.md — technical details
workspace/inputs/experimental_log.md — raw data for tables and qualitative analysis
workspace/drafts/intro_relwork.tex — the template with Intro + Related
Work already filled in by Step 3. This is your starting point. The
preamble, package list, style, and the two pre-filled sections must be
preserved verbatim.
workspace/citation_pool.json — the citation map ({key, title, abstract}
for each verified paper)
workspace/refs.bib — the BibTeX file
workspace/inputs/conference_guidelines.md — formatting rules
workspace/figures/ — the actual PNG files from Step 2 (used as
multimodal vision input!)
workspace/figures/captions.json — caption text per figure_id
workspace/tex_profile.json — TeX package availability flags (written by
check_tex_packages.py at Step 0). Read this before generating any
LaTeX. It tells you which packages are installed so you select the right
cross-reference pattern, font packages, etc. before you write — not after
you try to compile.
Output
workspace/drafts/paper.tex — the complete LaTeX paper, with all sections
filled. The Step 5 Refinement Agent will iterate on this file.
How to do it
0.5. Read tex_profile.json and select LaTeX patterns
Before composing the prompt, read workspace/tex_profile.json and apply
these rules to every LaTeX choice in the generated paper:
| Profile flag |
True → use |
False → use instead |
use_cleveref |
\cref{fig:X}, \cref{tab:Y} |
Figure~\ref{fig:X}, Table~\ref{tab:Y} |
use_nicefrac |
\nicefrac{a}{b} |
$a/b$ |
use_microtype |
\usepackage{microtype} |
omit the line |
use_t1_fontenc |
\usepackage[T1]{fontenc} |
omit the line |
If tex_profile.json does not exist (old workspace), default to the safe
fallback column (no cleveref, no nicefrac, no microtype, no T1 fontenc).
1. Pre-extract metrics from the experimental log
Run the deterministic helper:
python skills/section-writing-agent/scripts/extract_metrics.py \
--log workspace/inputs/experimental_log.md \
--out workspace/metrics.json
This parses the ## 2. Raw Numeric Data section's markdown tables into
structured JSON. The Section Writing Agent uses this to construct LaTeX
booktabs tables without re-deriving values from raw text. Read
references/latex-table-patterns.md for the booktabs conventions.
2. Compose the prompt and make ONE multimodal call
Load references/prompt.md (verbatim Section Writing Agent prompt from App.
F.1). Prepend the Anti-Leakage Prompt from
../paper-orchestra/references/anti-leakage-prompt.md.
The user message contains:
outline.json — full content
idea.md — full content
experimental_log.md — full content (tables AND prose)
intro_relwork.tex — full content (this becomes template.tex for the prompt)
citation_pool.json — full content (becomes citation_map.json)
conference_guidelines.md — full content
figures_list — array of {figure_id, filename, caption} from
captions.json and the file listing
- The actual figure PNGs as multimodal image inputs, so the model can
visually inspect them and write accurate descriptions / refer to them
correctly in the prose.
If your host LLM has no vision input, fall back to text-only mode: pass the
captions in captions.json as descriptions and tell the agent it cannot see
the images directly. Quality drops noticeably (the paper notes that visual
grounding measurably improves figure-text alignment), but the pipeline
still completes.
3. Save the output
The agent's response is wrapped in \``latex ... ```fences. Extract the LaTeX code and save toworkspace/drafts/paper.tex`.
4. Run the deterministic gates
# Orphan citation gate: every \cite{KEY} must exist in refs.bib
python skills/section-writing-agent/scripts/orphan_cite_gate.py \
workspace/drafts/paper.tex workspace/refs.bib
# Latex sanity: matched braces, matched begin/end, no unescaped specials
python skills/section-writing-agent/scripts/latex_sanity.py \
workspace/drafts/paper.tex
# Anti-leakage post-check: no author names, emails, affiliations
python skills/paper-orchestra/scripts/anti_leakage_check.py \
workspace/drafts/paper.tex
If any gate fails, re-prompt the writing call with the gate's error
report appended to the user message and ask the agent to fix the specific
issues. Do NOT try to fix the gate violations by hand — the model needs to
see its own mistakes.
Critical rules from the prompt
These are excerpted from references/prompt.md (App. F.1, pp. 47-49). The
host agent MUST honor them on the writing call:
Existing-content preservation
- DO NOT modify the text, style, or content of sections that are already
filled in
intro_relwork.tex. Preserve Intro + Related Work verbatim.
- Keep the preamble (packages, document class, style) exactly as is.
- Come up with a good title if one is missing. Fill author names if missing
(but the Anti-Leakage Prompt says not to invent real ones — use a
placeholder like "Anonymous Authors" for double-blind).
Data and tables
- Build LaTeX tables for the experimental results.
- Extract numeric values directly from
experimental_log.md. Do not
hallucinate numbers — use the exact values in the log.
- Use the
booktabs package format: \toprule, \midrule, \bottomrule.
- All tables must appear before the Conclusion section, unless they are
explicitly placed in an Appendix.
Citations
- The
outline.json provides citation_hints per subsection. For each hint,
find the matching key in citation_pool.json (by title or content) and
use that exact key in \cite{...}.
- Use ONLY keys from
refs.bib. Inventing or guessing keys violates the
Lit Review Agent's verified pool.
- Read the abstract from
citation_pool.json for the papers you cite.
Use the abstract context to write specific, accurate sentences about
those works — not generic "[A, B] proposed methods for X".
Writing content
- Write the missing sections following
outline.json's section_plan
structure exactly. Hierarchy rule: if 4.1 exists, 4.2 must exist.
- Use formal mathematical equations, notations, and definitions where
appropriate AND directly supported by
idea.md or experimental_log.md.
Do not hallucinate math. Do not use complex math just for the sake
of it.
- Always provide detailed ablation studies and qualitative analysis of the
experimental results: what worked, what does not, and why.
- Optional: discuss limitations and future work at the end.
- If you put anything in the Appendix, the Appendix section appears AFTER
the References section, on a fresh new page.
Figures and visual fidelity
- You are being given the actual image files of the figures. You MUST
describe them faithfully and accurately. Do NOT hallucinate
interpretations that contradict the visual evidence in the plots.
- Use ALL of the figures provided in
figures/. Use the exact filenames
including extensions (e.g., .png) in your \includegraphics commands.
- DO NOT merge or group multiple figures into one display.
- If the paper is in a 2-column format, prefer single-column figures
(
\begin{figure}) unless they are very wide.
- All figures must appear before the Conclusion section, unless explicitly
in the Appendix.
- Refine the captions if necessary, but they are already provided in
captions.json and should generally be used as-is.
- Do NOT include "Figure X" in the caption text — LaTeX handles numbering.
Style
- Adopt the tone of a top-tier ML conference paper: dense, objective,
technical.
- Match the indentation and spacing style of the original
template.tex.
Do not change the overall LaTeX style.
LaTeX integrity
- The output must compile flawlessly out-of-the-box.
- All
\begin{X} must match a \end{X} (e.g., \begin{figure*} must be
closed with \end{figure*}, not \end{figure}).
- DO NOT change
\usepackage[capitalize]{cleveref} to
\usepackage[capitalize]{cleverref} — there is no cleverref.sty.
- Always emit
\clearpage immediately before \bibliographystyle{...}.
Without it, figures deferred by LaTeX's float algorithm will appear inside
or after the References section — a hard-to-spot layout defect that only
shows up in the compiled PDF. \clearpage forces all pending floats to be
output before the bibliography starts. See
references/latex-table-patterns.md for details.
- Cross-references: prefer
Figure~\ref{fig:X} and Table~\ref{tab:Y}
over bare \ref{fig:X}. This is necessary when cleveref is unavailable
and produces readable prose in all cases. Use \cref{...} only when
cleveref.sty is confirmed present.
Output format
- Wrap the full updated
template.tex in \``latex ... ````.
- The previously empty sections should now be filled.
- Previously filled sections (Intro, Related Work) should remain mostly
untouched; only adjust for consistency purposes.
Resources
references/prompt.md — verbatim Section Writing Agent prompt from App. F.1
references/latex-table-patterns.md — booktabs rules + table-from-log examples
references/figure-integration.md — \includegraphics, 2-column handling, placement
scripts/extract_metrics.py — markdown tables in experimental_log → JSON
scripts/latex_sanity.py — unmatched braces, env mismatches, specials
scripts/orphan_cite_gate.py — every \cite{KEY} exists in refs.bib
1---2name: section-writing-agent3description: Step 4 of the PaperOrchestra pipeline (arXiv:2604.05018). ONE single multimodal LLM call that drafts the remaining paper sections (Abstract, Methodology, Experiments, Conclusion), extracts numeric values from experimental_log.md into LaTeX booktabs tables, splices the generated figures from Step 2, and merges everything into the template that already contains Intro + Related Work from Step 3. TRIGGER when the orchestrator delegates Step 4 or when the user asks to "write the methodology and experiments sections" or "fill in the rest of the paper".4---5
6# Section Writing Agent (Step 4)
7
8Faithful implementation of the Section Writing Agent from PaperOrchestra
9(Song et al., 2026, arXiv:2604.05018, §4 Step 4, App. F.1 pp. 47–49).
10
11**Cost: ONE LLM call** (App. B: "Section Writing Agent (1 call): A single,
12comprehensive multimodal call to draft and compile the complete LaTeX
13manuscript"). Do NOT split this into per-section calls — the paper
14explicitly designs it as one comprehensive call so the model can maintain
15global coherence across sections.
16
17## Inputs
18
19- `workspace/outline.json` — the master plan
20- `workspace/inputs/idea.md` — technical details
21- `workspace/inputs/experimental_log.md` — raw data for tables and qualitative analysis
22- `workspace/drafts/intro_relwork.tex` — the template **with Intro + Related
23 Work already filled in by Step 3**. This is your starting point. The
24 preamble, package list, style, and the two pre-filled sections must be
25 preserved verbatim.
26- `workspace/citation_pool.json` — the citation map (`{key, title, abstract}`
27 for each verified paper)
28- `workspace/refs.bib` — the BibTeX file
29- `workspace/inputs/conference_guidelines.md` — formatting rules
30- `workspace/figures/` — the actual PNG files from Step 2 (used as
31 multimodal vision input!)
32- `workspace/figures/captions.json` — caption text per figure_id
33- `workspace/tex_profile.json` — TeX package availability flags (written by
34 `check_tex_packages.py` at Step 0). **Read this before generating any
35 LaTeX.** It tells you which packages are installed so you select the right
36 cross-reference pattern, font packages, etc. before you write — not after
37 you try to compile.
38
39## Output
40
41- `workspace/drafts/paper.tex` — the complete LaTeX paper, with all sections
42 filled. The Step 5 Refinement Agent will iterate on this file.
43
44## How to do it
45
46### 0.5. Read tex_profile.json and select LaTeX patterns
47
48Before composing the prompt, read `workspace/tex_profile.json` and apply
49these rules to every LaTeX choice in the generated paper:
50
51| Profile flag | True → use | False → use instead |
52|---|---|---|
53| `use_cleveref` | `\cref{fig:X}`, `\cref{tab:Y}` | `Figure~\ref{fig:X}`, `Table~\ref{tab:Y}` |
54| `use_nicefrac` | `\nicefrac{a}{b}` | `$a/b$` |
55| `use_microtype` | `\usepackage{microtype}` | omit the line |
56| `use_t1_fontenc` | `\usepackage[T1]{fontenc}` | omit the line |
57
58If `tex_profile.json` does not exist (old workspace), default to the safe
59fallback column (no cleveref, no nicefrac, no microtype, no T1 fontenc).
60
61### 1. Pre-extract metrics from the experimental log
62
63Run the deterministic helper:
64
65```bash
66python skills/section-writing-agent/scripts/extract_metrics.py \
67 --log workspace/inputs/experimental_log.md \
68 --out workspace/metrics.json
69```
70
71This parses the `## 2. Raw Numeric Data` section's markdown tables into
72structured JSON. The Section Writing Agent uses this to construct LaTeX
73booktabs tables without re-deriving values from raw text. Read
74`references/latex-table-patterns.md` for the booktabs conventions.
75
76### 2. Compose the prompt and make ONE multimodal call
77
78Load `references/prompt.md` (verbatim Section Writing Agent prompt from App.
79F.1). Prepend the Anti-Leakage Prompt from
80`../paper-orchestra/references/anti-leakage-prompt.md`.
81
82The user message contains:
83
84- `outline.json` — full content
85- `idea.md` — full content
86- `experimental_log.md` — full content (tables AND prose)
87- `intro_relwork.tex` — full content (this becomes `template.tex` for the prompt)
88- `citation_pool.json` — full content (becomes `citation_map.json`)
89- `conference_guidelines.md` — full content
90- `figures_list` — array of `{figure_id, filename, caption}` from
91 `captions.json` and the file listing
92- **The actual figure PNGs** as multimodal image inputs, so the model can
93 visually inspect them and write accurate descriptions / refer to them
94 correctly in the prose.
95
96If your host LLM has no vision input, fall back to text-only mode: pass the
97captions in `captions.json` as descriptions and tell the agent it cannot see
98the images directly. Quality drops noticeably (the paper notes that visual
99grounding measurably improves figure-text alignment), but the pipeline
100still completes.
101
102### 3. Save the output
103
104The agent's response is wrapped in `\`\`\`latex ... \`\`\`` fences. Extract
105the LaTeX code and save to `workspace/drafts/paper.tex`.
106
107### 4. Run the deterministic gates
108
109```bash
110# Orphan citation gate: every \cite{KEY} must exist in refs.bib
111python skills/section-writing-agent/scripts/orphan_cite_gate.py \
112 workspace/drafts/paper.tex workspace/refs.bib
113
114# Latex sanity: matched braces, matched begin/end, no unescaped specials
115python skills/section-writing-agent/scripts/latex_sanity.py \
116 workspace/drafts/paper.tex
117
118# Anti-leakage post-check: no author names, emails, affiliations
119python skills/paper-orchestra/scripts/anti_leakage_check.py \
120 workspace/drafts/paper.tex
121```
122
123If any gate fails, **re-prompt the writing call** with the gate's error
124report appended to the user message and ask the agent to fix the specific
125issues. Do NOT try to fix the gate violations by hand — the model needs to
126see its own mistakes.
127
128## Critical rules from the prompt
129
130These are excerpted from `references/prompt.md` (App. F.1, pp. 47-49). The
131host agent MUST honor them on the writing call:
132
133### Existing-content preservation
134
135- DO NOT modify the text, style, or content of sections that are already
136 filled in `intro_relwork.tex`. Preserve Intro + Related Work verbatim.
137- Keep the preamble (packages, document class, style) **exactly** as is.
138- Come up with a good title if one is missing. Fill author names if missing
139 (but the Anti-Leakage Prompt says not to invent real ones — use a
140 placeholder like "Anonymous Authors" for double-blind).
141
142### Data and tables
143
144- Build LaTeX tables for the experimental results.
145- Extract numeric values directly from `experimental_log.md`. **Do not
146 hallucinate numbers** — use the exact values in the log.
147- Use the `booktabs` package format: `\toprule`, `\midrule`, `\bottomrule`.
148- All tables must appear before the Conclusion section, unless they are
149 explicitly placed in an Appendix.
150
151### Citations
152
153- The `outline.json` provides citation_hints per subsection. For each hint,
154 find the matching key in `citation_pool.json` (by title or content) and
155 use that exact key in `\cite{...}`.
156- **Use ONLY keys from `refs.bib`.** Inventing or guessing keys violates the
157 Lit Review Agent's verified pool.
158- **Read the abstract** from `citation_pool.json` for the papers you cite.
159 Use the abstract context to write specific, accurate sentences about
160 those works — not generic "[A, B] proposed methods for X".
161
162### Writing content
163
164- Write the missing sections following `outline.json`'s `section_plan`
165 structure exactly. Hierarchy rule: if 4.1 exists, 4.2 must exist.
166- Use formal mathematical equations, notations, and definitions where
167 appropriate AND directly supported by `idea.md` or `experimental_log.md`.
168 **Do not hallucinate math.** Do not use complex math just for the sake
169 of it.
170- Always provide detailed ablation studies and qualitative analysis of the
171 experimental results: what worked, what does not, and why.
172- Optional: discuss limitations and future work at the end.
173- If you put anything in the Appendix, the Appendix section appears AFTER
174 the References section, on a fresh new page.
175
176### Figures and visual fidelity
177
178- You are being given the actual image files of the figures. You MUST
179 describe them faithfully and accurately. Do NOT hallucinate
180 interpretations that contradict the visual evidence in the plots.
181- Use ALL of the figures provided in `figures/`. Use the exact filenames
182 including extensions (e.g., `.png`) in your `\includegraphics` commands.
183- DO NOT merge or group multiple figures into one display.
184- If the paper is in a 2-column format, prefer single-column figures
185 (`\begin{figure}`) unless they are very wide.
186- All figures must appear before the Conclusion section, unless explicitly
187 in the Appendix.
188- Refine the captions if necessary, but they are already provided in
189 `captions.json` and should generally be used as-is.
190- Do NOT include "Figure X" in the caption text — LaTeX handles numbering.
191
192### Style
193
194- Adopt the tone of a top-tier ML conference paper: dense, objective,
195 technical.
196- Match the indentation and spacing style of the original `template.tex`.
197 Do not change the overall LaTeX style.
198
199### LaTeX integrity
200
201- The output must compile flawlessly out-of-the-box.
202- All `\begin{X}` must match a `\end{X}` (e.g., `\begin{figure*}` must be
203 closed with `\end{figure*}`, not `\end{figure}`).
204- DO NOT change `\usepackage[capitalize]{cleveref}` to
205 `\usepackage[capitalize]{cleverref}` — there is no `cleverref.sty`.
206- **Always emit `\clearpage` immediately before `\bibliographystyle{...}`.**
207 Without it, figures deferred by LaTeX's float algorithm will appear inside
208 or after the References section — a hard-to-spot layout defect that only
209 shows up in the compiled PDF. `\clearpage` forces all pending floats to be
210 output before the bibliography starts. See
211 `references/latex-table-patterns.md` for details.
212- **Cross-references**: prefer `Figure~\ref{fig:X}` and `Table~\ref{tab:Y}`
213 over bare `\ref{fig:X}`. This is necessary when `cleveref` is unavailable
214 and produces readable prose in all cases. Use `\cref{...}` only when
215 `cleveref.sty` is confirmed present.
216
217### Output format
218
219- Wrap the full updated `template.tex` in `\`\`\`latex ... \`\`\``.
220- The previously empty sections should now be filled.
221- Previously filled sections (Intro, Related Work) should remain mostly
222 untouched; only adjust for consistency purposes.
223
224## Resources
225
226- `references/prompt.md` — verbatim Section Writing Agent prompt from App. F.1
227- `references/latex-table-patterns.md` — booktabs rules + table-from-log examples
228- `references/figure-integration.md` — `\includegraphics`, 2-column handling, placement
229- `scripts/extract_metrics.py` — markdown tables in experimental_log → JSON
230- `scripts/latex_sanity.py` — unmatched braces, env mismatches, specials
231- `scripts/orphan_cite_gate.py` — every `\cite{KEY}` exists in refs.bib