# Reftex cross-reference files in different directories

My latex files are stored in a directory structure as this

- main.tex
- .dir-locals.el
-- sections/
--- sec1.tex, sec2.tex, ...
-- tables/
--- table1.tex, table2.tex, ...


In main.tex, sec1.tex is "imported" as \input{sections/sec1}; In sec1.tex, table1.tex is "imported" as \input{tables/table1.tex}. I have already specify TeX-master as "main.tex" in .dir-locals.el like this:

((latex-mode
(TeX-master . "main.tex")))


All .tex files under the root directory knows the correct master file. But my problem is that reftex cannot detect labels in table1.tex when I am editing sec1.tex (It works correctly when I edit main.tex). The reason seems to be that when I am in sec1.tex the code \input{tables/table1.tex} would let reftex to think there is a "tables" directory inside the "sections" directory.

My particular problem is different from How to use reftex in multiple files and RefTex Multiple files problems , because the nested modular input structure is relative only to the master file, but not so from the perspective of a slave file.

## Can anyone help me with this please? How can I let reftex to reference labels from table1.tex when I edit sec1.tex?

Update: here is an example.

.dir-locals.el:

((latex-mode
(TeX-master ."main.tex")))


main.tex:

\documentclass{article}
\begin{document}
This is main.tex. \\
\input{sections/sec1}
This is main.tex referencing Section~\ref{sec:foo}.
This is main.tex referencing Table~\ref{tab:foobar}.\\
main.tex ends here.
\end{document}


sections/sec1.tex:

\section{foo} \label{sec:foo}
This is from sec1.tex. \\
\input{tables/table1}
This is sec1.tex referencing Table~\ref{tab:foobar}. \\
sec1.tex ends here.


tables/table1.tex:

\begin{table}[h]
\begin{tabular}{ll}
A & B \\
1 & 2
\end{tabular}
\caption{foobar}
\label{tab:foobar}
\end{table}


In reftex, the command reftex-reference (press C-c )) would provide labels when I edit main.tex, as expected. But the problem is that it would not detect label tab:foobar when I edit sec1.tex.

• Welcome to TeX.SX! What I have to offer is not a real solution, but a trick: What about adding a link from sections/tables to ../tables? Under Linux/Unix/MacOS, you would issue the command ln -s ../tables in the sections directory; under Windows it has to be done differently, but should also be possible. – gernot Sep 18 '16 at 14:39
• @gernot what you suggest would be difficult for a git repository though. – Yi Liu Sep 18 '16 at 17:40
• The problem would go away if you simply didn't use all the sub-directories. Unlike in other contexts, in the world of complex *TeX documents, 'tidying' things up by creating a sub-directory for every little thing only serves to complicate things. No one is going to confuse sec1.tex with table2.tex, after all.... If anything, use -output-directory to place the output in a different location. – jon Sep 18 '16 at 20:51
• Adding links, as @jon suggests, would not worry a git repository at all: you simply don't track these files! (Git only tracks the files that you explicitly tell it to.) I haven't used reftex, and you have not provided a minimal working example, (so it isn't straightforward for me to test this) but I suspect that setting the environment variable TEXINPUTS to search the subdirectories may solve your problem. – Andrew Sep 19 '16 at 4:25
• Andrew offers a hint for a solution, but note that you would not be able to use \input the way you are now using it. (This is a good thing, in fact, because you are sort of mis-using it as it stands.) – jon Sep 19 '16 at 4:42

Found a solution. The cause of the problem is TeX-master is not specified correctly.
TeX-master should be set as the plain absolute path like "path/to/main.tex", or in the case of a git repository, should be programmatically set to a correct absolute path.
When edit any .tex file reftex can now retrieve labels specified elsewhere as long as they are "imported" to the master file, along with a correct toc tree.