Faster and more comfortable LaTeX editing and previewing with latexmk

When writing LaTeX documents I usually just edit the plain text in vim, and in another xterm I type make && xpdf foo.pdf to regenerate the PDF output and view it in xpdf. This is tedious and slow if you do it often. A much better way I found out about now, is to use latexmk.

apt-get install latexmk

This tool autogenerates a PDF or PostScript file out of your *.tex file(s), so it would make my Makefile obsolete. But the much better thing is that it has a preview mode:

latexmk -pvc -pdfps foo.tex

This generates a PDF out of foo.tex (and its dependencies, if any), and refreshes the PDF every time the foo.tex file gets updated (i.e., every time I type :w in vim). So I can now leave the xpdf instance open all the time, and it'll be refreshed automatically to show the latest version of my document.

Caveat: in xpdf you have to press (for example) "next page" and then "previous page" (SPACE, BACKSPACE) to refresh the screen. Leaving out the "-pdfps" makes the regeneration process a bit faster and uses xdvi for previewing as dvi (instead of PDF) which does not require the above SPACE+BACKSPACE hack. But I like xpdf better than xdvi, so I'll stick with it.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thank you for the latexmk howto

This was exactly the solution I was looking for! I'm using LaTeX for some things I should really be using a graphic-design program for, so I'm always manually fine-tuning the locations of images and text blocks. latexmk makes life so much better! Thanks for the howto!

Have you tried texmacs? Now

Have you tried texmacs? Now I just use texmacs all the time instead of vim+latex+xdvi...

You can do it all from vim

You can do it all from vim (e.g. by using :autocmd bufwritepost * make).

Ulrik's "onchange" utility does exist, as well, and is called inotifywait.

inotifywait

works fine, thanks a lot!

I'm not the best hacker around, but I made these:
-onchange-

#!/bin/sh
FILE="$1"
COMMAND="$2"
while inotifywait -e close_write "$FILE"; do
sh -c "$COMMAND"
done

-texwatch-

#!/bin/sh
FILE="$1"
~/bin/onchange "$FILE.tex" "pdflatex -interaction nonstopmode $FILE.tex; evince $FILE.pdf"

vim-latexsuite

I use vim-latexsuite which binds \ll to compile the document using latex, \lv to open the dvi file in a new xdvi instance and \ls to do forward search from vun to (a new or existing) xdvi instance

TexLet g:Tex_CompileRule_dvi = 'latex --src-specials -interaction=nonstopmode $*'

to enable reverse search, I've setup

TexLet g:Tex_ViewRule_dvi = 'xdvi -editor "vim --servername '.v:servername.' --remote +\%l \%f"'

vimlatexsuite

Ah, yes, vimlatexsuite has been on my TODO list for a long time. From what I heard it has some other very nifty features which might be worth checking out...

rubber, gv and dnotify

My take on this was to use gv to view the ps/pdf file, it has an autoupdate mode where it notices when a file changes and reloads it. I used rubber to compile the latex code, a simple rubber --ps file did the trick. And I let the compilation happen whenever I saved the file by using dnotify to notice when a file is saved and run rubber on it.

The end result was that I edited the file, saved it occasionally and switched a few seconds after saving to see the latest version, assuming I didn't do any latex mistakes that broke compilation.

rubber, dnotify

Hm, that looks like another viable solution, thanks! Will test soon.

general solution

So what about the general solution, watching a file and responding to it?

Since "evince file.pdf" just reloads the pdf if you already had it open, I'd like a solution so that pdflatex was run and then evince was called each time when something changes.

What if we could have the command 'onchange'
usage:
onchange file command [options]

onchange file.tex 'pdflatex file.tex ; evince file.pdf'

Just use $pdf_previewer

By creating a .latexmkrc file in your home folder and including the line

$pdf_previewer = "start evince";
$pdf_update_method = 0;

Latexmk will then use evince as the pdf viewer and autoupdate it. I wrote a bit about it on http://www.tjansson.dk/?p=278.

onchange

how about: http://marginalhacks.com/bin/every_change

onchange

I haven't tested this, but I think it will open a new Evince instance every time. The latexmk solution refresh an already open instance...

Refresh xpdf

Pressing the 'R' key reloads the file in xpdf.

Refresh in xpdf

Nice, thanks. That's a bit faster...

Refreshing xpdf without

Refreshing xpdf without click

I found the following solution for continuos preview of a PDF-Document that works for me:

Start xpdf:

1) $ xpdf -remote 127.0.0.1 foo.pdf

Then, after each latex run, you tell xpdf to refresh:

2) $ xpdf -remote 127.0.0.1 -reload

So, starting xpdf, let's say, from a make-target ("make show" or something), is always done in remote mode using command 1). The make-target for latex runs ("make pdf"?) checks, if an xpdf is already running in remote mode, say by

ps -ef | grep "xpdf -remote 127.0.0.1" | egrep -v grep | awk '{print $2}'

If this yields a PID, line 2 can be executed. (Otherwise, line 2) would open a new xpdf-instance instead of refreshing an already running one.) See the manpage of xpdf for details.

The benefit of this is, that there is _no_more_need_ to do some action in the xpdf window like scrolling back one page and scrolling forward again or something similar. xpdf just mirrors what you typed in, say, vim.

Auto-refreshing xpdf from newer versions of latexmk

The man page of recent versions of latexmk describes how to modify your .latexmkrc to do this entirely with (the newest version of) latexmk:

$pdf_previewer = "start xpdf -remote %R %O %S";
$pdf_update_method = 4;
$pdf_update_command = "xpdf -remote %R -reload";

The Debian version of latexmk is unfortunately quite outdated, so you might have to install the new version directly from CTAN.