Error

LaTeX Error: Environment undefined

The fix

The "Environment X undefined" error means you used \begin{X}...\end{X} for an environment LaTeX does not know. Fix it by loading the package that defines it, or correcting a typo. For example algorithm needs \usepackage{algorithm}, lstlisting needs \usepackage{listings}, and align needs \usepackage{amsmath}.

Not sure it worked on your document? Paste it into LetX and compile it, free and in the browser.

Why it happens

Environments are provided by packages. Writing \begin{align} without \usepackage{amsmath}, or \begin{lstlisting} without listings, leaves the name undefined. A misspelled environment name does the same thing.

Example

% Wrong. The environment's package is not loaded:
\begin{align} a &= b \end{align}   % needs amsmath
% Fix:
\usepackage{amsmath}

% Common environment -> package map:
%   align/gather/cases -> amsmath
%   algorithm          -> algorithm
%   lstlisting         -> listings
%   tikzpicture        -> tikz

Frequently asked questions

How do I know which package an environment needs?

Search the environment name plus "latex package". Frequent ones: align/gather/cases → amsmath, algorithm → algorithm/algpseudocode, lstlisting → listings, minted → minted.

I loaded the package but still get the error.

Check spelling and load order, and make sure the \usepackage is in the preamble (before \begin{document}). Some environments also need a library, e.g. TikZ shapes need \usetikzlibrary{...}.

Related LaTeX fixes

Browse all LaTeX problems & fixes, or start with the LaTeX guide.

Learn the topic properly

Try the fix in LetX

Open the editor, paste your code, and compile in seconds to see the error clear. Free, in your browser.

Open LetX Free