Friday, 21 June 2013

Vim’s conceal feature and syntax file for all symbols defined by the unicode-math package

When writing Latex documents my editor of choice is Vim. I do not use any kind of additional plugins (not even the latex plugin) but only rely on the basic setup. I like to keep things as pure as possible. Thanks to Björn Winckler’s blog my love for Vim has grown even more as it allows you a rudimentary version of Emacs inline formula display via the conceal feature. How this is done you should read on Björn’s blog. Instead, I will show you how to make use of Vim’s conceal feature in combination with the unicode-math package (requires LuaLaTeX engine). The unicode-math package tries to give a command for every mathematical and logical symbol that is commonly used in modern mathematics and logic and therefore can be considered a necessary complement to the STIX-project. (To wit: here’s the list of all symbols so far defined by the package.) Of course most editors even the newest ones are slow to get a grip on luatex and on new packages like unicode-math. For the unicode-math package this basically means that they do not conceal the command and show you the symbol it calls in the source code like they do with standard mathematical latex commands. (E.g. most editors will allow you to conceal the command \to but they will not allow you to conceal \downzigzagarrow which is available via the unicode-math package) This also applies to Vim... At least it did so far. Some weeks ago I took the time to compile a tex.vim syntax file for Vim with a mapping of all commands defined by the unicode-math package to the symbols they stand for in the source code. Some important things to note about that:
(1) Trivial: As far as I can see you can only use my syntax file if you use Vim to edit your latex documents.
(2) Not Vim specific (Just replace “Vim” with your editor of choice which includes a conceal feature.): The Vim conceal feature only works with fonts that do contain the relevant symbol. Hence, the command actually being not just concealed but replaced by the right symbol is conditional on which font you use in your editor.
(3) Analogous (and trivial): Commands defined by the unicode-math package will only lead to the output of a symbol in the compiled document if the used font contains the relevant symbol.
(4) If you’re working on a rather long file that includes a lot of formulas and you use the conceal command set col=2, you may experience a considerable negative impact on scrolling speed. I had this problem with a calculus script and some philosophy related stuff. Here are some observations about how to diminish the problem and how not to solve it (Please go back in the line and notice the “not”.):
(4.1) How to not solve it: My tex.vim (Henceforth, I will refer to the one I created as tex.vim.) file resides in my /home/username/.vim/after/syntax/ folder (I’m using Linux as you may now have noticed. Surprise.) and my suspicion was that Vim was having trouble dealing with two distinct syntax files for one and the same language in two different locations. So I just copied everything in my tex.vim file to the original tex.vim file that Vim is shipped with and erased my tex.vim file. To put it simply: That didn’t solve anything. So I reversed it.
(4.2) Wild guess: My Laptop is too slow. (Works better on newer computers.)
(4.3) If (4.2) applies to you then I can only advise you to use a small font-size. That helps a little. But that’s about it so far. Anyone who has a decent idea: Share it!!!

Installation Instructions:
On the offical Vim scripts page I offer a *.tar file which contains two files: tex.vim and tex.vim~
The two files give you two mutually exclusive options (at least if you want to avoid a monster-slow-down-your-scrolling-speed-scenario).
Option 1: Take the file named tex.vim and copy it to your local Vim folder. In the Linux case this should be (/usr)/home/username/.vim/after/syntax/.
Option 2: Take the file named “tex.vim~ and rename it to “tex.vim and replace the original tex.vim syntax file standardly shipped with Vim. Again, in the Linux case the relevant file to replace should reside in /usr/share/vim/vim73/syntax/.

Note that the two files are not substitutable for each other. The file named “tex.vim just contains the mapping between the unicode-math commands and the symbols while the “tex.vim~ file contains the whole setup of syntax highlighting for TeX and LaTeX documents and for simplicity comes with a different syntax in order to achieve the mapping.

Both options are equally good as far as I can see. But I suggest you use Option 1 in case anything substantial and interesting should change in the standard tex.vim syntax file in the future.

Anyway, here’s the syntax file for Option 1: tex.vim. You can download Option 1 and Option 2 file from the offical Vim scripts page as a *.tar file. I hope this helps some people.

Take Care,