Tx · LaTeX & TikZ

How to Fix “LaTeX Error: Environment tikzpicture Undefined”

Published By the 9to5Science editorial team

At a glance

  1. 1. Load TikZ

    Add \usepackage{tikz} to the preamble of the document that is being compiled.

  2. 2. Check placement

    Put the package command before \begin{document}, and put the tikzpicture environment inside the document body.

  3. 3. Compile a test

    Build a minimal example; if tikz.sty is missing, install or update the PGF/TikZ package.

The error LaTeX Error: Environment tikzpicture undefined means LaTeX encountered \begin{tikzpicture} but does not know that environment. In a standard LaTeX document, the usual fix is to load TikZ in the preamble with \usepackage{tikz}.

This article shows where that command belongs, gives a minimal working example, and explains what to check if the error remains. If your log instead says that tikz.sty cannot be found, the issue is that TikZ is not installed or accessible to your TeX installation.

The usual fix: load the TikZ package

Put this line in the document preamble, after \documentclass{...} and before \begin{document}:

\usepackage{tikz}

The tikzpicture environment is defined by TikZ, which is part of the PGF system. Loading the package makes the environment and TikZ drawing commands available. For example:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw (0,0) -- (2,1);
\end{tikzpicture}

\end{document}

Compile this complete file with your normal LaTeX engine. It should produce a simple line from the coordinate (0,0) to (2,1).

Check where the code is compiled

The package must be loaded by the document that LaTeX actually compiles. If your picture is in a separate file included with \input or \include, put \usepackage{tikz} in the main document’s preamble, not inside the included file after \begin{document}.

For example, this arrangement works:

% main.tex
\documentclass{article}
\usepackage{tikz}

\begin{document}
\input{figure}
\end{document}

The file figure.tex can contain the tikzpicture environment, but it normally should not contain its own \documentclass or \begin{document}. If you compile figure.tex directly rather than compiling main.tex, LaTeX may not load the preamble from the main file. Compile the main file instead.

If the error persists

Check these causes in order:

  1. The package command is missing or misspelled. The package name is tikz, not tikzpicture; use \usepackage{tikz}.
  2. The command is in the wrong place. A \usepackage command belongs in the preamble, before \begin{document}.
  3. A different file is being compiled. Confirm that the compiled root or main file is the one containing the package command.
  4. The environment name does not match. Write \begin{tikzpicture} and \end{tikzpicture} with matching spelling and capitalization. LaTeX environment names are case-sensitive.
  5. TikZ is not installed. If the log says File 'tikz.sty' not found, install or update the PGF/TikZ package through your TeX distribution’s package manager, then compile again.

The last case is a different error from “environment undefined.” It means LaTeX tried to load the package but could not locate its files. Installing the package is the appropriate fix; changing the document’s drawing commands will not resolve a missing package.

Using TikZ with other packages

For ordinary TikZ drawings, \usepackage{tikz} is sufficient. If you use pgfplots to make plots, load that package as well, since it provides the axis environment and plotting features:

\usepackage{tikz}
\usepackage{pgfplots}

In many documents, pgfplots loads the needed PGF/TikZ components itself, but explicitly loading TikZ makes the requirement clear when the document also contains tikzpicture environments. To use optional TikZ libraries, load them after TikZ, for example \usetikzlibrary{arrows.meta}. A library extends TikZ; it does not replace loading the main package.

Common mistakes

  • Putting \usepackage{tikz} inside the document body. Package declarations go in the preamble.
  • Installing a package in a different TeX distribution. If you have multiple TeX installations, the editor may compile with one while you install packages into another. Check which LaTeX engine and distribution your editor is using.
  • Treating tikzpicture as a package. It is an environment supplied by the tikz package, so there is no \usepackage{tikzpicture} command.
  • Ignoring the first relevant log message. Later errors may be consequences of an earlier missing package or malformed preamble. Read the log from the first error and fix that cause before diagnosing subsequent messages.

For a quick diagnosis, reduce the document to the minimal example above. If it compiles, add your original content back a piece at a time. If it does not compile and reports that tikz.sty is missing, install PGF/TikZ in the TeX distribution used for compilation.

References

  1. The TikZ and PGF Packages
  2. TikZ & PGF Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.