SyncTeX support in GNOME
José Aliste explains how GNOME now makes it easier to correct errors in TeX files. D-Bus is involved!
The idea of this article is to show how you can achieve cool integration between different applications in the GNOME Desktop in a simple way by using D-Bus. I will concentrate on the SyncTeX support included since GNOME 2.32.
First, I need to give some context about SyncTeX, since many of you have probably never heard of this technology before. In the academic world, there is a tool called LaTeXwhich allows us to beautifully typeset professional documents. It is particularly simpler to add mathematical formulas in LaTeX than in other apps. Unfortunately, LaTeX is a little tedious, because you need a source file full of commands (similar in spirit to a HTML file) and then you use LaTeX to transform this source file into a document (usually a PDF file). So you can imagine that, when processing large documents, detecting and correcting errors can be quite cumbersome.
That’s where SyncTeX enters into play. Modern TeX engines allow you to, in addition to the PDF file, produce a synctex file, which contains information that helps you synchronize between the source file and the output document. Viewers and editors can use this information to perform forward and backward searches between the source and the document.
“Forward” search means that you are editing one particular chunk of text in your editor and you want to look at how the output corresponding to this chunk will look in the document. To do this, you Ctrl-click over the line of text in Gedit to perform a “Forward” search.
Alternatively, if you are looking at the PDF document with Evince, you Ctrl-click to perform a “backward search” that will allow you to look in Gedit and see the line that generated the part of the document you are interested in.
So how does this integration work?
The basic principle is to use D-Bus, which is a technology which allows inter process communication (IPC). D-Bus is in heavy usage along the complete GNOME Desktop. For instance, DBUS is heavily used in Telepathy (the messaging framework of the GNOME Desktop). The basic idea behind it is that there is a daemon process, the dbus-daemon, and each application connects to the dbus-daemon and sends messages to it. The dbus-daemon is then responsible for forwarding messages to the corresponding applications. The good news is that GLib has now native support for DBus, called GDBus. so it is ready to use in GNOME apps.
Let us dig into how Evince uses D-Bus. First, Evince spawns one process for each Document. This is to ensure that, even if one Document makes Evince crash, all the other Documents are safe. This is because there is a small app called ev-daemon which registers itself in D-Bus with the easy-to-remember name ‘org.gnome.evince.Daemon’.
So you can send messages to the ev-daemon through D-Bus to know whether a given PDF document is currently opened by Evince, and you can even ask the ev-daemon to spawn an instance for you. Next, you can remote-control Evince windows by calling D-Bus methods on the D-Bus object of the given window, and you can react to events in the Window by listening to signals on the same object.
So this is basically how SyncTeX support in GNODME works. Each Evince window declares a D-Bus method “SyncView” and a D-Bus signal “SyncSource”. Thus, when you Ctrl+click in Gedit with the gedit-synctex plugin enabled, the plugin calls the SyncView method through D-Bus, and you get the results of the forward search highlighted in Evince. Conversely, Evince emits a SyncSource signal whenever you Ctrl+click in the document that support synctex. If you are editing the corresponding file in Gedit, then Gedit was already listening for the SyncSource signal, and so it reacts by highlighting the corresponding source chunk in the file.
At the moment, only SyncTeX D-Bus control is implemented in Evince. However, we plan to introduce new ways of controlling Evince via D-Bus, most notably to control an Evince window through Telepathy D-Bus tubes, which will basically transform the GNOME Desktop into a free alternative to many Web-based seminar systems out there.
José Aliste is a GNOME developer.