I use yasnippet's defined in ~/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/latex-mode to insert small pieces of code when working with LaTeX. I recently started to use Sweave and therefore work on .Rnw files. However, I noticed that the (ya)snippets don't work anymore - hitting TAB after a trigger word just does not expand the defined snippet. Instead, nothing happens. I thought that's a simple problem to solve, just copy the snippets to ~/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/rnw-mode or .../noweb-mode. But that did not work either (meaning that hitting TAB does not expand trigger words). What I can do, however, is M-x yas/expand right after the trigger word, so it's working, but just not with TAB.

How can I use yasnippets from .Rnw files?

Update 1

I found http://groups.google.com/group/smart-snippet/browse_thread/thread/8206a23037f598b1/39ed819692df63ad?lnk=gst&q=rnw#39ed819692df63ad I have exactly that problem -- <tab> is bound by noweb-mode. However, the solution posted in the last post does not work for me/does not seem to have an effect on the problem (so, as above, TAB-expansion of trigger words does not work).

As described in that post, the mode changes from LaTeX to noweb depending on where the point is in the .Rnw file. I only need my LaTeX macros to expand when in LaTeX mode (of course).

In case this matters, I work with Emacs 24 on Mac OS X 10.7.3.

Update 2

C-h k TAB leads to (either with or without the solution posted on the above line):

<tab> runs the command noweb-indent-line, which is an interactive Lisp function
in `noweb-mode.el'.

It is bound to <tab>.


Indent the current line according to mode, after narrowing to this chunk.

So obviously, TAB is used by noweb.

Update 3

When the point is in a LaTeX part of the .Rnw file, then the modes displayed in the mode line are: LaTeX/MP Ref Fly yas pair vl Wrap Fill Noweb NWFL. If the point is in an R chunk of the file, then the modes are: ESS[S] [none] Rox Fly ElDoc pair vl Wrap Noweb NWFL. So indeed, the modes change when out of/within R chunks. However, I still can't TAB-expand snippets. My .emacs contains (which is obviously not working as expected):

(add-hook 'LaTeX-mode-hook
  '(lambda ()
     (make-variable-buffer-local 'yas/trigger-key)
     (setq yas/trigger-key [tab])))
  • It would help if you for both tried methods include what happened and what you expected to happen. – N.N. Mar 26 '12 at 14:11
  • With "does not work" I mean "it does nothing on hitting TAB". So I have a snippet align which expands to an align*-environment. If I type align and hit TAB, nothing is expanded. What I can do is M-x yas/expand and then the align*-environment is expanded, but just TAB does not work anymore as described in the post under Update – Marius Hofert Mar 26 '12 at 15:55
  • Please include that in your question instead by editing it. Makes it easier for others to find it. Would binding yas/expand to another key than Tan in rnw- and noweb-mode suffice? – N.N. Mar 26 '12 at 15:58
  • Okay, I added it above. – Marius Hofert Mar 26 '12 at 20:09

Finally, the problem is solved. One needs to switch to the tighter-tab branch of yasnippet. See https://github.com/capitaomorte/yasnippet/issues/231 for more details

| improve this answer | |

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.