# 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? Sep 19, 2017 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. Sep 21, 2017 at 9:56
• In a sufficiently new version, you don't need to restart Vim or press \lx. Just restart the compiler. github.com/lervag/vimtex/issues/2168 Sep 13, 2021 at 23:57

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.

• Note that method (1) might affect some other tools other than vim, as it "changes the default engine for latexmk". (2) method 2 will not work in the current version, because vimtex generates the command-line argument to specify the engine to use, and that overrides the argument provided. Instead use a directive line in the file itself (and restart the compiler), or see plugin vimtex - xelatex as default latex engine - Vi and Vim Stack Exchange for setting the default setting. Sep 13, 2021 at 23:59

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. Jul 17, 2019 at 6:46