Latexmk + GVim + SumatraPDF (WINE) On Linux

Posted on July 23, 2014

0


OK. I admit they are an awkward combination. It is just for me to get rid of ugly evince red box for forward search. I hope this post can help those who want some other awkward combination.

Basic

I am a daily user of GVim, some time for coding and some time for writing. Latex is, besides its odd syntax, a great tool for typesetting and I use Latex-box plugin on GVim. As all who use Latex may know, people can easily get lost in either the source code or in the compiled PDF. There it comes the forward/backward search, which can do bi-directional curse synchronization. I have been using Ubuntu 12.04 since its release and do not have the courage to upgrade to a new version (before I get a new laptop). That means I have to stand for the ugly red box every time I do a forward search in Evince, which came with Ubuntu 12.04 Gnome, apparently I am not a KDE fan (no offense). So I decided to use another PDF viewer for my writing. After looking around a bit, I find myself obsessed with SumatraPDF which looks nice and pityingly is disclaimed of a Linux version. Sign, couldn’t it be easier?

SumatraPDF

It is nice that SumatraPDF  works like a charm in Wine. The only thing I need to worry about is to set the backward search for it, i.e., the setting to direct the search for the line of source code that generates current page in PDF. Easy, just put GVim as the default editor.

Wait, what did you say, SumatraPDF? You cannot get the executable? Oh, no, it is because of Wine.

The good thing is, as pointed out in [1], that actually Wine do not seal the environment of the running windows program. Which you can still run Linux script from a windows program. Alright, let’s do some scripting.

# File: gvim_backwardsearch
#!/bin/bash
LINE=$1
FILEPATH=$2
NFILE=${FILEPATH:2}  # get rid of Z: in Wine's path
NFILE=${NFILE//\\//} # replace the \ for /
/home/wenli/devel/opt/bin/gvim --servername TEXGVIM --remote-silent +${LINE} ${NFILE}

Now we can safely put Z:\absolute\path\to\gvim_backward_search.sh %l %f in the backward search option in SumatraPDF. Noted that you need to open a PDF file come with synctex.gz file to enable the setting.

GVIM (Latex-box/Latexmk)

Now it is time for forward search from GVim. It took me a while to figure out how SyncTex work. I eventually find the file actually a gzipped text file. Inspection of the file suggests the replacement of the path in the lines started with ‘Input’. So there comes another script.

# File: conv_synctex_wine
#!/bin/bash
MAINFILE=$1
mv "${MAINFILE}.synctex.gz" "${MAINFILE}.synctex.orig.gz"
gzip -c -d "${MAINFILE}.synctex.orig.gz" | sed -e '/^Input/s_/_\\_g' -e '/^Input/s_:\\_:Z:\\_' | gzip > "${MAINFILE}.synctex.gz"

Then we need an extra step after Latexmk build every thing. As stated in the manual [2], a simply script can be put for $pdflatex in the RC file of Latexmk ‘.latexmkrc’ in the project root directory.

# File: .latexmk
if ( system('which conv_synctex_wine') == 0 ){
 $pdflatex='pdflatex -synctex=1 %O %S; conv_synctex_wine %B';
}

You may check Latexmk’s manual for all the percentage symbols. Anyway, there we go.

Reference

[1] http://stackoverflow.com/questions/6004070/execute-shell-commands-from-program-running-in-wine

[2] http://ctan.mirrorcatalogs.com/support/latexmk/latexmk.pdf

Posted in: LInux