# if the remote file exists then include it, else include the local one

Here's the path where my tex file stands :

/home/me/documents/foo.tex

I also have one import file here :

/home/me/texmf/tex/latex/personal/bar.tex

However for security purpose I also include it here locally :

/home/me/documents/bar.tex

When I input it in my tex file, e.g. :

\input{bar}

I noticed the local file is used over the remote one (latex personal directory)

My question is : how can I manage the following statement ?

• If the distant file exists then include it over the local one.
• If the distant file is none then include the local one.
-

The file search strategy of TeX can be configured, see texmf.cnf for TeX Live. Example for pdflatex:

TEXINPUTS.pdflatex      = .;\$TEXMF/tex/{latex,generic,}//

And TEXMF is set by default as:

TEXMF = {\$TEXMFCONFIG,\$TEXMFVAR,\$TEXMFHOME,!!\$TEXMFSYSCONFIG,!!\$TEXMFSYSVAR,!!\$TEXMFLOCAL,!!\$TEXMFDIST}

Or shorter, first the current directory is used, then the home texmf tree, the local texmf tree and at last the distribution tree.

Of course the search order can be changed by changing or overwriting the entries, e.g.

TEXINPUTS=/my/documents:

Then first the directory /my/documents/ is searched, then the normal search of the configured TEXINPUT applies because of the trailing colon.

However, the search is directory based, The search order cannot be changed for single files. In your case you have both the main TeX file foo.tex and the other file bar.tex in the same directory /home/me/documents/. The first file should be found there, but the latter should be first found somewhere else:

/home/me/texmf/tex/latex/personal/bar.tex

Thus changing the search order will not help, because foo.tex should not be found first somewhere else, too.

However, in this case, there is a difference in the parent directory of the file that can be used here to differentiate between the two versions:

\InputIfFileExists{personal/bar.tex}{%
% File personal/bar.tex exists and is read
}{%