3

How can I use yasnippet to invoke LaTeX commands interactively?

If I use auctex to insert \usepackage it starts a minibuffer which lets me select the desired package (including tab completion). Similar for other commands like includegraphics. Is it possible to make yasnippet to do the same, i.e. help me choosing the package or image etc.?

4

You can tell yasnippet to interpret its type as a command rather than a snippet to interpret the body as lisp code.

The following example allows you to enter LaTeX environments (e.g., as if you had used C-c C-e):

# -*- mode: snippet -*-
# name: environment
# key: env
# type: command
# --
(call-interactively #'LaTeX-environment)

And here's another option to insert the \includegraphics macro:

# -*- mode: snippet -*-
# name: includegraphics
# key: ingr
# type: command
# --
(TeX-insert-macro "includegraphics")
  • Thanks. How can I apply this for example directly to includegraphics. I tried this one: # -*- mode: snippet -*- # name: image # key: img # type: command # -- (call-interactively #'TeX-insert-macro "includegraphics") but it doesn't work. – student Nov 29 '14 at 12:48
  • @student: I've updated the answer for \includegraphics. TeX-insert-macro is a function that requires an argument in this case (see its docstring by C-x f TeX-insert-macro RET), which is why it's not called with call-interactively. Take a look at the following links to get started with learning a little Emacs lisp if you'd like to know more: intro manual, Bastien Guerry's 15-minute intro, and the EmacsWiki node. – Dan Nov 29 '14 at 13:17
  • thanks, but this doesn't work either. It just inserts the sting \includegraphics and does nothing more. Additionally it is very slow. – student Nov 29 '14 at 14:25
  • @student: it works for me when all the necessary infrastructure is functioning. First, make sure it works with M-x TeX-insert-macro (you need to make sure that auctex recognizes your \usepackage{graphicx}). Second, you will need to enter the appropriate parameters in the minibuffer when it prompts. Third, if will allow you to do completion in a more friendly way if you use something like ido-mode. – Dan Nov 29 '14 at 14:36
  • Now it works. I figured out that the problem only occured when trying it in a test file in my home directory. But there also M-x TeX-insert-macro includgraphics didn't work. However in other directories it works. Seems a bit strange, but again, thanks a lot! – student Dec 21 '14 at 21:29

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.