| name | comp-compile-en |
| description | Compile English competition paper (MCM/ICM/APMCM) and run compliance checks. Use when user says "compile MCM paper", "编译美赛论文". |
| argument-hint | ["paper-directory"] |
| allowed-tools | Bash(*), Read, Write, Edit, Grep, Glob |
Competition Paper Compile & Compliance (English)
Compile and validate: $ARGUMENTS
Constants
Workflow
Step 1: Verify environment
Check pdflatex and bibtex are installed.
Step 2: Pre-compile cleanup
if [ -f "_utils/compile_utils.sh" ]; then
bash _utils/compile_utils.sh paper/
elif [ -f "skills/shared-scripts/compile_utils.sh" ]; then
bash skills/shared-scripts/compile_utils.sh paper/
else
echo "compile_utils.sh not found, manual cleanup needed"
fi
The script auto-handles: special chars, table fixes, path correction, hidelinks, wide table resizebox, light-color text fixes, TikZ library injection.
Also check ref/label matching and embed missing figures:
mkdir -p _tmp
grep -oh '\\ref{[^}]*}' paper/sections/*.tex paper/main.tex 2>/dev/null | sort -u > _tmp/_refs.txt
grep -oh '\\label{[^}]*}' paper/sections/*.tex paper/main.tex 2>/dev/null | sort -u > _tmp/_labels.txt
comm -23 <(sed 's/\\ref/\\label/g' _tmp/_refs.txt) _tmp/_labels.txt > _tmp/_missing_labels.txt
cat _tmp/_missing_labels.txt
If labels are missing, find corresponding figure/table code in figures/*.tex and embed into the correct section file.
Also check compile_utils.sh output for "UNEMBEDDED" warnings — each one means a figure or table from figures/ is not in any section.
MANDATORY FIX LOOP — do NOT proceed to compilation until all figures AND tables are embedded:
UNEMBED=0
for pdf in figures/*.pdf; do
[ -f "$pdf" ] || continue
bn=$(basename "$pdf")
grep -rq "$bn" paper/sections/*.tex paper/main.tex 2>/dev/null || { echo "UNEMBEDDED PDF: $bn"; UNEMBED=$((UNEMBED+1)); }
done
for tbl in figures/TABLE_*.tex; do
[ -f "$tbl" ] || continue
for lbl in $(grep -oh '\\label{[^}]*}' "$tbl" 2>/dev/null); do
grep -rq "$lbl" paper/sections/*.tex paper/main.tex 2>/dev/null || { echo "UNEMBEDDED TABLE: $lbl"; UNEMBED=$((UNEMBED+1)); }
done
done
echo "Total unembedded: $UNEMBED"
If UNEMBED > 0, fix ALL before compiling. For each unembedded item, copy the figure/table block into the appropriate section with lead-in text + analysis. Re-run until UNEMBED = 0.
for pdf in figures/*.pdf; do
[ -f "$pdf" ] || continue
bn=$(basename "$pdf")
grep -rq "$bn" paper/sections/*.tex paper/main.tex 2>/dev/null || echo "⚠ $bn not referenced"
done
Step 3: Compile (manual steps, no latexmk)
cd paper/
pdflatex -interaction=nonstopmode main.tex
bibtex main
pdflatex -interaction=nonstopmode main.tex
pdflatex -interaction=nonstopmode main.tex
Step 4: Error fix loop (MANDATORY)
After each compilation, check main.log for CRITICAL errors. You MUST fix ALL errors before declaring compilation complete.
MATH_ERR=$(grep -c 'Bad math environment delimiter\|Missing \$ inserted\|begin{document} ended by' paper/main.log 2>/dev/null || echo 0)
LR_ERR=$(grep -c 'Not allowed in LR mode' paper/main.log 2>/dev/null || echo 0)
echo "Math errors: $MATH_ERR, LR mode errors: $LR_ERR"
[ $((MATH_ERR + LR_ERR)) -gt 0 ] && grep -B2 'Bad math\|Missing \$ inserted\|Not allowed in LR mode' paper/main.log | grep -E '^\./|^l\.' | head -20
Iterate up to 5 times. For each error:
-
Math errors: read the error location from main.log, open the file, fix broken $...$ delimiters individually. Do NOT use broad sed patterns.
-
LR mode errors: add \par or blank line before float environments.
-
Undefined control sequence: add missing \usepackage or fix typo.
-
BibTeX failures: fix LaTeX errors first (BibTeX fails when LaTeX errors exist upstream), then recompile.
After each fix, recompile (xelatex → bibtex → xelatex → xelatex) and recheck. ⛔ Do NOT proceed until MATH_ERR = 0 and LR_ERR = 0.
When fixing errors in main.tex, only fix the specific error. Do not rewrite or restructure main.tex — the template's preamble, page margins, and formatting settings must remain unchanged.
Step 5: Post-compile checks
bash _utils/compile_check.sh paper/ 2>/dev/null || bash skills/shared-scripts/compile_check.sh paper/
The script checks: PDF existence/size, undefined references, overfull hbox, TOC, bibliography entries, citation count, unused figures, figure stacking, TikZ diagram presence.
Step 6: Competition compliance
Check items:
-
Page count: body = Summary Sheet through Conclusions, excluding References/Appendix. Must be ≥ MAX_PAGES
-
Summary Sheet exists (MCM/ICM critical)
-
Team Control Number placeholder present
-
Anonymous (no school names)
-
APMCM: commitment letter not in PDF
-
Code appendix exists
-
No undefined references/citations
<page_diagnosis>
Page count diagnosis (when insufficient)
If body pages < 80% of MAX_PAGES:
echo "=== Page count diagnosis ==="
echo "Target: ≥ MAX_PAGES pages"
echo ""
echo "=== Section character counts ==="
for f in paper/sections/*.tex; do
chars=$(wc -c < "$f")
echo " $(basename $f): $chars chars (~$(echo "scale=1; $chars/2200" | bc) pages)"
done
echo ""
echo "=== Sections needing expansion (3 smallest) ==="
for f in $(ls -S paper/sections/*.tex | tail -3); do
chars=$(wc -c < "$f")
echo " ⚠ $(basename $f): only $chars chars"
done
Mark as CRITICAL with specific recommendations:
If pages < 80% of MAX_PAGES, attempt to expand thinnest 1-2 sections from MODELING_REPORT.md and RESULTS.md, then recompile.
</page_diagnosis>
Step 7: ⛔ FINAL QUALITY GATE (must ALL pass before finishing)
echo "=== Running check scripts ==="
bash _utils/compile_check.sh paper/ 2>/dev/null || bash skills/shared-scripts/compile_check.sh paper/ 2>/dev/null
bash _utils/writing_check.sh paper/ 2>/dev/null || bash skills/shared-scripts/writing_check.sh paper/ 2>/dev/null
echo ""
echo "=========================================="
echo " FINAL QUALITY GATE"
echo "=========================================="
GATE_FAIL=0
[ -f paper/main.pdf ] && [ $(wc -c < paper/main.pdf) -gt 100000 ] && echo "✅ PDF exists" || { echo "❌ PDF missing/small"; GATE_FAIL=$((GATE_FAIL+1)); }
MATH_ERR=$(grep -c 'Bad math.*delimiter\|Missing \$ inserted' paper/main.log 2>/dev/null || echo 0)
LR_ERR=$(grep -c 'Not allowed in LR mode' paper/main.log 2>/dev/null || echo 0)
[ "$((MATH_ERR+LR_ERR))" -eq 0 ] && echo "✅ No LaTeX errors" || { echo "❌ $MATH_ERR math + $LR_ERR LR errors"; GATE_FAIL=$((GATE_FAIL+1)); }
BBL=$(grep -c '\\bibitem' paper/main.bbl 2>/dev/null || echo 0)
[ "$BBL" -gt 0 ] && echo "✅ Bib: $BBL entries" || { echo "❌ Bib empty"; GATE_FAIL=$((GATE_FAIL+1)); }
CITE=$(grep -roh '\\cite{' paper/sections/*.tex paper/main.tex 2>/dev/null | wc -l)
[ -gt 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
UNEMBED=0; pdf figures/*.pdf; [ -f ] || ; bn=$( ); grep -rq paper/sections/*.tex paper/main.tex 2>/dev/null || UNEMBED=$((UNEMBED+));
[ -eq 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
PLACEHOLDERS=$(grep -rl paper/sections/*.tex paper/main.tex 2>/dev/null | -l)
[ -eq 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
VBOX=$(grep -c paper/main.log 2>/dev/null || 0); [ -eq 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
HBOX=$(grep -c paper/main.log 2>/dev/null || 0); [ -lt 5 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
AI=0; f paper/sections/*.tex; [ -f ] || ; | grep -qi && ; c=$(grep -c 2>/dev/null || 0); AI=$((AI+c));
[ -eq 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
UNDEF_REFS=$(grep -c paper/main.log 2>/dev/null || 0)
[ -eq 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
STACKING=0; f paper/sections/*.tex; [ -f ] || ; s=$(awk 2>/dev/null); STACKING=$((STACKING+s));
[ -eq 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
grep -q paper/main.tex 2>/dev/null;
[ -s paper/main.toc ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
TIKZ=$(grep -rl paper/sections/*.tex 2>/dev/null | -l)
TMPL=
t _templates/mcm_main.tex _templates/stats_main.tex _templates/cumcm_main.tex; [ -f ] && TMPL= && ;
[ -n ] && [ -f paper/main.tex ];
TMPL_PRE=$(sed -n | grep | )
MAIN_PRE=$(sed -n paper/main.tex | grep | )
MISSING=$( -23 <( ) <( ) 2>/dev/null | -5)
[ -z ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
grep -q paper/main.tex 2>/dev/null;
; GATE_FAIL=$((GATE_FAIL+))
[ -f figures/tikz_architecture_examples.tex ] || grep -qi PAPER_PLAN.md PROBLEM_ANALYSIS.md 2>/dev/null;
[ -gt 0 ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
[ -gt 0 ] && ||
PLAN_FIGS=0; ACTUAL=$( figures/*.pdf 2>/dev/null | -l)
plan PAPER_PLAN.md PROBLEM_ANALYSIS.md TOPIC_PLAN.md; [ -f ] || ; pf=$(grep -ci 2>/dev/null || 0); [ -gt ] && PLAN_FIGS=;
[ -gt 0 ];
[ -ge ] && || { ; GATE_FAIL=$((GATE_FAIL+)); }
[ -eq 0 ] && ||
⛔ If GATE_FAIL > 0, fix every ❌, recompile, re-run gate. Do NOT finish with any ❌.
Step 8: Output report
Status, PDF path, page count, compliance pass/fail.
Key Rules
-
No latexmk — manual step-by-step compilation
-
Do not delete .bbl file after compilation
-
Summary Sheet is critical for MCM/ICM
-
APMCM: commitment letter must not be in PDF
-
Body pages ≥ MAX_PAGES
-
Primary output: paper/main.pdf, temp files: _tmp/