惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
L
LINUX DO - 最新话题
C
Check Point Blog
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
S
Security @ Cisco Blogs
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
D
Docker
Engineering at Meta
Engineering at Meta
AWS News Blog
AWS News Blog
S
Security Affairs
U
Unit 42
P
Palo Alto Networks Blog
V
Visual Studio Blog
Y
Y Combinator Blog
D
DataBreaches.Net
Forbes - Security
Forbes - Security
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
Security Latest
Security Latest
aimingoo的专栏
aimingoo的专栏
Simon Willison's Weblog
Simon Willison's Weblog
A
Arctic Wolf
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hacker News: Front Page
博客园 - 司徒正美
博客园 - Franky
宝玉的分享
宝玉的分享
TaoSecurity Blog
TaoSecurity Blog
Latest news
Latest news
Scott Helme
Scott Helme
MongoDB | Blog
MongoDB | Blog
量子位
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
P
Privacy International News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN Mintty Tips and Configurations Generating Table of Contents for Markdown with Tagbar Convert Python Script to Exe on Windows with Pyinstaller Ubuntu on Windows Missing after Windows Update 使用代理加速 Mac 终端下载速度 My Experience with Several Zsh Plugin Managers 深圳租房小记 How to Install zplug inside Docker Container Why don't settings inside bashrc or bash_profile take effect? Setting Up Locale in Linux 谷歌 Adsense 申请及在 Hugo 中的配置 How to Write Algorithm Pseudo Code in LaTeX Nifty Nvim Techniques That Make My Life Easier -- Series 4 A Few Grammar Questions in Writing How to Read and Write Images with Unicode Paths in OpenCV Creating A Professional Table in LaTeX with booktabs How to Create Proper Folding for Vim/Nvim Configuration Linux Tips and Tricks -- s1 JPEG Image Orientation and Exif How Do I Show the Current File Path In Neovim? JPEG Image Quality in PIL Difference between view, reshape, transpose and permute in PyTorch Convert PIL or OpenCV Image to Bytes without Saving to Disk Fast Movement and Navigation Inside Vim or Neovim Unintuitive Behaviour of Case Sensitivity in Python glob Binding Keys in Zsh 几把机械键盘试用体验 Nvim Autocompletion with Deoplete Converting Markdown to Beautiful PDF with Pandoc Exclusive and Inclusive Motion in Neovim/Vim Nifty Nvim Techniques Which Make My Life Easier -- Series 3 Why Doesn't Jedi Autocompletion Work for Some Methods Vim-like Editing inside Browser Markdown 生成 HTML 时汉字之间出现多余空格问题 小米 9 安装谷歌商店(Google Play Store)与相关配置 Create Mappings That Take A Count in Neovim Spell Checking in Nvim English Words Completion inside Neovim/Vim How to Use Python Inside Vim Script with Neovim Nifty Little Nvim Techniques to Make My Life Easier -- Series 2 Setting up Ultisnips for Neovim Mac 上罗技 M590 鼠标设置 Nifty Little Nvim Techniques to Make My Life Easier -- Series 1 Manipulating Images with Alpha Channels in Pillow Sublime Text Regular Expression Cheat Sheet Cropping Rotated Rectangles from Image with OpenCV Boosting Your Productivity on Terminal with Zsh and Plugins 最新版 Rime 输入法使用 (2022 更新) Display Image with Pillow inside Ubuntu on Windows Faster Directory Navigation with z.lua Cmder Advanced Configurations Nvim-qt Settings on Windows 10 Tmux Plugin Install and Management How to Debug Python Code in Terminal Markdown Writing and Previewing in Neovim -- A Complete Guide Line Number Settings for More Efficient Movement in Neovim 两个大规模中文语料库介绍以及处理 Windows 系统下几款程序员不可不用的神器 我的 2018 阅读清单 A Complete Guide to Neovim Configuration for Python Development How Is Newline Handled in Python and Various Editors? Two Issues Related to ImageFont Module in PIL 在 Listary 中调用 GoldenDict 或欧路词典查单词 Reading and Writing Text Files on Windows The Mathematics behind Font Shapes --- Bézier Curves and More 快速识别图片字体:字体识别工具介绍 Deoplete Failed to Load at Startup after Updating Python neovim Package What Is The Difference between pip, pip3 and pip3.6 Shipped with Anaconda3? Windows 10 系统下 Neovim 安装与配置 How to Use shutil.move() on Windows and Linux
A Complete Guide on Writing LaTeX with Vimtex in Neovim
2019-03-26 · via jdhao's digital space
Update log
  • 2021-02-20: Add inverse search settings on Windows and macOS

Previously, I have written a post on how to write and compile LaTeX source code in Sublime Text. Now that I am familiar with Neovim, I want to do everything related to text editing inside Neovim. In this post, I would like to share how to configure Neovim for LaTeX editing and previewing.

My complete config for vimtex can be found here (search vimtex) on my nvim-config repo .

Pre-requisite#

Before we begin, you should make sure that you have installed the following software and plugins.

  • TeX Distribution: TeX Live is preferred since it is bundled with the possible many tools you will ever need for writing LaTeX.
  • Auto-complete plugin: Since I use Neovim, so I recommend using deoplete.
  • Snippet Engine and snippet plugin: I recommend Ultisnips along with the many snippets provided by plugin vim-snippets.

Then we need to install the LaTeX plugin for Neovim. Vimtex is a great choice. To install it, we can use vim-plug:

Faster LaTeX source file editing#

Auto-completion#

In writing LaTeX source code, auto-completion is crucial for fast editing and improves our efficiency dramatically. Vimtex supports completion for citations, labels (for figure, equation, table, section, etc.), commands, environments, packages and document classes and more. For more documentation on completion, see :h vimtex-completion.

Vimtex can work with several completion engines such as deoplete, YouCompleteMe, ncm2. Since I use deoplete for completion, I will just introduce how to configure it for deoplete1. To make vimtex work with deoplete, add the following setting to your init.vim:

" This is new style
call deoplete#custom#var('omni', 'input_patterns', {
      \ 'tex': g:vimtex#re#deoplete
      \})

After that, you should be able to use auto-completion.

How to quickly insert an environment?#

Vimtex supports autocompletion through autocompletion engines such as deoplete. After configuring vimtex to use deoplete, you should be able to use the snippet provided by Ultisnips2.

Ultisnips is a snippet engine, which uses vim-snippets to provide the actual snippet for various file types. To insert an figure environment, type fig in insert mode and press Tab.

To insert an enumerate environment, type enum in insert mode and press Tab. The complete list of snippets provided by vim-snippets can be found here.

How to close an environment?#

After typing \begin{ENV}, how do we automatically close it with \end{ENV}? With vimtex, you can press ]] in insert mode to auto-close an environment.

Useful mappings and shortcuts#

Vimtex provides the e text object to facilitate vim-surround like features for manipulating an environment quickly:

  • cse: change an environment
  • dse: delete an environment
  • tse:toggle stared environment and no-star environment, e.g., change from equation* to equation and vice versa.

You can also use the standard Vim motion with this object. For example, vie will select the inside an environment, and vae will select the entire environment (including the begin and end commands).

There are other shortcuts provided:

  • ]]: close an environment (only work in insert mode, in normal mode, it will bring cursor to next subsection instead). After you have typed begin{ENV}, use ]] to auto-close it with \end{ENV}

For more mappings, see :h vimtex-default-mappings.

Jumping between references and the definition#

It would be nice if we can jump to the definition of a label with the native Vim shortcut Ctrl-]. You have to do several things.

First, you need to install a ctags executable. See here on how to install ctags.

Second, you need to install a plugin to manage tags file for you. vim-gutentags is a good option. It will update the tags file automatically once the source file has been changed. To make gutentags work, you need to tell it where your project root is. The easiest way is to make your project a git repository.

Open the TeX source file again, the tags file should be created automatically. If the cursor is in \ref{some_label}, you can press Ctrl + ] to go the position where some_label is defined. BTW, to jump back, you can use Ctrl-T.

Useful command#

  • :VimtexInfo: show all relevant info about current LaTeX project.
  • :VimtexTocOpen: show table of contents window
  • :VimtexTocToggle: toggle table of contents window
  • :VimtexCompile: Compile the current LaTeX source file and opens the viewer after successful compilation.
  • :VimtexStop: Stop compilation for the current project.
  • :VimtexClean: clean auxiliary files generated in compliation process.

Other settings#

PDF previewing#

Vimtex supports several PDF viewer based on the platform. On Windows, SumatraPDF is a good choice. Below is the settings needed for SumatraPDF to work as the default viewer for the compiled PDF files.

" settings for sumatraPDF
let g:vimtex_view_general_viewer = 'SumatraPDF'
let g:vimtex_view_general_options
    \ = '-reuse-instance -forward-search @tex @line @pdf'
let g:vimtex_view_general_options_latexmk = '-reuse-instance'

After the above settings, if you use :VimtexCompile command, the compiled PDF will be opened in sumatraPDF automatically. If it does not show up, you can also use :VimtexView to open the PDF manually.

On Mac, you can install Skim and use it as a PDF viewer. My current settings for previewing PDF on Mac is:

Click to see the code.
" let g:vimtex_view_method = "skim"
let g:vimtex_view_general_viewer = '/Applications/Skim.app/Contents/SharedSupport/displayline'
let g:vimtex_view_general_options = '-r @line @pdf @tex'

augroup vimtex_mac
    autocmd!
    autocmd User VimtexEventCompileSuccess call UpdateSkim()
augroup END

function! UpdateSkim() abort
    let l:out = b:vimtex.out()
    let l:src_file_path = expand('%:p')
    let l:cmd = [g:vimtex_view_general_viewer, '-r']

    if !empty(system('pgrep Skim'))
    call extend(l:cmd, ['-g'])
    endif

    call jobstart(l:cmd + [line('.'), l:out, l:src_file_path])
endfunction

Inverse search#

Note the below settings are not updated anymore, see this post for a better and up-to-date config without much manual labor.

First, we need to install neovim-remote:

pip install -U neovim-remote

Second, we start nvim with a listen address so that nvim can listen for RPC calls.

nvim --listen localhost:12345 test.tex

The port number does not matter as long as it is not taken by other applications. If you use nvim-qt, you can use the following command instead:

nvim-qt -- --listen localhost:12345 test.tex

Now the settings will differ for the PDF viewer you use on different platforms.

Inverse search for Sumatra PDF on Windows#

Open Sumatra PDF, go to Settings --> Options, in the bottom part, there is a section Set inverse search command-line, put the following command there:

nvr --servername localhost:12345 +"%l" "%f"
  • %f: it means the tex source file path corresponding to the current pdf.
  • %l: it means the line in the original tex file.

Double click somewhere in the PDF file, your cursor in nvim/nvim-qt should go to the corresponding line in the file buffer.

Inverse search for Skim on macOS#

Open Skim PDF viewer, open the Preferences menu and go to Sync page. Set the part PDF-Tex Sync support, use the following settings:

  • Preset: Custom
  • Command: nvr
  • Arguments: --servername localhost:12345 +"%line" "%file"

Based on discussion here, we can also start nvim without the --listen option (nvim test.tex). When we open a tex file in nvim, we can write the nvim server address to a file and read it when running the nvr command in Skim.

Inside the Nvim config, add the following setting:

augroup vimtex_mac
  autocmd!
  autocmd FileType tex call SetServerName()
augroup END

function! SetServerName()
  call system('echo ' . v:servername . ' > /tmp/curvimserver')
endfunction

For skim, we change the Arguments in Sync page to the following:

  • Arguments: --servername `cat /tmp/curvimserver` +"%line" "%file"

To start inverse search, press Shift and Command key, then click the text you want to inv-search.

Linting and syntax checking#

I use neomake for syntax checking. For LaTeX source file, neomake provides several makers:

Among the makers, lacheck and chktex are installed if you have installed Tex Live. Proselint can be installed via pip:

Setting up table of contents#

Vimtex can show a nice little table of contents window on the left side if you use command VimtexTocOpen. By default, the lables in each section is also shown, which makes the TOC clutter with texts. We can customize TOC with following settings:

" TOC settings
let g:vimtex_toc_config = {
      \ 'name' : 'TOC',
      \ 'layers' : ['content', 'todo', 'include'],
      \ 'resize' : 1,
      \ 'split_width' : 50,
      \ 'todo_sorted' : 0,
      \ 'show_help' : 1,
      \ 'show_numbers' : 1,
      \ 'mode' : 2,
      \}

For detailed meaning of the keys in the above dictionary, refer to :h g:vimtex_toc_config.

TeX directives#

If you are compiling a LaTeX source file written in non-English character, you may want to use the TeX directive to specify the compiling program. Vimtex supports two TeX directives: TeX root and TeX program. TeX program directive is useful when we want to compile the LaTeX source code with xelatex instead of pdflatex. In order to compile your LaTeX source file with xelatex, add the following directive to the first line of the source code:

References#