---
name: wtfp:export-latex
description: Export paper to LaTeX with bibliography and formatting
---

<objective>
Export the paper to LaTeX format.

Generates:
- paper.tex from markdown sections
- references.bib from literature index
- Applies journal/conference template

Outputs to paper/ directory.
</objective>

<process>

<step name="verify">
**Verify content exists:**

```bash
[ ! -d paper ] && echo "ERROR: No paper/ directory found. Write sections first." && exit 1
ls paper/*.md 2>/dev/null || echo "WARNING: No markdown content in paper/"
```

</step>

<step name="template">
**Select template:**

Use the question tool:
- header: "LaTeX Template"
- question: "Which LaTeX template should we use?"
- options:
  - "Article (generic)" — Standard article class
  - "IEEE" — IEEE conference/journal format
  - "ACM" — ACM conference format
  - "Custom" — I have a specific template
  - "None" — Plain LaTeX, no special template

If "Custom":
- Ask for template path or template name
</step>

<step name="gather">
**Gather content:**

- Read all section files from paper/
- Read PROJECT.md for title, authors (if specified)
- Read sources/literature.md for bibliography
- Identify figures referenced
</step>

<step name="convert">
**Convert to LaTeX:**

Create paper/paper.tex:

```latex
% Generated by WTF-P
% [timestamp]

\documentclass[template-options]{article-or-template}

% Packages
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{cite}

\title{[Title from PROJECT.md]}
\author{[Author if specified]}
\date{[Date]}

\begin{document}

\maketitle

% Abstract
\begin{abstract}
[Abstract content]
\end{abstract}

% Sections
\section{Introduction}
[Introduction content converted to LaTeX]

\section{Methods}
[Methods content]

% ... remaining sections ...

% References
\bibliographystyle{[style]}
\bibliography{references}

\end{document}
```

Conversion rules:
- `## Heading` → `\section{Heading}`
- `### Subheading` → `\subsection{Subheading}`
- `**bold**` → `\textbf{bold}`
- `*italic*` → `\textit{italic}`
- `[citation]` → `\cite{citation}`
- `![figure](path)` → `\includegraphics{path}` with figure environment
- Math: `$inline$` → `$inline$`, `$$display$$` → equation environment

</step>

<step name="bibliography">
**Generate bibliography:**

Create paper/references.bib from sources/literature.md:

```bibtex
@article{key1,
  author = {Author Name},
  title = {Paper Title},
  journal = {Journal Name},
  year = {2024},
  volume = {1},
  pages = {1-10}
}

% ... additional entries ...
```

</step>

<step name="compile_check">
**Optional: Test compilation:**

Use the question tool:
- header: "Compile"
- question: "Try to compile the LaTeX?"
- options:
  - "Yes, compile" — Run pdflatex
  - "No, just export" — Skip compilation

If yes:
```bash
cd paper && pdflatex paper.tex && bibtex paper && pdflatex paper.tex && pdflatex paper.tex
```

Report any errors.

</step>

<step name="commit">
```bash
git add paper/paper.tex paper/references.bib
git commit -m "$(cat <<'EOF'
export: generate LaTeX output

Template: [template]
Sections: [N]
References: [N]
EOF
)"
```

</step>

<step name="done">
**Present completion:**

```
LaTeX export complete:

- Document: paper/paper.tex
- Bibliography: paper/references.bib
- Template: [template]

## Files Generated
- paper.tex: [N] sections, [X] words
- references.bib: [N] entries
[If compiled:] - paper.pdf: Generated successfully

## Manual Steps
1. Review paper/paper.tex for any conversion issues
2. Add any custom LaTeX packages needed
3. Compile: `cd paper && pdflatex paper.tex && bibtex paper && pdflatex paper.tex`

---

## ▶ Next Up

**Review output** — check formatting and fix issues

Or if ready:
`/wtfp:submit-milestone "draft-1"` — archive this version

---
```

</step>

</process>

<success_criteria>
- [ ] All sections converted to LaTeX
- [ ] Bibliography generated
- [ ] Template applied correctly
- [ ] Figures referenced properly
- [ ] Citations converted
- [ ] Committed to git
</success_criteria>
