2

I wanted to define a short key combination for preview all latex fragments in a org file. Per this answer:

To preview all latex fragments in the buffer pass C-u twice before calling org-latex-preview.

Now I use this sequence a lot and find it cumbersome to use multiple C-u's etc.

Is there way to define a key combination in init.el for C-u C-u M-x org-latex-preview?

(I am not sure how to express the C-u C-u before the actual function call as I don't know much about elisp.)

5

1 Answer 1

2

The doc string of org-latex-preview says

(org-latex-preview &optional ARG)

Toggle preview of the LaTeX fragment at point.

If the cursor is on a LaTeX fragment, create the image and
overlay it over the source code, if there is none.  Remove it
otherwise.  If there is no fragment at point, display images for
all fragments in the current section.

With a ‘C-u’ prefix argument ARG, clear images for all fragments
in the current section.

With a ‘C-u C-u’ prefix argument ARG, display image for all
fragments in the buffer.

With a ‘C-u C-u C-u’ prefix argument ARG, clear image for all
fragments in the buffer.

The C-u prefixes pass an optional ARG to the function:

  • C-u passes the argument '(4), i.e. a list of one element, the number 4.
  • C-u C-u passes the argument '(16).
  • C-u C-u C-u passes the argument '(64).

Basically every additional C-u multiplies the previous value by 4. See the Numeric Arguments section of the Emacs manual and the Prefix Command Arguments in the Emacs Lisp manual for these and other forms of prefix arguments.

In this case, what you need to do is call the function with an argument of '(16). So define a command that does that:

(defun my/org-preview-all-latex-fragments-in-buffer ()
   (interactive)
   (org-latex-preview '(16)))

and bind it to the key of your choice:

(define-key org-mode-map (kbd "C-c p") #'my/org-preview-all-latex-fragments-in-buffer)

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.