[Tex/LaTex] ny way to get real-time compilation for LaTeX


I would really love to have a software or tool which provided real time compilation of my LaTeX document (to be able to see the final pdf or dvi document as I type the latex code), especially for when I am drawing pictures using tikZ. So far, I know of two things, but none of them work for me. They are:

  • KtikZ: This is only for tikZ, which would be perfect for me, but unfortunately as far as I know it only works with Ubuntu and Debian, and I use Mac OS X (so I have never actually tried it, though I did dig a lot to see whether I would be able to install on my mac and from what I see it is really what I want – I am even considering installing Ubuntu just to use it!).

  • WhizzyTeX: This is a minor mode for Emacs. It was extremely hard to get it to work on the mac (see my quest here), and it is not all that fast. The speed is not all that big of a deal for me, but the fact that it does not work with tikZ pictures is (the nodes all collapse in one point so that the words are all one on the top of the other).

So my question is: is there a software ideally like KtikZ or another type of editor which provides real-time compilation and that I can use on my mac? I guess if you know of a Windows/Linux option I would also like to know, and the same for a way to set up a script to get the document compiled every 5 seconds or so (I am sure I have seen this written somewhere, but I don't know whether I can do it with a mac or whether it is editor/pdf viewer dependent), but what I really want is a software/tool which would work with a mac.

To be clear, I am not after something like LyX, that is, I am not after a WYSIWYG-type thing, but rather something where I can type real LaTeX and see my code, but have at the same time another window showing me the pdf (or dvi) file compiled.

Best Answer

On a Mac, you can of course just open up a terminal, cd to the directory where you keep the TeX file, and issue

while true; do sleep 5; latex -halt-on-error filename.tex; done

and have the DVI file open in a viewer that watches for (and reloads on) changes. The one-liner runs latex continuously with 5 second break between runs (the -halt-on-error options prevents the incantation from getting stuck if you saved a file with errors). You can also swap in pdflatex instead.

This solves half of your problem. The other half has to be dealt with by your editor of choice. You need to set it up to automatically save the file every x seconds, and how to do that depends on the editor at hand.

Now, that one-liner I gave above is quite ugly and resource wasting, since it makes no sense to re-compile if no changes are made (Edit: See this comment below for a much better way to avoid this problem). So you can do something like

while true; do sleep 2; if [ filename.tex -nt filename.log ]; then latex -halt-on-error filename.tex; fi; done

which watches for changes to the TeX file (signaled by the fact that filename.tex is more recently modified than filename.log) and compile when necessary (with a possible two seconds delay).

Short of a WYSIWYG, I am not quite sure how you can achieve full real-time solution. Compiling the code takes usually a short amount of time (1 or 2 seconds, or more if the file is large). So if you are looking for a solution that calls the LaTeX compiler, it probably shouldn't try to do it more often than once every 5 or 10 seconds. So you won't be able to immediately see what you typed in the DVI window. Also, if the editor autosaves the file in a spot where you are halfway typing a command, then the source won't compile.

With LaTeX I feel that the better idea is "compile-on-saves", where the human initiates the saving of the file (as compared to "automatically saving and compiling in the background). For that, modern editors can generally support hot-keys where saving and compiling is mapped to one keystroke. In vim I map F2 to compile and F4 to call XDVI.

Related Question