2

If I drag and drop an image file (say myfile) to emacs when running in auctex mode, emacs just shows the file.

How can I make emacs to insert \includegraphics{myfile} instead of showing the picture?

Is it also possible to make emacs ask me for options such as (TeX-insert-macro "includegraphics") does?

1

AUCTeX doesn't have such feature builtin but you can use the code submitted to the AUCTeX' mailing list last year by Dieter Jurzitza: https://lists.gnu.org/archive/html/auctex/2014-11/msg00007.html (actually it was originally proposed by Michal Sojka in 2010). Once you have installed that code in your init file, you can activate this new minor mode in every LaTeX buffer with the following line to be inserted in your init file

(add-hook 'LaTeX-mode-hook 'AUCTeX-dnd-mode)

Here is the complete code:

(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)

(defcustom AUCTeX-dnd-format "\\includegraphics[width=\\textwidth]{%s}"
  "What to insert, when a file is dropped on Emacs window. %s is
replaced by the actual file name. If the filename is located
under the directory of .tex document, only the part of the name
relative to that directory in used."
  :type 'string
  :group 'AUCTeX)


;; Modified version
(defun AUCTeX-dnd-includegraphics (uri action)
  "Insert the text defined by `AUCTeX-dnd-format' when a file is
dropped on Emacs window."
  (let ((file (dnd-get-local-file-name uri t)))
    (when (and file (file-regular-p file))
      (if (string-match "/my/texinputs/path/to/images" file)
      (insert (format AUCTeX-dnd-format (file-name-nondirectory file)))
    (insert (format AUCTeX-dnd-format file))
    )
      )
    )
  )


(defcustom AUCTeX-dnd-protocol-alist
  '(("^file:///" . AUCTeX-dnd-includegraphics)
    ("^file://"  . dnd-open-file)
    ("^file:"    . AUCTeX-dnd-includegraphics))
  "The functions to call when a drop in `mml-mode' is made.
See `dnd-protocol-alist' for more information.  When nil, behave
as in other buffers."
  :type '(choice (repeat (cons (regexp) (function)))
                 (const :tag "Behave as in other buffers" nil))
  :version "22.1" ;; Gnus 5.10.9
  :group 'AUCTeX)


(define-minor-mode AUCTeX-dnd-mode
  "Minor mode to inser some text (\includegraphics by default)
when a file is dopped on Emacs window."
  :lighter " DND"
  (when (boundp 'dnd-protocol-alist)
    (if AUCTeX-dnd-mode
        (set (make-local-variable 'dnd-protocol-alist)
             (append AUCTeX-dnd-protocol-alist dnd-protocol-alist))
      (kill-local-variable 'dnd-protocol-alist))))
  • The dnd-mode works (thanks a lot). But The latex hook doesn't do you have any idea why? (For sake of completeness and because of possible link rod I pasted the relevant parts to your answer, hope this is ok). – student Oct 7 '15 at 17:22
  • What do you mean by "The latex hook doesn't [work]"? The mode is not activated when opening a LaTeX buffer? – giordano Oct 7 '15 at 18:57
  • Yes it is not activated when opening a LaTeX file. – student Oct 8 '15 at 6:06
  • I don't know, it works for me. Do you have other functions added to LaTeX-mode-hook? Are they run? – giordano Oct 8 '15 at 7:42
  • 1
    After some updates the hook works. – student Aug 30 '17 at 11:10

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.