TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the three following (see below) files: test.tex is in the main directory, test1.tex is in the subdirectory ./test1/ and test11.tex is in the subsubdirectory ./test1/test11/ (see the directory structure below).

When I compile with auctex (C-c C-c) from the buffer where I read test11.tex, the file is compiled correctly: no problem. However, if I want to compile only the region (see screenshot below) where the figure environement is (C-c C-r), it fails. The _region_.tex produced by auctex in the directory where the master-file is is reproduced at the end of the question. This happens because no import-related command is issued in the _region_.tex file: thus the graphicspath is not adjusted. Is there a workaround?

Note: I can add the directory of the imported and subimported files in the Local Variables section it if can help.

emacs screenshot

├── _region_.aux
├── _region_.log
├── _region_.pdf
├── _region_.tex
├── test1
│   ├── test11
│   │   ├── KnuthAtOpenContentAlliance.jpg
│   │   ├── test11.log
│   │   └── test11.tex
│   └── test1.tex
├── test.aux
├── test.log
├── test.pdf
├── test-pics.pdf
└── test.tex

% This is ./test.tex



% This is ./test1/test1.tex

% Local Variables:
% TeX-master: "../test.tex"
% End:

% This is ./test1/test11/test11.tex
% http://commons.wikimedia.org/wiki/File:KnuthAtOpenContentAlliance.jpg



% Local Variables:
% TeX-master: "../../test.tex"
% End:

This is the file _region_.tex produced by auctex.

\message{ !name(../../test.tex)}% This is ./test.tex

\message{ !name(test1/test11/test11.tex) !offset(-4) }


\message{ !name(../../test.tex) !offset(-7) }

share|improve this question
Does the compilation fail, or you simply cannot view the image? If the former, can you send the contents of Emacs' output buffer? If the latter, did you check for resulting _region_.pdf in the master file directory? – Sašo Živanović Feb 24 '13 at 20:31
@SašoŽivanović See my edited question. – cjorssen Feb 24 '13 at 21:01
up vote 6 down vote accepted

Inspection of _region_.tex shows that it contains the information which file the region came from.

\message{ !name(test1/test11/test11.tex) !offset(-38) }

All we have to do is extract the path and fake it for \includegraphics. Thus, the plan:

  1. Temporarily redefine \message.
  2. Extract the path. (This is actually the hardest part ... I couldn't find any package doing it, so I used pgfkeys.)
  3. Set whatever current-path macros import sets.
  4. Make sure all this happens only if we're compiling a region. We don't want to end up with a redefined \message!

Point 4 is accomplished by setting AUCTeX's variable TeX Command -> TeX Region Extra to \let\message\regioncompilationfix.

And here's the code accomplishing the real work. (Only the master file needs to be changed.)

% This is ./test.tex

% 1. Temporarily redefine `\message`.
\expandafter\def\expandafter\regioncompilationfix@parsemessage\space!name(#1) !offset(#2) {%
  % 3. Set whatever the `import` package sets.
% 2. Define a macro which extracts the path from path+filename.
    % to be really sure this worked, we'd also need to delete
    % key "/getpath/path/.unknown" ... but since we're searching for
    % the "path", we just hope nothing's there ...


UPDATE: A purely AUCTeX way

AUXTeX provides hook TeX-region-hook which is run just after the region buffer is constructed. This is the Elisp code that inserts the TeX code that adjusts import's paths:

 (lambda ()
   (goto-char (point-min))
   (search-forward "\\begin{document}")
    (TeX-quote-filename (file-name-directory original))

Put this code in your .emacs file.

share|improve this answer
Nice trick +1. However, I was hoping that one could configure auctex to change the content of _region_.tex to include the adapted graphicspath. I'd rather not change my tex-master file. Do you know by chance what piece of lisp code create the _region_.tex file and how to customize it? – cjorssen Feb 25 '13 at 8:32
I was writing an explanation why it cannot be done when I found TeX-region-hook ;-) ---Btw, _region_.tex is created by function TeX-region-create in tex-buf.el. – Sašo Živanović Feb 25 '13 at 14:59
Exactly what I needed. Thanks a lot. – cjorssen Feb 25 '13 at 21:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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