How to Avoid AI-Generated LaTeX That Will Not Compile
When AI generates LaTeX, it may not know your project's loaded packages, compiler, or template rules. It can therefore call undefined commands, place mathematical notation in ordinary text, or use unsupported characters. The log points to the affected source line, not to the missing context that caused it.
Fix the Preamble Before Generating Content
Give AI a known preamble together with the task and say: “Do not add \usepackage; use only the commands supported here.” This is a complete example:
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc} % Keep for pdfLaTeX; omit for XeLaTeX
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb} % Math environments and symbols
\usepackage{graphicx} % \includegraphics
\usepackage{booktabs} % \toprule \midrule \bottomrule
\usepackage{hyperref} % Links; usually loaded near the end
\begin{document}
\section{Method}
We minimise the objective
\begin{equation}\label{eq:obj}
J(\theta) = \frac{1}{n}\sum_{i=1}^{n}\ell\bigl(f(x_i;\theta), y_i\bigr)
\end{equation}
where $\theta \in \mathbb{R}^{d}$.
\begin{table}[htbp]
\centering
\begin{tabular}{lcc}
\toprule
Model & Accuracy & F1 \\
\midrule
Baseline & 0.812 & 0.794 \\
Ours & 0.867 & 0.851 \\
\bottomrule
\end{tabular}
\caption{Illustrative results.}
\label{tab:results}
\end{table}
\end{document}
The package list defines the available commands: \toprule needs booktabs, \bm needs bm, and \SI needs siunitx. Without that information, AI guesses from common patterns, potentially causing Undefined control sequence. or File 'xxx.sty' not found.
Five Common Errors and Their Causes
| Error | Likely cause | Fix |
|---|---|---|
Undefined control sequence. | A command such as \toprule, \bm, or \SI lacks its package | Load the appropriate supported package, or use the existing command set |
File 'xxx.sty' not found. | A package name is misspelled, invented, or unavailable | Correct the name or use a package already available to the project |
Missing $ inserted. | Math commands or subscripts appear outside math mode | Use \(\alpha\) or \(x^2\); treat text percent signs separately as \% |
Unicode character θ (U+03B8) not set up for use with LaTeX. | The current setup does not support that literal character | Prefer \(\theta\); changing engines also requires a compatible font and math setup |
Extra alignment tab has been changed to \cr. | A normal table row has too many alignment separators | Check the column specification and separators; account separately for \multicolumn spans |
These messages identify source problems, not “AI errors.” Also remember that % starts a comment. An unescaped percentage can hide the rest of a source line and cause later, less obvious errors.
Six Constraints to Include in the Request
- Ask for a complete compilable example, from
\documentclassto\end{document}, when testing a standalone idea. - Generate one section at a time. Compile it before moving on.
- State the compiler and document class. Their treatment of Unicode, fonts, and languages differs.
- Provide the original error with its line number.
! Undefined control sequence. l.42 \topruleis actionable; “it failed” is not. - Keep the preamble and global formatting unchanged unless a specific change is required.
- Keep a version that compiled successfully. Reverting to it helps isolate which edit introduced the error.
The first five supply missing context; the sixth limits the cost of a bad change.
References have the same failure mode: an invented citation key may not exist in .bib, leaving [?] in the PDF. See the citation-checking guide.
Keep a compilable baseline project in EasyLaTeX, then ask AI to change only the intended section.