Tx · LaTeX & TikZ

Fix “Undefined Control Sequence \includegraphics” in TeXstudio

Published By the 9to5Science editorial team

At a glance

  1. 1. Load graphicx

    Add \usepackage{graphicx} to the preamble, before \begin{document}.

  2. 2. Check the first error

    Read the compiler log at the first occurrence; a missing package produces a different error.

  3. 3. Verify the build

    Compile a minimal test and confirm TeXstudio is building the intended file with the expected TeX installation.

The LaTeX command \includegraphics is defined by the graphicx package. TeXstudio is an editor and build interface; updating it does not itself define or remove LaTeX commands. The usual fix is to load graphicx in the preamble. If that line is already present, check whether TeXstudio is compiling the intended main file and using the TeX installation you expect.

Load the package in the preamble

Put \usepackage{graphicx} after \documentclass{...} and before \begin{document}. For example:

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\includegraphics[width=0.6\linewidth]{photo.png}
\end{document}

This example expects an image named photo.png in the same directory as the .tex file. You can change the filename and width to suit your document. The graphicx package also provides options such as width, height, scale, and angle for controlling the image.

To test whether the command is recognized without needing an image file, use the demo option. It displays a placeholder box instead of loading an image:

\documentclass{article}
\usepackage[demo]{graphicx}

\begin{document}
\includegraphics[width=0.6\linewidth]{placeholder}
\end{document}

The demo option is only for testing; remove it when you want LaTeX to include the actual image.

Check the compiler log and project setup

If the package line is already in your document, work through these checks:

  1. Make sure it is in the main document. In a project split across several files, the file containing \documentclass is normally the main file. A package loaded in a separate chapter or included file may not be loaded before LaTeX processes the image command.
  2. Check that the line is active. A line beginning with % is a comment, so % \usepackage{graphicx} does not load the package. Also check that the line was not placed after \begin{document}.
  3. Check the exact spelling and case. LaTeX commands are case-sensitive: \includegraphics is not the same as \IncludeGraphics or \includeGraphics.
  4. Read the first relevant error in the log. Later errors can be consequences of an earlier problem. Compile the demo example above to separate a document issue from a project configuration issue.
  5. Confirm which file TeXstudio builds. A project may have a root or master document setting that points to another .tex file. Check the file named in the build log and make sure it is the one that loads graphicx.
  6. Check the selected TeX distribution. TeXstudio can invoke an installed distribution such as TeX Live or MiKTeX. After an update, its configured commands or paths may point to a different installation than before. Verify the compiler and distribution in TeXstudio’s build settings, then compile again.

Distinguish an undefined command from a missing package

The error Undefined control sequence at \includegraphics usually means the command was not defined when LaTeX reached it—most often because graphicx was not loaded in the active document.

A different message, such as LaTeX Error: File 'graphicx.sty' not found, means LaTeX tried to load the package but could not find its files. In that case, install the package or repair/update the TeX distribution that TeXstudio is using. Reinstalling TeXstudio alone will not install a missing LaTeX package.

If the minimal test works but your project does not, compare the main-file selection and preamble of the test with those of your project. If even the test fails with a missing-package error, focus on the TeX distribution and its package installation rather than the image filename.

Common mistakes

  • Putting the package command after \begin{document}: package loading belongs in the preamble.
  • Adding graphicx to a subfile only: load it from the main document’s preamble.
  • Confusing image-file errors with command errors: an incorrect image path normally produces a file-not-found error, not an undefined \includegraphics command.
  • Changing TeXstudio before checking the log: the log identifies whether the problem is an undefined command, a missing package, or a different build configuration.
  • Assuming the editor update changed LaTeX syntax: \includegraphics is still provided by graphicx; the key question is whether the selected compiler loaded that package.

References

  1. The LaTeX Graphics Bundle

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