神刀安全网

Using Vim to write and live-preview tex files

Using Vim to write and live-preview tex files

The woman above is what you will see when searching "latex" in google image, I add a VIM badge so as to represent "Vim + Latex"… Google, do you really understand what Latex is?

Initially, I am a heavy fan of Sublime text and I even wrote several plug-ins for it; however, when I started maintaining this personal website based on mezzanine, I needed to do a lot of programming on the server side. SFTP plug-in of Sublime text is an option, but it is not so straightforward about user privilege settings. Therefore, only vim and emacs were left, I picked vim.

It is not a problem to write Python, Rust or other program languages using vim, since YouCompleteMe and UltraSnips deal with everything smoothly. But I am a scholar of economics, so I need to write serious latex files.

Well, it is said that some guys could write raw latex for around xxx pages without any syntax check and preview, but successfully compile the code at once. In the real world, I have not seen any one could do that, neither I did. So a decent live-preview is useful and, somewhat necessary for me to migrate from sublime text to vim. There are several good tex plugins, such as vim-tex and latex-suite, I used vim-tex and I am quite happy with it. But how about live-preview?

I found a vim plug-in vim-latex-preview , written in pure vim script. It is fairly easy to install, everything is crystal clear in the webpage, all we need to do (with pathogen) is to git clone the plug-in in the folder that pathogen or vbundle monitored, then add the following two lines in .vimrc:

autocmd Filetype tex setl updatetime=1 let g:livepreview_previewer = 'evince'  

of course, since we are in OS X, we need to

brew install evince 

to install evince.

The author said that evince and okular are the best two pdf viewers with this plug-in, so let’s go!

……

after I run :LLPStartPreview in vim, the pdf viewer did appear, but it did not respond to any change that I made in vim. in one word, it previewed, but not lively.

What’s wrong?

after some investigation, I think that it was because in OS X, we did not have inotify library, so the system could not inform evince to update the view when file changed.

So we have to accept manually reload? No!

I started trying various of pdf viewers, such as MuPDF and Zathura, but none of them worked. the default pdf viewer of OS X — Preview did work, but it only worked when it had focus, so it updated if and only if we use cmd + Tab key to switch back and forth between the window of Preview and VIm.

This is obviously not acceptable, then I tried Skim, change this line in .vimrc

let g:livepreview_previewer = 'open -a Skim' 

Skim worked, but as said in vim-latex-live-preview’s project page, it only worked when the update time was sufficiently large, so setting it to 1 led an error, setting it to 100 would be okay, but still it complained when you moved the cursor quickly in vim’s command mode.

I stuck here. I clicked on Mactex’s folder, then TexShop suddenly jumped into my sight. Why not TexShop? It was another PDF Viewer! so I changed the .vimrc again:

autocmd Filetype tex setl updatetime=1 let g:livepreview_previewer = 'open -a texshop'  

Success! everything worked like a charm!

Wait, where was the focus, oh, gosh! every time I changed a little bit in vim and texshop auto-reloaded, and, it also gained the focus.

Continuing stack overflowing, I found the solution, just pasted this line into shell and enter:

defaults write TeXShop BringPdfFrontOnAutomaticUpdate NO 

Now texshop no more required the focus, a perfect vim-latex environment was established.

Using Vim to write and live-preview tex files

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Using Vim to write and live-preview tex files

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址