What is the easiest way to detect if one has used an \includegraphics{*.eps} in a tex document?

(motivation: When I have used this I would like to not use pdflatex and instead use the combination of latex and dvipdfm (I will take care of these).)


I think nowadays one should drop the extension for image files completely and do only \includegraphics{<file-name-sans-extension>}. The used engine will pick up the right file acc. to the extensions described in \Gin@extensions in .def files provided by LaTeX support for color and graphics. If you use C-c C-c within AUCTeX, you can control the engine with Options for TeX Processors described in the manual.

A general approach to parse the argument of \includegraphics over possible master and child files would be to use the built-in parser in AUCTeX. I suggest to write a style file for this purpose which can look like this:

;;; includegraphics-parser.el --- Dummy AUCTeX style to parse argument
;;; of \includegraphics

;;; Code:

;; Needed for auto-parsing.
(require 'tex)

(TeX-auto-add-type "includegraphics-parser" "LaTeX")

(defvar LaTeX-includegraphics-parser-regexp
    1 LaTeX-auto-includegraphics-parser)
  "Matches the mandatory argument of \\includegraphics.")

(defun LaTeX-includegraphics-parser-auto-prepare ()
  "Clear `LaTeX-auto-includegraphics-parser' before parsing."
  (setq LaTeX-auto-includegraphics-parser nil))

(defun LaTeX-includegraphics-parser-auto-cleanup ()
  "Search for \"eps\" in parsed arguments of \\includegraphics."
  (when (string-match "\\.eps"
              (mapconcat #'car (LaTeX-includegraphics-parser-list) "|"))
    (message "Found eps file extension")
    (sit-for 2)))

(add-hook 'TeX-auto-prepare-hook #'LaTeX-includegraphics-parser-auto-prepare t)
(add-hook 'TeX-auto-cleanup-hook #'LaTeX-includegraphics-parser-auto-cleanup t)
(add-hook 'TeX-update-style-hook #'TeX-auto-parse t)

 (lambda ()
   ;; Make sure graphicx.el is loaded
   (TeX-run-style-hooks "graphicx")

   ;; Hook into AUCTeX parser
   (TeX-auto-add-regexp LaTeX-includegraphics-parser-regexp))

;;; includegraphics-parser.el ends here

Set the variable TeX-style-private to a directory of your choice, e.g.:

(setq TeX-style-private "~/.emacs.d/mystyles")

and save includegraphics-parser.el there.

Customize TeX-parse-self to t and restart Emacs.

Put something like this in your .tex file


and hit C-c C-n to reparse your file.

  • Thanks, but this does not answer the original question. A solution could be a regexp searching \\includegraphics{[^}]*\.eps. I asked my question to know if there is a better solution. – Name Jan 7 '18 at 13:51
  • @Name - Your question was "what is the easiest way". Using a regexp doesn't fit the bill ;-) See my updated answer. – Arash Esbati Jan 8 '18 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.