Compile a region in auctex with import package

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.

├── _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
\documentclass{article}
\usepackage{graphics}
\usepackage{import}
\begin{document}

\import{./test1/}{test1.tex}

\end{document}


% This is ./test1/test1.tex
\subimport{test11/}{test11.tex}

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


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

\begin{figure}
\includegraphics{KnuthAtOpenContentAlliance}
\end{figure}

Test...

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


This is the file _region_.tex produced by auctex.

\message{ !name(../../test.tex)}% This is ./test.tex
\documentclass{article}
\usepackage{graphics}
\usepackage{import}
\begin{document}

\message{ !name(test1/test11/test11.tex) !offset(-4) }
\begin{figure}
\includegraphics{KnuthAtOpenContentAlliance}
\end{figure}

Test...

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

\end{document}

-
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

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
\documentclass{article}
\usepackage{graphics}
\usepackage{import}

\makeatletter
% 1. Temporarily redefine \message.
\let\regioncompilationfix@orig@message\message
\def\regioncompilationfix#1{%
\let\message\regioncompilationfix@orig@message
\message{#1}%
\regioncompilationfix@parsemessage#1%
}
\expandafter\def\expandafter\regioncompilationfix@parsemessage\space!name(#1) !offset(#2) {%
\getpath{#1}%
% 3. Set whatever the import package sets.
\edef\import@path{\getpathresult/}%
\edef\Ginput@path{{\getpathresult/}{./}}%
\edef\input@path{{\getpathresult/}{./}}%
}
% 2. Define a macro which extracts the path from path+filename.
\usepackage{pgfkeys}
\def\getpath#1{%
{%
%\pgfkeyslet{/getpath/#1}\relax
%\pgfkeyslet{/getpath/#1/.@cmd}\relax
% 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 ...
\pgfkeys{%
/handlers/.unknown/.code={%
\xdef\getpathresult{\pgfkeyscurrentpath}%(
\expandafter\getpath@stripprefix\getpathresult)%
},
/getpath/#1,
}%
}%
}
\def\getpath@stripprefix/getpath/#1){\gdef\getpathresult{#1}}
\makeatother

\begin{document}
\import{./test1/}{test1.tex}
\end{document}


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:

(add-hook
'TeX-region-hook
(lambda ()
(goto-char (point-min))
(search-forward "\\begin{document}")
(insert
"\\makeatletter"
"\\def\\import@path{"
(TeX-quote-filename (file-name-directory original))
"}"
"\\edef\\Ginput@path{{\\import@path}{./}}"
"\\edef\\input@path{{\\import@path}{./}}"
"\\makeatother"
)
)
)


Put this code in your .emacs file.

-
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