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}.

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{...}.

Try the fix in LetX

Open the editor, paste your code, and compile in 1 second to see the error clear — free, in your browser.

Open LetX Free