I've been using emacs with AUCTeX mode to edit my latex documents for a few days now, but there's something that's really bugging me. As it should be, whenever I do C-c C-c RET it compiles the file, and if repeat the command it views the output in xdvi. It's also set to the mini-mode TeX-source-specials-mode, so instead of opening a new window in xdvi it only reloads the window that's already open, brings it to the front, and sends me to wherever the pointer was in emacs (forward search).

Now here's the problem:
Even though the xdvi window is brought to the front, it's not focused. Instead, the emacs windows stays with focus (and that's where any keyboard input goes). And I keep forgetting about that, which leads me to accidentally editing the source file while trying to navigate in xdvi. Not to mention I'm forced to alt-tab in order to focus xdvi, and alt-tab twice if I just want to get back to emacs.

Is there a way around this problem? I just want xdvi to be focused whenever I run the view command from emacs.

EDIT:I found out I can focus the xdvi window with the command wmctrl -a main (main is the title of the xdvi window). How can I set emacs to automatically apend this command to the end of the view command?

  • Updated the question – Malabarba May 19 '10 at 17:28

Here is an alternate solution to your problem (which is, if I'm correct: to have an immediate view of your newly compiled document): just open your dvi file in a new buffer an set it to auto-revert-mode. Now each time you'll compile your document this buffer should show you the changes without the need of leaving emacs and it's tiled purity. Note, you might want to try this with pdf as your compilation output; at least to me, it seems that auto-revert is much more responsive with pdfs.


That didn't quite work for me since my emacs window has the tex file as it's name. This means that wmctrl gives emacs focus instead of xdvi.

However, a slight change fixed it for me. I appended the following line instead:

%d && wmctrl -a xdvik #

After a whole lot of digging, I think I found a way.It feels kinda clunky, so feel free to suggest a better way.

The default view command is composed by "xdvi", followed by a few arguments defined by some variables, followed by the file name. The last argument before the file name is defined by the variable "Tex Source Specials View Editor Flags", and its default value is: "-editor "%cS"" (without the outermost quotes).

I added the string "%d && wmctrl -a $(echo %d | sed -e 's/.dvi//g') #" (again, no quotes) to the end of this variable. Remember that this is the last argument in the view command before the filename.

  • %d is meant to replace the name of the dvi file, necessary for the xdvi command.
  • && ends the xdvi (view) command.
  • wmctrl -a will give focus to the window titled: echo %d | sed -e 's/.dvi//g' (which is the name of the dvi file without the .dvi extension).
  • at last, just tells

    bash to ignore whatever comes next. In our case, that's the filename that emacs automatically appends after all of this.

To edit this variable I used emacs customization menu for latex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.