0

In org-mode, I use preview images for various things such as latex formula and linked images (e.g. [[file:xxx.png]]]). At times, I need to change the code for these and switch back to the code view. I know I can use things like C-c C-x C-l for LaTeX and M-x org-toggle-link-display.

Is there a simpler way to switch back to the source, e.g. by double clicking the image or using one key-binding/command on it, for all types of preview images?

  • There was one (at least for LaTeX fragments): the very busy C-c C-c would restore the code view (I don't remember if it toggled, but I think not). However, that was deleted some time ago in order to make the key less busy. I don't think there is anything like what you are looking for now, but it should not be too hard to add a function that figures out what org-element is at the location of the event and does the necessary to flip the state if it finds the element to be either a LaTeX fragment or a link (and does nothing otherwise). – NickD Jun 19 at 13:18
1

Along the lines of my comment, here is a function that toggles the state of a link or a LaTeX fragment at point. It's bound to C-c t (for "toggle") only because that was undefined in my Org mode keymap:

(defun ndk/toggle-state-at-point ()
  (interactive)
  (let ((ctx (org-element-context)))
    (pcase (org-element-type ctx)
      ('link           (org-toggle-link-display))
      ('latex-fragment (org-latex-preview)))))

(define-key org-mode-map (kbd "C-c t") #'ndk/toggle-state-at-point)

It does nothing if the context at point is not a link or a LaTeX fragment.

N.B. org-latex-preview was introduced in Org mode 9.3 and is replacing org-toggle-latex-fragment which is deprecated. For Org mode versions < 9.3, replace the org-latex-preview above with org-toggle-latex-fragment. Thanks to @tinlyx for pointing out the problem.

| improve this answer | |
  • Thanks. It seems org-toggle-latex should be org-toggle-latex-fragment in my Emacs 26. – tinlyx Jun 20 at 4:03

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.