0

Consider the following content of a file called image-tooltips.el which I source into my .emacs file via load-file. Displaying the image tooltip works if I put a ;\includegraphics{myfile.jpg} into this file after executing eval-buffer.

However if I load a latex-file also including an includegraphics stanza, it doesn't work.

I turned flyspell mode off and also tried to but the following code between a

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

;;; code below

))

Here is the content of image-tooltips.el:

(defvar image-tooltip-re (concat  "\\\\includegraphics{\\(?1:.*\\."
                                 (regexp-opt '("png" "PNG" "JPG" "jpeg"
                                               "jpg" "JPEG" "eps" "EPS" "pdf"))
                                 "\\)}")
 "Regexp to match included images")


(defun image-tooltip (window object position)
  (save-excursion
    (goto-char position)
    (let (beg end imgfile img s)
      (while (not (looking-at image-tooltip-re))
        (forward-char -1))
      (setq imgfile (match-string-no-properties 1))
      (when (file-exists-p imgfile)
        (setq img (create-image (expand-file-name imgfile)
                                'imagemagick nil :width 800))
        (propertize "Look in the minibuffer"
                    'display img)))))


(font-lock-add-keywords
 nil
 `((,image-tooltip-re
    0 '(face font-lock-keyword-face
             help-echo image-tooltip))))

(font-lock-fontify-buffer)
(setq x-gtk-use-system-tooltips nil)

;\includegraphics{image.jpg};; works

How can I make this work automatically if I load a tex-file and also if flyspell mode is enabled (the matched strings should be ignored by flyspell and the image should show on mouseover instead).

The code above is a slightly adapted version of Displaying image overlays on image filenames in Emacs

Edit Same problem with the code from https://emacs.stackexchange.com/a/27428/2323.

Edit 2 Here is a minimal .emacs file where I get the problem with John Kitchin's solution that it works only after explicitely reloading latex mode, i.e. load a .tex file (where latex-mode is loaded automatically) the it doesn't work but it works afer an M-x latex-mode. Also if I add an \includegraphics{image.jpg} line it sometimes doesn't recognize it, but after reloading latex mode it works. However it should recognize it without reloading latex-mode.

(require 'package)
(add-to-list 'package-archives 
    '("marmalade" .
      "http://marmalade-repo.org/packages/"))
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)

(setq package-archive-enable-alist '(("melpa" deft magit)))

(package-initialize)

(setq ispell-program-name "hunspell")
(setq ispell-local-dictionary "de_DE")
(setq ispell-local-dictionary-alist
      '(("de_DE" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil nil nil utf-8)
    ("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil nil nil utf-8)
    ))



(add-hook 'LaTeX-mode-hook 'turn-on-flyspell)
(add-hook 'LaTeX-mode-hook 'flyspell-buffer)



(setq x-gtk-use-system-tooltips nil)

(defvar image-tooltip-re (concat  "\\\\includegraphics{\\(?1:.*\\."
                  (regexp-opt '("png" "PNG" "JPG" "jpeg"
                        "jpg" "JPEG" "eps" "EPS" "pdf"))
                  "\\)}")
  "Regexp to match included images")


(defun image-tooltip (window object position)
  (save-excursion
    (goto-char position)
    (let (beg end imgfile img s)
      (while (not (looking-at image-tooltip-re))
        (forward-char -1))
      (setq imgfile (match-string-no-properties 1))
      (if (file-exists-p imgfile)
      (progn
        (setq img (create-image (expand-file-name imgfile)
                    'imagemagick nil :width 800))
        (propertize "Look in the minibuffer"
            'display img))
    "No file found"))))

(defun img-match (limit)
  "Match bar in at the end of a word."
  (when (re-search-forward image-tooltip-re limit t)
    (flyspell-delete-region-overlays (match-beginning 1)
                     (match-end 1))
    t))

(add-hook 'LaTeX-mode-hook
      (lambda ()
        (font-lock-add-keywords
         nil
         '((img-match 1 '(face font-lock-keyword-face
                   help-echo image-tooltip))))))

1 Answer 1

1
+50

You probably want something like this. I think you need a function that matches the img, so you can get the region and remove flyspell overlays. Also you need to modify the add-hook call a bit.

(setq x-gtk-use-system-tooltips nil)

(defvar image-tooltip-re (concat  "\\\\includegraphics{\\(?1:.*\\."
                  (regexp-opt '("png" "PNG" "JPG" "jpeg"
                        "jpg" "JPEG" "eps" "EPS" "pdf"))
                  "\\)}")
  "Regexp to match included images")


(defun image-tooltip (window object position)
  (save-excursion
    (goto-char position)
    (let (beg end imgfile img s)
      (while (not (looking-at image-tooltip-re))
        (forward-char -1))
      (setq imgfile (match-string-no-properties 1))
      (if (file-exists-p imgfile)
      (progn
        (setq img (create-image (expand-file-name imgfile)
                    'imagemagick nil :width 800))
        (message (propertize "Look in the minibuffer"
                 'display img)))
    "No file found"))))

(defun img-match (limit)
  "Match bar in at the end of a word."
  (when (re-search-forward image-tooltip-re limit t)
    (flyspell-delete-region-overlays (match-beginning 1)
                     (match-end 1))
    t))

(add-hook 'LaTeX-mode-hook
      (lambda ()
        (font-lock-add-keywords
         nil
         '((img-match 1 '(face font-lock-keyword-face
                   help-echo image-tooltip))))))
7
  • Thanks. That's in principle what I want, but if I load a .tex file it doesn't work initially after loading LaTeX-mode automatically. If I reaload LaTeX-mode via M-x latex-mode it works. Any idea what's the missing point here?
    – student
    Commented Oct 29, 2016 at 7:56
  • See my edit above for a minimal .emacs file which illustrates the problem.
    – student
    Commented Oct 29, 2016 at 8:26
  • Maybe the file is opened in Tex-mode and not latex-mode? Commented Oct 29, 2016 at 12:13
  • No, it's opened in latex mode.
    – student
    Commented Oct 30, 2016 at 8:37
  • Did it work for you?
    – student
    Commented Oct 31, 2016 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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