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.)

Drew
  • 75,699
  • 9
  • 109
  • 225
tinlyx
  • 1,276
  • 1
  • 12
  • 27
  • Does this answer your question? [Is there an easy way to detect how many \`C-u\`s a command was given?](https://emacs.stackexchange.com/questions/4173/is-there-an-easy-way-to-detect-how-many-c-us-a-command-was-given) – Drew Apr 23 '22 at 21:35
  • Another possible duplicate: https://emacs.stackexchange.com/q/37622/105 – Drew Apr 23 '22 at 21:35
  • @Drew I don't think they are the same. The two questions linked are about the the prefix `C-u` itself. My question here is about how to call them. – tinlyx Apr 23 '22 at 21:41
  • That's not what you asked, either in the question title or the question body. You asked *"How to define key-combinations involving multiple `C-u`s?"*, and again *"define a short key combination"*, and again *"define a key combination in init.el for C-u C-u M-x org-latex-preview"*. – Drew Apr 24 '22 at 01:45
  • And there are duplicates for the other end of this, as well: https://emacs.stackexchange.com/q/21626/105, https://emacs.stackexchange.com/q/60822/105. – Drew Apr 24 '22 at 01:53

1 Answers1

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)
NickD
  • 27,023
  • 3
  • 23
  • 42