1

I need to write Rmd files, but I can't imagine life without vimtex. Is there a way that I can use vimtex with Rmd files? Ideally, I would also like to keep the syntax highlighting that I have for Rmd files (via https://github.com/vim-pandoc/vim-rmarkdown).

Edit: In response to a comment by @filbranden, here is useful post that provides more information on Rmarkdown. Because the files are rendered using pandoc, one can mix R code with standard markdown with LaTeX.

As a simple example, an Rmarkdown file might contain code that looks like this:

# My document header
Here is an **emphasis**. Now here's some LaTeX:

\begin{align}
    \hat{\beta} = \hat{\alpha}^{3} + \epsilon.
\end{align}

In a .tex document I could use vimtex features, such as cse inside the align environment to toggle it to an align* environment. That's just one of many many features of vimtex that I would like to be able to use in Rmd.

@Karl Yngve Lervåg is the creator of vimtex, so if he doesn't know of a solution then that's not a great sign for there being one...

Is it a bad idea to "trick" vim into thinking that an Rmd is a Tex file? Then presumably I could use vimtex features? The downside is that the syntax highlighting would be messed up.

8
  • I don't have much experience with RMarkdown and I don't really use TeX that often... But aren't Markdown and TeX completely separate languages? How would vimtex help editing a Markdown file? I'm guessing it's maybe some extension to typeset math formulas in Markdown (or specifically RMarkdown), but I think it would be helpful if your question would spell out more clearly what kind of support you're expecting from vimtex on RMarkdown.
    – filbranden
    Nov 27, 2020 at 1:20
  • 1
    I don't know a good solution to this, but at least there are some relevant threads in the vimtex issues: see #1706 and #1729. Nov 27, 2020 at 11:04
  • 1
    @KarlYngveLervåg could I do something like you suggest here: github.com/lervag/vimtex/issues/1729#issuecomment-653228007. "The bundled ftplugin/rnoweb.vim already does runtime! ftplugin/tex.vim and thus loads vimtex. " Maybe I can make .Rmd files do that as well?
    – evencoil
    Nov 28, 2020 at 18:09
  • You might want to consider filing an issue about it under vim-pandoc or vim-rmarkdown... If TeX snippets are part of the syntax, integration with vimtex (or perhaps similar/equivalent key mappings) might be something they might want to add first class support for.
    – filbranden
    Nov 28, 2020 at 19:44
  • 1
    I think @filbranden's comment is very good: it would be best if integration is initiated from e.g. vim-pandoc or vim-markdown (or related plugins). Nov 29, 2020 at 20:51

1 Answer 1

1

After a lot of trial and error, I found a reasonable solution. I expect this is not a "good" solution, so would appreciate any thoughts as to how it could go wrong, and how to strengthen it.

To get vimtex features to load, I simply put this in ftplugin/rmd.vim:

set filetype=tex

That's it --- now all of the vimtex bindings and motions appear to load when I open an rmd.vim file. Many of the vimtex features obviously won't work correctly with .rmd files, such as the ones that involve compiling and expect a .tex document. However, the NVim-R plugin provides good compilation/viewing solutions for .rmd, and I am still able to use that after set filetype=tex, presumably because they have already been loaded.

A major source of confusion for me was that there is a difference in syntax highlighting behavior between calling :set filetype=tex manually after opening a file, and putting set filetype=tex in ftplugin/rmd.vim. Changing the filetype manually also changes the syntax highlighting, but changing the filetype inside the ftplugin script preserves the .rmd syntax highlighting. I think this is because scripts in syntax are called after those in ftplugin, but I'm not sure.

The upshot is that if you put the filetype call into ftplugin, you can both load vimtex features and preserve rmd syntax highlighting --- which is what I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.