There are some commonly used directives, like:
%!TeX encoding= UTF-8
%!TeX root=main.tex
%!TeX program=XeLaTeX
Of course this could be achieved using a separated latexmkrc file.
But I think it may be more convenient if directives could be used.
Some widely used editors understand these magic comments (http://tex.stackexchange.com/a/84687/9848):
E P R S
TeXShop x x x x
TeXStudio x x x x
TextMate ? x x ?
TeXworks x x x x
SublimeText o x x x
Atom o x x o
| | | |
x = yes | | | Spellcheck
o = no | | Root
? = ? | Program
Encoding
There is no plan, no. That is, TeX root is already implemented. The rest are not supported at the moment. However, we could have the discussion and if I agree it is useful I might agree to implement. There are some questions, though:
We could also just use local vimrc configurations as discussed in #710. Why is this not as good of a solution as using the directives?
Why is it not better to instead use a (local) .latexmkrc file to specify the compilation program? If you use vimtex, then you already use latexmk. And then it makes more sense, at least to me, to set the compiler option in a local .latexmkrc file.
What would the encoding option do? I am not sure what I should do when reading it. To me, it seems Vim already reads the files correctly no matter which encoding we use. And if you use UTF-8, then it should suffice to use \usepackage[utf8]{inputenc}?
It seems to me, overall, as if the various directives might need separate discussions. I think the main argument for these directives is collaborative editing.
For the spellcheck, you just use
% vim: set spelllang=en :
near the top or bottom of the file. Be careful with the _three_ ls... ;-)
Personally, I would have some use for the program= directive for testing one-off XeLaTeX documents. The detection itself could easily be done in a personal .vimrc (see this example gist). However, vimtex does not really have simple support for programs besides g:vimtex_latexmk_options.='-pdflatex=xxx'. Each time the file is reloaded, my script naively appends an additional -pdflatex=. I considered checking for the existence of one and replacing, but this approach seems very brittle.
Actually to me, the most useful one is %!TeX program=XeLaTeX. A latexmk.rc in the working directory does the job. BUT:
I write lots of notes everyday, each on a different topic and most of them are short (not a big project). Some of these notes do require xelatex rather than pdflatex. I don't think it's easier to manage all these notes in one file nor each in a different folder. So there are multiple .tex files in one folder. It will be much more convenient to put just one line in the particular tex file to declare the tex engine. rather than to manage lots of local vimrc or latexrc files..
Hmmm... I don't see why in such a mixed environment you can't simply use xelatex for all of your files. At least for consistency in fonts treatment or whatever....
Anyway, a simple script like (notice: it has to be made smarter, but well, you get the idea) should work...
#!/bin/bash
#
program=pdflatex
head -5 "$1" | grep "XeLaTeX" && program=xelatex
$program "$1"
...and now stick a % XeLaTeX comment in the first five lines of the file you want to compile with xelatex.
Disclaimer: the script is way too na茂ve to work; it should at list parse the argument to discard the options and apply the "grep" to the file name, but well... you got the idea.
@Rmano , Thank you!
Yes, yes there are lots of workarounds and @andymass also gave a solution.
But don't you think just one line %!TeX program=XeLaTeX is a very convenient way to control the engine?
The reason that I do not want to use xelatex for all files is that xelatex is much slower on Windows than it on OSX or Linux.
On the other hand, it has been a habit/custom/practice/tradition to use these directives for people using TeXShop/TeXworks/TeXStudio. Actually I moved to vim from TeXworks half a year ago. This compatibility would make people who immigrate from other TeX editors much happier. I think the target users for vimtex is not only vim users who write LaTeX, but also LaTeX users who would like to use Vim. For many of the latter group, these directives are helpful.
I'm sold. I'm adding support for the TeX program line. I hope you don't mind if you borrow your code, @andymass.
@lervag of course! (I must say I didn't test it much). Thanks for this very speedy update. Unfortunately, I made a transposition in the list. --xtx should be on the xetex line, not the pdftex line:
\ 'context (xetex)' : '-pdflatex=''texexec --xtx''',
\ 'context (luatex)' : '-pdflatex=context',
\ 'context (pdftex)' : '-pdflatex=texexec',
Thanks, @andymass. I've updated accordingly. I also added some documentation. Please let me know what you think (see 2f1b86c). @Rmano @haoyun
Sorry to bump an old issue. I just noticed that the TeX program command does not work if g:vimtex_latexmk_progname is set (specifically, to nvr as suggested in the documentation). This is not a big deal for me, so I thought I'd ask if I should open a new issue or if I'm missing something obvious here.
That's strange. I don't really see how those two should conflict. It would be good if you could provide a minimal example and open a new issue.
Never mind, I found out what's happening. The TeX program directive is read when loading the file, not when starting compilation. So when I added the TeX program = lualatex line and started <local-leader>ll, and it was still using pdflatex, I closed gvim, checked my vimrcs, commented out the prime suspect, and restarted. And lo and behold, it worked, and I posted...
Is this (directive read on load, not compilation) intended behavior?
Hmm. Good question. It was the simplest thing to do. Would you really make use of changing this during a single editing session?
Not twice, but putting in that line after having started writing is something I'd expect to do.
(I'm using lualatex for letters where corporate identity dictates fonts not packaged for LaTeX, so those are frequent one-off files, not a big project like a book.)
In any case, it's no big deal, but it certainly something I didn't expect and might merit mention in the documentation.
I decided to update the docs with a hint to the VimtexReload command (mapped to \lx by default). This should work and provde the desired behaviour, I think.
Yep, that's perfectly fine for me -- especially with VimtexReload, which I was not aware of, thanks for pointing it out!
No problem. VimtexReload is one of my "secret weapons" when developing. Very useful!
I was also bitten by this issue. You have to use VimtexReload to make certain changes take effect.
Most helpful comment
Thanks, @andymass. I've updated accordingly. I also added some documentation. Please let me know what you think (see 2f1b86c). @Rmano @haoyun