Is there any way to get real-time compilation for LaTeX?

Latexmk has an option that will compile your document every time you save it. It requires Perl. There is also some very useful information in this Stackoverflow question about how to work with latexmk when you document has errors (which can happen a lot with TikZ).


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 -hald-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.


Claus Gerhardt's Flashmode offers live preview for TeXShop on MacOS. From that page:

Flashmode enables simultaneous typesetting if TeXShop is used as editor for TEX or LATEX or any other common TEX variant. When Flashmode is started, it looks for the front most document in TeXShop, gets its path and after that it is hooked forever to this document as long as it stays open, i.e., a few split seconds after Flashmode has started the front most document in the TeXShop could be anything without doing any harm.

Then Flashmode checks in regular intervals (default is 0.2 sec), if its tex document has been modified, and if so, it initiates a save, a pdflatex run, and asks TeXShop to refresh the corresponding pdf file. Flashmode can be invoked even if a document isn’t typeset yet, but beware that its first action will be a typesetting command.

An important feature is that tex syntax errors don’t cause any error messages or disruptions of any kind, i.e., the pdf gets never corrupted, at least the pdf viewer is never aware of it, and the last refreshment is always visible; the pdf viewer will never complain.

Thus, the user never gets any error messages; the presence of errors can only be deduced from the pdf window which then doesn’t change any more; of course the errors will be reported in the log file and can be looked up.