# Is there any 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.

-
You can use Qt, the windowing toolkit for Ktikz, natively with OSX. Compiling it is brutal (took me 25 hours, on an old ibook), but the integration is nice. None of the other dependencies of Ktikz look problematic, and I think they're all in Macports. You should then be able to compile the sources without trouble, following the instructions at lwn.net/Articles/274725 –  Charles Stewart Jul 30 '10 at 9:46
@Charles: this information is potentially life-changing for me! I spent hours, days, looking for anything like that, but couldn't find. I got a friend to try and compile it in Fedora but he couldn't make it work even there!! The problem is, I have never compiled anything, and I don't even know where to start. Those instructions (if they apply to OSX as you say, because that isn't clear to me) are too advanced for me. I don't know what dependencies are either. But again, I thank you for the tip. I will hassle someone to help me try the process :) –  Vivi Jul 30 '10 at 10:30
I'd agree with Charles that you should look into compiling Qt and the KDE libraries on your Mac. If you aren't able to coerce a friend into helping you ;-) Super User might be a good place to ask for more detail on how exactly to do it. (You'll be very familiar with running commands from the terminal by the end) –  David Z Jul 30 '10 at 19:10
The homebrew package manager for OS X can compile both Qt and the KDE core libraries. Check it out at mxcl.github.com/homebrew. Then brew install qt and brew install kdelibs. –  Sharpie Jul 31 '10 at 3:08
Well... it didn't work "out of the box" for me. With the source code, a few beers, and a couple hours on the Ballmer peak it could probably be straightened out. –  Sharpie Aug 3 '10 at 21:43

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.

-
By the way, the shell scripts listed are given for bash, which newer OSX machines defaults to. The older ones default to tcsh, so I am not completely sure if the second one will work. –  Willie Wong Jul 30 '10 at 14:26
I've done something pretty much the same with a python script. –  vanden Jul 30 '10 at 14:43
The latexmk program, which comes with most TeX distributions, when run with the -pvc will do everything the above does, and better. (Watch the file, recompile whenever it changes, run enough times to resolve all dependencies.) But it still doesn’t offer the true live-updating experience that you get with Gummi and WhizzyTeX. –  frabjous Aug 5 '10 at 2:00
You could also use the watch command instead of the sleep loop –  enthdegree Aug 24 '12 at 14:00
You could use inotifywait (see this unix.se answer) in monitor mode to trigger the compilation precisely after write access –  Tobias Kienzler Oct 22 '12 at 13:47

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

-
Thanks for the answer, but this is really not what I am looking for. I want something that will do it on the fly, without having to prompt it to do it. The way it is for me now, the effort to save and to compile my document is the same (C-x C-s or C-c C-c) and once I compile it my viewer updates the file. –  Vivi Jul 30 '10 at 20:50
You could set emacs to save every half second and use latexmk -pvc –  Niall Murphy Jul 30 '10 at 21:07

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.

-
That's great! Thanks for posting it here! I will try it now :) –  Vivi Sep 12 '11 at 4:11

Linux has a relatively new editor called Gummi that gives real-time output. Unfortunately it has crashed a few times for me (which is a few times too many). I plan to watch this editor closely as it becomes more stable.

I currently use Ktikz to edit my LaTeX documents in real-time. By default only tikz pictures are compiled, but by simply writing an empty template with the text

<>


ktikz will compile an entire LaTeX document.

-
Yeah, gummi provides the best live updating preview I’ve tried; better than WhizzyTeX anyway. Earlier versions crashed on me too, but so far the newest one hasn’t, but I’m still a bit nervous about it too. –  frabjous Aug 5 '10 at 1:57

The LaTeX editor Kile now offers a live preview feature in its development version. It's still experimental.

Requirements:

• PDFLaTeX with SyncTeX support (offered by TeX Live 2010 and 2011, for example)
• Development version of the Okular viewer
• Development version of Kile

More details can be found here: Live Preview with Kile.

-
Are you using Kile with Mac OS X? –  Tobi Nov 12 '11 at 15:58
I'm using it on Ubuntu Linux. –  Stefan Kottwitz Nov 12 '11 at 16:08
Ah, I hoped you could show me how to install it on my Mac. I failed with all step by step guides :-( … –  Tobi Nov 12 '11 at 16:40

Bakoma-TeX (http://www.bakoma-tex.com/) has a "true Wysiwyg" interface. Stable for Windows and "test" versions for Mac and Linux. You can find a favorable review at http://www.economics.utoronto.ca/osborne/latex/BAKOMA.HTM.

I sometimes use it, although I usually do not think it is worth the bother. TeXworks and frequent recompiling work well enough for me.

-
I didn't know about that. The fact that is being reviewed by an economist is a plus for me :P –  Vivi Sep 15 '11 at 4:02
Bakoma is the best choice, if you want to pay for it! –  user565739 Oct 5 '12 at 14:00

TexShop (also available through MacPorts) does a save on compile. It has a preview window so you can see your compiled document as you go.

-
TeXShop comes with the default MacTeX distribution, so you shouldn't need to install it via MacPorts, though. –  ShreevatsaR Jul 30 '10 at 18:03
As I said in a comment to another answer, this is really not what I am after. I want something that compiles without having to do anything, not even save the file. I want an on-the-fly compiler. Thanks for the answer, I am sure it will be useful for someone :) –  Vivi Jul 30 '10 at 20:52
@ShreevatsaR Package management is an amazing thing. –  Nerdling Aug 1 '10 at 15:37
@Nerdling: Yes of course (though package management on MacPorts is awful and bug-ridden in my experience, relative to the standards of, say, Debian), but why would you want to install something that's already present on your system? :-) –  ShreevatsaR Aug 4 '10 at 7:04
@ShreevatsaR Neither MacPorts nor TeXShop came on my system. What do you mean? –  Nerdling Aug 4 '10 at 12:07

Latexian offers a live preview on OS X.

-

MacKichan's SWP, SW, and SN are WYSIWYM (what you see is what you mean) editors, which do not give you the compiled pdf or dvi file without compiling, but compared to a source code the view is quite advanced. There are trial downloads available: http://www.mackichan.com/licensing.net/dnloadreq.aspx, but frankly I would wait purchasing it until the next version (which shall include eTeX and the ability to update everything to the latest versions at CTAN instead of being frozen at - , well, I guess it is 2002?).

-
The current version of SWP does not work on the Mac. Also, I was pretty clear that I wanted something where I could type LaTeX and see the compiled version at the same type (rather than type in something that looks like Word and you don't need to know LaTeX). That said, SWP is not so bad. –  Vivi Sep 13 '11 at 4:07
SW has a wonderful interface, but one should really not use it. It has a number of features which make it basically impossible to use many of the useful features of LaTeX. For instance: it expands all \newcommand and saves the file with the expanded version; it rewrites the part of the file before the \begin{document} moving all comments to before the \documentstyle; any text within an environment that it does not know of is presented in a dreadful little gray box and basically non editable. It generally encourages incompetence in the use of LaTeX. I have co-authors who use it, and hate it. –  Jacques Cremer Sep 14 '11 at 1:36
That is why I said "frankly I would wait purchasing it until the next version" - hoping the next version will have corrected everything. Well, there is a demo, thus when the next version is released, we can check whether it has been fixed. About "encourages incompetence in the use of LaTeX" you are totally right. –  Stephen Sep 14 '11 at 18:04

WhizzyTeX was mentioned in the question with the remark (emphasis added):

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

I'd like to counter that by pointing to a question: How can I execute a macro for every node in TikZ? where the aim was to figure out how to make TikZ nodes "draggable" in the WhizzyTeX-ADvi combination. I've now used that combination successfully to "fill in" a PDF form so can confirm that it works. Thus WhizzyTeX does work with TikZ pictures. It may need some development to get it to work in full - I've only used it in a rudimentary fashion - but certainly for nodes then it worked just fine.

Edit I just looked at the WhizzyTeX developer's homepage where it is clearly stated:

WhizzyTeX --- Version 1.3.2 of Aug 2, 2011 (works with pgf and beamer)

-

As a completely different option you might want to consider, it is also possible to use an editing program that exports TiKZ files. This may be perhaps the most responsive option, unless you have a super-zippy machine to run TeX. As mentioned by Andrew Stacey in an answer to an earlier question, the Inkscape extension inscape2tikz generates TiKZ output. If you don't care too much about the readability of the TiKZ code this would be worth a look.

-
Oh, no, I love writing tikz code! I don't use tikz so that I have a picture in code, I use tikz because I can write my picture using code!! For me, this is a bit like using Scientific Workplace or Lyx to write LaTeX. Easier, but not as flexible or interesting! –  Vivi Aug 3 '10 at 21:41
I understand your stance -- this is my attitude too! –  András Salamon Aug 4 '10 at 7:08

The MathTran Editor, which I developed, gives you instant preview of plain TeX math formulas. http://www.mathtran.org/editor/

-

I can recommend a program similar to Ktikz, CirKuit, which I've been using successfully for quite a while. It enables you to create not only TikZ pictures, but also circuit diagrams using the Circuit macros.

-
But this is for Linux, isn't it? –  Vivi Aug 22 '10 at 1:30
No, it's for KDE, just like KTikZ is. It should be possible to run it on Mac OS or Windows - any place you can use KTikZ, you should be able to use CirKuit as well. –  David Z Aug 22 '10 at 3:29

there exists only one real WYSIWIG editor http://mirror.ctan.org/systems/win32/microimp/microimp.pdf, which was the first and last try to create one for LaTeX. In general: it is not possible to have a WYSIWIG editor! A user can overwrite anything of the existing code or define anything new. This makes it nearly impossible to write such an editor.

What you can have is an editor which runs in the background ???latex when the source code changes. But this implies two windows, input and output, like Gummi does: http://dev.midnightcoding.org/projects/gummi

-

You may opt to use tikzedt.

Features:

1. Syntax highlighting and code completion, based on AvalonEdit.
2. Real time rendering, using pdflatex and mupdf.
3. It contains a Tikz parser, built using the Antlr parser generator.
4. Parsed coordinates are displayed as an overlay on the rendered image. The overlay can be edited with the mouse in a WYSIWYG manner. The source code is updated accordingly.
5. It comes with a snippet library to store frequently used commands and styles, containing many pre-defined examples.

According to the New features in version 0.2.2 of tikzedt:

One can use an external pdf renderer (mupdf) for better rendering quality. To do this, copy mudraw.exe into Tikzedt's program folder and change the rendering method in the settings. Customization of editor font. Version for Linux, and MacOS (to be tested) Bug-fixes

Another option may be to use TikZ-Editor

The features include:

1. simplistic and dedicated GUI : Tikz Editor is only about TikZ.
2. Syntax highlighting for TikZ/Latex sources.
3. Real-time preview of figures.
4. Customizable code snippets.
5. Feedback of LaTeX typesetting errors using source annotations and margin markers.
6. Separated edition of TikZ source and LaTeX preamble.
-
Picture for tikz-editor is from their github site. –  Harish Kumar Jun 4 '13 at 12:53
I can't get either option to work. Both say they can't find the command pdflatex :( –  Vivi Jun 4 '13 at 21:57
@Vivi :( Sorry. I am not on Mac. Somewhere in the settings you may have to indicate the path to pdflatex. But things should be taken automatically. Let me dig to some extent. –  Harish Kumar Jun 4 '13 at 21:59
@vivi For tikzedt, can you specify the path to pdflatex in Settings > Settings > General (tab)? What happens then? –  Harish Kumar Jun 4 '13 at 22:03
I am completely useless when it comes to this more complicated stuff (specifying path, commands to compile, etc). I will ask a question later to see if I get this to work. Also, although tkzedt seems like a better piece of software, it is not made for the mac, so I will probably stick with the other option (tikz-editor) for now. Thanks for the help, I think you have done more than I could ask for! –  Vivi Jun 4 '13 at 22:11

TeXlipse automatically compiles the document on save and I usually have the PDF open (in Preview on Mac OS X or in evince on Ubuntu 10.04, which both automatically show the updated PDF if it is changed).

-