Ah, the joys of creating a document via editing of a plain text file using your editor of choice. Via straight-forwardness of Markdown or producing beautifully type set formulas with , the source of it all are text files that can be version controlled and processed using whatever command line tool you have come across recently.
But let’s be honest, not being able to obtain instant visually feedback is a disadvantage. Here (and I am tempted to say only here), the what you see is what you get (WYSIWYG) editor like MS Word excels.
When writing a blog entry using a locally served
jekyll, this is
actually not a problem, since jekyll detects when files change and
regenerates the HTML pages accordingly.
Solution for LaTeX
Why isn’t there something like that for ? Well, one reason may be that output is typically not served via webservers. On the other side, there is no inherent reason, why you have to serve static HTML pages, when you could just load them in your browser.
So to answer the question and come to the point of the article:
inotifywait to the rescue! The underlying library lets you subscribe
for file system events. That is, it yields a file descriptor that can
be polled. If the underlying file is modified, accessed or created
(et cetera) the poll will succeed and you can handle the respective
So if you are working on a file hierarchy under your current working
directory and you would like to compile your document
rubber, then the following code does the job:
I highly recommend using
latexmkor a customized Makefile that takes care of all the steps to produce a PDF. So far
rubberhas not let me down.
-rsets up recursive watches.
-elets you subscribe the type of events.
Do not rely on inotify for tricky multi-threaded things. You may miss events when a watch has not been set up or the file has been renamed or deleted before you become aware of it.
Now you just need a PDF viewer like
okularwith auto-refresh and every save-document-action in your editor translates into a refreshed view of your PDF.
Solution for HTML
Well, that was refreshing to hear. LaTeX-to-PDF? Check. Can we do the
same for HTML? Sure, whenever a HTML page is generated, we could just
open a Tab in a browser. Actually refreshing an existing active Tab in
a browser is a bit harder and requires
xdotool. This utility
essentially allows you to script window manager interaction. Thus, we
xdotool to search for the chromium window, press
switch back. Here is the full script, which assumes
- that you dump the HTML output into /tmp/page.compiled/,
- have opened a single chromium window,
- have the tab of interest active in the window.
xdotool can be used for many interesting tasks. In this case, just
be aware that selection of the correct window may require some
calibration (see the definition of
Have you found other interesting use cases for the above mentioned tools? I would be delighted if you shared them by leaving a comment.