ubuntu20.04下vim+vim-latex+latex-live-preview编写中文latex文档

我一直都想实现vim下编写中文latex文档,也是被《国外小哥1700页数学笔记》这篇文章anli了,之后开始满网查找资料想实现报道中的效果,一直未能如愿。

如下记录我使用vim+vim-latex+latex-live-preview编写中文latex文档的过程,以便引导如我般困惑的道友入门。

Latex的学习,请参考我的另外文章:

效果图

先给大家看下我实现的效果图(左边是vim,右边是okular)

在这里插入图片描述
系统环境

  • ubuntu 20.04
  • i3wm(平铺式窗口管理器,替代了ubuntu的默认桌面)
  • okular(pdf阅读器)

安装中文环境的latex套件

sudo apt-get install texlive-latex-base latex-cjk-all texlive-latex-extra texlive-xetex texlive-publishers

texlive-latex-base, LaTex的基础包,有需要再安装其他的
latex-cjk-all, 中日韩等语言环境
texlive-latex-extra,编译时需要
texlive-xetex,编译带中文的文档
texlive-publishers,support for publishers, theses, standards, conferences, etc.

安装vim-plug

mkdir ~/.vim/{autoload,plugged}/
cd ~/.vim/autoload/
wget https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

在~/.vimrc 中配置如下

call plug#begin('~/.vim/plugged')
"添加待安装的插件
call plug#end()

安装vim-latex和latex-live-preview

  • vim-latex,编译latex文档,使生成pdf文档
  • latex-live-preview,用pdf软件实时预览latex文档的编写

使用vim-plug安装上面2个插件,在~/.vimrc 中配置如下

扫描二维码关注公众号,回复: 11377173 查看本文章
call plug#begin('~/.vim/plugged')
Plug 'jcf/vim-latex'
Plug 'xuhdev/vim-latex-live-preview', { 'for': 'tex' }
call plug#end()

配置vim-latex和latex-live-preview

在~/.vimrc中配置如下

filetype indent on
let g:tex_flavor='latex'
set iskeyword+=:
let g:Tex_CompileRule_pdf='xelatex -interaction=nonstopmode $*'
let g:Tex_DefaultTargetFormat="pdf"
let g:Tex_ViewRule_pdf = 'okular --unique'
let g:livepreview_previewer = 'okular --unique'
autocmd Filetype tex setl updatetime=1

这样在编写latex带中文文档时,就可以实现如上一样的效果图。

一个例子

使用vim 编辑后缀为“.tex”的文件,例如 test.tex,键入如下代码并保存:

\documentclass{article}
\usepackage[UTF8]{ctex}
\begin{document}
hello,你好
china
海上生明月
\end{document}

使用latex-live-preview

在命令模式下输入“:LLPStartPreview”即可实时预览latex文档。
也可以在vimrc中,配置如下代码,以便使用键映射:

nmap <F12> :LLPStartPreview<cr>

注意:latex-live-preview 只是实时预览文件,没有在当前目录生成pdf文件。

使用vim-latex

在命令行模式下

  • 键入“\ll”,即可编译test.tex文件生成同名的test.pdf文件(好像最后一步需要回车)
  • 键入“\lv”,即可打开pdf软件预览生成的test.pdf文件

完整的~/.vimrc配置如下

我使用的完整的vimrc配置如下,按照如上步骤安装相应的软件,再配置如下的vimrc配置,即可实现编写带中文的latex文档。

filetype indent on
let g:tex_flavor='latex'
set iskeyword+=:
let g:Tex_CompileRule_pdf='xelatex -interaction=nonstopmode $*'
let g:Tex_DefaultTargetFormat="pdf"
let g:Tex_ViewRule_pdf = 'okular --unique'
let g:livepreview_previewer = 'okular --unique'
autocmd Filetype tex setl updatetime=2

call plug#begin('~/.vim/plugged')
Plug 'jcf/vim-latex'
Plug 'xuhdev/vim-latex-live-preview', { 'for': 'tex' }
call plug#end()

不足点

  1. 上面的不足点就是,\ll 后需要按“回车键”。
  2. vim-latex的编译预览没有在一起,只能先编译,再预览。
  3. latex-live-preview在使用时好像占用资源很高,我使用virtualBox搭建的虚拟机,在编译latex时很容易就机器卡死。

如上不足,若有道友解决了,希望能共享下,也让我学习学习,谢谢!

题外话,不喜欢使用vim编写latex的道友,也可以图形化软件 texmarker

还有一个更好的Latex的环境,可以把编译跟实时预览结合起来,请参考我的另一篇文章《ubuntu20.04下vim+vimtex编写latex的利器

参考链接

  1. https://blog.csdn.net/shangwp3/article/details/104526314
  2. https://blog.csdn.net/zhangsming/article/details/43341565
  3. https://www.jianshu.com/p/d185aad1f915
  4. https://blog.csdn.net/weixin_41655470/article/details/79299147#t1
  5. https://blog.csdn.net/rankun1/article/details/78775404

猜你喜欢

转载自blog.csdn.net/tianzong2019/article/details/106422708