Vimtex and Xelatex

I tried for hours to use `vimtex` to compile a document that requires XeLaTeX. As the `vimtex` documentation says, the first line of my TeX document is `%! TEX program = xelatex`. However, I always get the following error:

/usr/local/texlive/2015/texmf-dist/tex/latex/fontspec/fontspec.sty|43 error| Fatal fontspec error: "cannot-use-pdftex"

So it look like PdfLaTeX is still used. Note that building a document that only requires PdfLaTeX work perfectly, and that the document I try to build with XeLaTeX is just a "Hello world" using a class that needs XeLaTeX and that compiles with `xelatex file.txt` command in a terminal.

• can you try with: `%!TEX TS-program = xelatex` and `%!TEX encoding = UTF-8 Unicode` on two first lines? – koleygr Sep 19 '17 at 23:10
• I know you "tried for hours", but have you actually put the line in the document and restarted vim afterwards? This is what I did wrong. – red_trumpet Sep 21 '17 at 9:56

If you are using `latexmk` to compile and you use `XeLaTeX` for the most of the time, I suggest change the defaut engine for your latexmk, just add one line to you `\$HOME/.latexmkrc`

``````\$pdflatex="xelatex %O %S";
``````

Or you might want to keep pdflatex as your latexmk option. I suggest changing the choice of vimtex: add these lines to your `\$HOME/.vimrc`

``````let g:vimtex_compiler_latexmk = {
\ 'executable' : 'latexmk',
\ 'options' : [
\   '-xelatex',
\   '-file-line-error',
\   '-synctex=1',
\   '-interaction=nonstopmode',
\ ],
\}
``````

`options` here stands for the options of `latexmk` when you use `\ll` to compile in vim.

You can refer to the Official Document to see more customizations for latexmk.

The exclamation point is in the wrong place. It should be this: `% !TEX program = xelatex` with the exclamation point right before the `TEX`. Then write the file, reload vimtex with `<leader>lx`, and recompile and see if that works.

• It work as well like I wrote it. All I had to do was restarting Vim. – Nakrule Jul 17 '19 at 6:46