[Lowerbounds, Upperbounds]

Algorithms are everywhere.

As I have hinted in this post about pdfopen, it is possible to do some sort of round-trip LaTeX editing with the right tools. There are two directions to make the round-trip complete.

The forward jump is editor-to-previewer: given the current cursor location in the editor, jump to the corresponding paragraph in the previewer. The backward jump is symmetrical. The magic behind this is called “source specials”.

While it sounds somewhat like a Chinese dish with special sauces, it really isn’t. :P Instead, when you generate the DVI file, you ask latex to tag the DVI file with “source specials”. Imagine each word in the DVI file is tagged with the source filename and the line number of the word.

The forward jump is to ask the previewer to display the location that is right after the editor’s cursor location (filename and line number). The backward jump is to ask the editor to put the cursor closest to the current location in the previewer (usually the location of a double-click).

Let’s get to the example. I will show how to get round-tripping to work in Windows using MiKTeX distribution 2.4 and GNU Emacs. Other configurations are similar.

(Round-tripping between LaTeX and PDF is similar in principle, but the tools are not very mature yet. I will save this for a later post.)

  • Install the AUC TeX package into Emacs.
  • To enable tagging in latex, invoke it with the --src-specials option. Say the primary LaTeX source is main.tex, AUC TeX invokes it this way, :
    latex --src-specials \nonstopmode\input{main.tex}

  • To do the forward jump with YAP (the DVI previewer in MiKTeX), say when the cursor is on line 65 of hetero.tex, AUC TeX will execute:
    yap -1 -s65hetero.tex main.dvi

  • To enable the backward search, install the gnuserv package. Google can help you locate many links, like this and the rest.
  • Assuming you have gnuclientw installed on your path, in YAP, go to View->Options->Inverse Search. Inside Program combo, you should see “GNU Emacs (Single Instance)”. Example command line:
    gnuclientw.exe -F +%l "%f"

  • Note that installing gnuclientw addresses the problem in this comment. In general, you rarely want to invoke emacs directly. Use gnuclientw instead.

Have fun!

9 Comments

  1. Bernhard
    0:56 on June 8th, 2006

    Thanks,

    could you give me the command line to open yap from emacs? I mean the general one that is in your .emacs-file.
    In other words, I might not always want to go to line 65, so what is the emacs-variable for the current line number and current tex-file?

  2. Bernhard
    12:48 on June 8th, 2006

    Another thing, I just played around with gnuserve and on my machine it seems that gnuclient is much more stable than gnuclientw. Very often, “gnuclientw filename.txt” would switch to the emacs window but not open the file “filename.txt”. This seems to be better with gnuclient. Let me know if you have similar experiences with that.

  3. If you use AUC TeX, then this is actually the formatted output of

    “yap -1 -s%n%b %d”

    Of course, these three variables only make sense in that package. If you really don’t want to use AUC TeX (but I really recommend using it), then you have to extract the line number %n (see function “what-line” on how to get it robustly) and the current file name %b (see “buffer-file-name”). As for the actual DVI filename %d, there is really no generic way since it is built from the value of the TeX-master variable in AUC TeX.

  4. So far (and I mean the last 9 years) gnuclientw has been working very solidly for me.

    Notice you really want gnuclientw instead of the “non-w” version. The “w version” will exit once the file has been passed to Emacs. The “non-w version” will wait until Emacs is closed. The latter is not what you want. (It’s similar to java vs javaw.)

  5. Bernhard
    15:04 on June 8th, 2006

    Thanks, this works for me now. I do have AucTex installed but I didn’t have a command line for yap in the package. So I wrote it into my .emacs-file under custom-set-variables, which seems to be OK.

  6. Anonymous
    16:39 on June 8th, 2006

    It’s very useful, but can you please say briefly how to activate/configure auctex? How do you tell it to use yap or acrobat? I tried C-c C-c and it “suggests” the command line “xdvi main.tex” (no yap and no line numbers).

  7. Basically you need to set your TeX-command-list correctly.

    Usually, in your .emacs, you do (require 'tex-site) and in tex-site.el you customize the AUC TeX global variables, such as TeX-command-list. In modern versions of AUC TeX (I use 11.11, not exactly newest), there is a tex-mik.el that provides a set of sensible defaults. You can just put (require 'tex-mik) at the end of your tex-site.el.

    Hope this helps.

  8. I believe it’s due to the TeX-command-list customization. See if my comment at 17:35 (below) helps.

  9. Anonymous
    20:36 on June 8th, 2006

    Thanks.

    I have installed the auctex package you suggested (the one for windows). It contains tex-mik.el and adding (require ‘tex-mik) to my _emacs improved the situation so that the suggested command was now “yap -1 main.tex”, but no lines numbers…

    Eventually, I figured out that I need to turn on src-specials by adding to my _emacs
    (setq TeX-source-specials-mode t)
    This can be done also via the customize auctex pulldown menu (which is the way I figured it out).

    Anyway, it now works quite well. THANKS!