vim-latex与vimtex如何选择

作为一个理科生,终生都离不开数学公式。而LaTeX绝对是处理数学公式的王者,在编辑LaTeX中的众多工具中,使用vim或neovim配合编辑LaTeX的插件又是王者中的王者。在众多的插件中,vim-latexvimtex是网络上提到最多的,也是使用者最多的插件,但是这两者该如何选择呢?经过本人的实践,最终选择了vim-latex.

选择依据分析

众多网络文章都提到了插件vimtex, 按其官方介绍:VimTeX is a modern Vim and Neovim filetype and syntax plugin for LaTeX files. , 或许就是这个modern让许多人选择了它。诚然vimtex是一个优秀的插件,我们来看一下它提供的功能(引用其README.md):

  • Document compilation with latexmk, latexrun, tectonic, or arara
  • LaTeX log parsing for quickfix entries using
  • Compilation of selected part of document
  • Support for several PDF viewers with forward search
  • Completion of
    • citations
    • labels
    • commands
    • file names for figures, input/include, includepdf, includestandalone
    • glossary entries
    • package and documentclass names based on available .sty and .cls files
  • Document navigation through
    • table of contents
    • table of labels
    • proper settings for 'include', 'includexpr', 'suffixesadd' and 'define', which among other things
      • allow :h include-search and :h definition-search
      • give enhanced gf command
  • Easy access to (online) documentation of packages
  • Word count (through texcount)
  • Motions (link to GIF demonstrations)
    • Move between section boundaries with [[, [], ][, and ]]
    • Move between environment boundaries with [m, [M, ]m, and ]M
    • Move between math environment boundaries with [n, [N, ]n, and ]N
    • Move between frame environment boundaries with [r, [R, ]r, and ]R
    • Move between comment boundaries with [* and ]*
    • Move between matching delimiters with %
  • Text objects (link to GIF demonstrations)
    • ic ac Commands
    • id ad Delimiters
    • ie ae LaTeX environments
    • i$ a$ Math environments
    • iP aP Sections
    • im am Items
  • Other mappings (link to GIF demonstrations)
    • Delete the surrounding command, environment or delimiter with dsc/dse/ds$/dsd
    • Change the surrounding command, environment or delimiter with csc/cse/cs$/csd
    • Toggle starred command or environment with tsc/tse
    • Toggle inline and displaymath with ts$
    • Toggle between e.g. () and \left(\right) with tsd
    • Toggle (inline) fractions with tsf
    • Close the current environment/delimiter in insert mode with ]]
    • Add \left ... \right) modifiers to surrounding delimiters with <F8>
    • Insert new command with <F7>
    • Convenient insert mode mappings for faster typing of e.g. maths
    • Context menu on citations (e.g. \cite{...}) mapped to <cr>
  • Improved folding (:h 'foldexpr')
  • Improved indentation (:h 'indentexpr')
  • Syntax highlighting
    • A consistent core syntax specification
    • General syntax highlighting for several popular LaTeX packages
    • Nested syntax highlighting for several popular LaTeX packages
    • Highlight matching delimiters
  • Support for multi-file project packages

而插件vim-latex(又称latex-suite), 按vimtexREADME.md

The main difference between VimTeX and LaTeX-Suite (aka vim-latex) is probably that VimTeX does not try to implement a full fledged IDE for LaTeX inside Vim. E.g.:

  • VimTeX does not provide a full snippet feature, because this is better handled by UltiSnips or neosnippet or similar snippet engines.
  • VimTeX builds upon Vim principles: It provides text objects for environments, inline math, it provides motions for sections and paragraphs
  • VimTeX uses latexmk, latexrun, tectonic or arara for compilation with a callback feature to get instant feedback on compilation errors
  • VimTeX is very modular: if you don't like a feature, you can turn it off.

这两个插件有相同的部分,比如说输入希腊字母,vim-latex在任何模式下都可以输出对应的希腊字母命令,而vimtex在数学模式中才输出希腊字母,相较而言vimtex更加保险一些,但是对于一个熟悉LaTeX的人来讲,这个事情都是知道的,影响不大,只能说vimtex更加细致一点。而vim-latex提供了完整的LaTeX的输入语法,同时提供了<++>标记方法,可以使用Ctrl+j快速的移动光标,这点是vimtex所无法比拟的。按vimtex的说法,vimtex把补全的任务交给了其他插件,但是就专业性和速度上讲,vim-latex绝对领先。

vimtex提供的较好的特性是光标在各环境中移动,这点是vim-latex不具备的功能。但是vimneovim的特性,使用搜索功能或数字键加hjkl方式移动光标,好像vimtex也没有占据足够的优势。

vimtex方便的更改环境功能,例如cse可以方便的更换为新的环境,但是vim-latex也可以使用<S-F5>快捷键快速更换环境,再则考虑到编写环境前作者肯定是规划好的环境,所以更改环境是一个低概率事件。此处注意,Neovim中同时按下组键<S+F5>(也就是Shift+F5)返回的是F17,并不能获得vim-latex所需的组合键<S-F5>!! 具体请参考keycode in terminal for vim&neovim, 于是做出修改:

~/.config/nvim/init.vim
1
2
3
4
map <F17> <S-F5>
imap <F17> <S-F5>
map <F19> <S-F7>
imap <F19> <S-F7>

修改完毕,在neovim中可以正常使用环境或命令的替换。很多人选择vimtex的一个重要原因是其可以配置增量编译,实现时时查看结果,但是LaTeX的精神本身就是!对于一个熟悉LaTeX的人来讲随时关注结果并不是必须的,除非公式比较复杂,而这只需要编译一下看看结果即可。时时编译的代价就是电脑消耗更多的资源,对于我的笔记本来说,风扇一直转发出的噪音会干扰我的写作思维。对于一个初入LaTeX的人,这或许是一个较好的选择,但是对于一个足够熟悉LaTeX的人来讲,不是必须的,也是符合Linux思维。

综上所述,vim-latexvimtex都是优秀的LaTeX插件,但是前者提供了完备的功能,后者的优势功能是一些低频率使用的功能,综合平衡的话还是选择vim-latex更好一些。由于二者并不冲突,所以可以同时配置二个插件,获取各自的功能这是一个比较折中的方案,但是不太符合Linux纯净化的哲学思想,所以我还是选择了前者,在没有足够的理由前应该不会同时安装二者。

作为可用的配置,下面将二者的配置文件列出,方便大家参考:

~/.config/nvim/lua/plg/vim-latex.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/env lua
--
-- vim-latex.lua
-- Copyright (C) 2023 feng <feng@arch>
--
-- Distributed under terms of the MIT license.
--
-- vim-latex默认的局势键
-- F1 帮助菜单
-- F5 插入环境选择对话框,若在导言区将弹出插入宏包对话框
-- F7 插入引用选择对话框
--绑定快捷键
-- -- 测试功能
-- inoremap <buffer><silent> <C-S-F5> <C-O>:call <SID>ChangeEnvironment(input('Environment? '))<CR>
-- noremap <buffer><silent> <C-S-F5> :call <SID>ChangeEnvironment(input('Environment? '))<CR>
vim.api.nvim_create_autocmd("FileType",{
pattern = "tex",
callback = function()
-- 配置F4打开模板
vim.api.nvim_set_keymap("n", "<F4>", ":TTemplate<CR>", {noremap = true, silent = true})
-- 配置pdflatex编译,原vim-latex中使用\ll 默认 xelatex 编译
vim.api.nvim_set_keymap("n", "<F6>", [[<cmd>!pdflatex % <CR>]], {noremap = true, silent = true})
-- 配置F8编译 aux辅助文件,直接根据数据库生成文献引用, 注意使用%:t:r 截取文件名
vim.api.nvim_set_keymap("n", "<F8>", [[<cmd>!bibtex %:t:r.aux <CR>]], {noremap = true, silent = true})
-- 配置F10编译多合一文件时,以当前目录名为主文件,以节省时间
vim.api.nvim_set_keymap("n", "<F10>", [[<cmd>!xelatex $(echo $PWD|awk -F"/" '{print $NF}').tex <CR>]], {noremap = true, silent = true})
end
})
-- 开启treesitter提供的语法高亮
vim.api.nvim_create_autocmd( 'FileType', { pattern = 'tex',
callback = function(args)
vim.treesitter.start(args.buf, 'latex')
vim.bo[args.buf].syntax = 'on' -- only if additional legacy syntax is needed
end
})
-- set for vim-latex/latex-suite
vim.g['Tex_Menus'] = 0
vim.g['Tex_Flavor'] = 'latex'
vim.g['Tex_IgnoreLevel'] = 8
vim.g['Tex_GotoError'] = 0
vim.g['Tex_DefaultTargetFormat'] = 'pdf'
-- 配置默认编译引擎为 xelatex
vim.g['Tex_CompileRule_pdf'] = 'xelatex -synctex=1 -interaction=nonstopmode -file-line-error-style $*'
-- 获取桌面环境,并根据桌面环境选择默认的PDF阅读器
if os.getenv("DESKTOP_SESSION") == "plasma" then
vim.g['Tex_ViewRule_pdf'] = 'okular'
else
vim.g['Tex_ViewRule_pdf'] = 'evince'
end
-- 统一设置为轻量级的mupdf阅读器
-- vim.g['Tex_ViewRule_pdf'] = 'mupdf'
--控制统计过程中的警告信息
vim.g['Tex_IgnoredWarnings'] = {}
--
vim.g['Tex_CustomTemplateDirectory'] = "~/.latex-templates/"
~/.config/nvim/lua/plg/vimtex.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#! /usr/bin/env lua
--
-- vimtex.lua
-- Copyright (C) 2023 feng <feng@archlinux>
--
vim.g['tex_flavor'] = 'latex'
vim.g['vimtex_view_method'] = 'zathura'
vim.g['vimtex_view_general_viewer'] = 'zathura'
vim.g['vimtex_view_general_options'] = [[--unique file:@pdf\#src:@line@tex]]
vim.g['vimtex_compiler_latexmk_engines'] = {
_ = '-xelatex'
}
vim.g['vimtex_compiler_progname'] = 'nvr'
-- vim.g['vimtex_view_general_options_latexmk'] = '--unique'
-- vim.g['vimtex_view_general_options_latexmk'] = '--reuse-instance'
--
-- A few examples of disabling default VimTeX features.
vim.g['tex_comment_nospell'] = 1
vim.g['vimtex_quickfix_mode'] = 1
vim.g['vimtex_indent_enabled'] = 1 -- turn off VimTeX indentation
vim.g['vimtex_imaps_enabled'] = 1 -- disable insert mode mappings (e.g. if you use UltiSnips)
vim.g['vimtex_complete_enabled'] = 1 -- turn off completion
vim.g['vimtex_syntax_enabled'] = 1 -- disable syntax conceal
vim.g['vimtex_syntax_conceal_disable'] = 1 --avoid the startup warning
-- 使一些符号以一种更加直观的方式显示出来
vim.g['tex_conceal'] = 'abdmg'
vim.g['conceallevel'] = 1
-- 更改maplocalleader避免与vim-latex的冲突
vim.g['maplocalleader'] = ","

参考文章

  1. VimAwesome
  2. vim-latex.sourceforge
  3. 用Vim高效地编辑LaTeX文档