2

I have a central BibTeX file that I have in my home folder. Sometimes, I use it as a git submodule in the subfolder central-bibtex. The relative location can vary a bit. Since I want my preamble to do the work for me, I need it to find it in a couple of possible locations. Those include:

  • ../../central-bibtex/Central
  • central-bibtex/Central
  • ../../zentrale_BibTeX/Central

Once I have a better syntax for this, I would like to add even more:

  • ../central-bibtex/Central
  • ../zentrale_BibTeX/Central
  • ../../../zentrale_BibTeX/Central

So far, I have this, which seems to work:

\IfFileExists{../../central-bibtex/Central}
{\newcommand{\bibliographyfile}{../../central-bibtex/Central}}
{
    \IfFileExists{central-bibtex/Central}
    {\newcommand{\bibliographyfile}{central-bibtex/Central}}
    {\newcommand{\bibliographyfile}{../../zentrale_BibTeX/Central}}
}

Is there some easy way to do this? I am thinking in Python:

dirs = ['../../central-bibtex', 'central-bibtex', '../../zentrale_BibTeX']
for dir in dirs:
    if os.path.isfile(dir + '/Central'):
        bibliographyfile = dir + '/Central'

I have tried TikZ and \foreach, but \bibliographyfile is not set to anything with this:

\newcommand\bibliographyfile{None}

\foreach \path in {
    ../../central-bibtex/Central,
    central-bibtex/Central,
    ../../zentrale_BibTeX/Central,
    /home/mu/Dokumente/Studium/zentrale_BibTeX/Central.bib
}
{
    \AtEndDocument{\path}
    \IfFileExists{\path}
    {\renewcommand\bibliographyfile{\path}}
    {}
}
Martin Ueding
  • 2,873
  • 4
  • 23
  • 38
  • Perhaps \forloop from forloop package might help? –  May 08 '14 at 18:41
  • Wouldn't it be easier to maintain a symbolic link from TEXMFHOME/bibtext/bib/Central.bib? – cfr May 08 '14 at 23:29
  • @cfr: Other people will clone the git repository containing the main file and also clone the repository that contains the bibliography into a location related to the first. So that link would have to be set up for every other user. – Martin Ueding May 09 '14 at 13:35

1 Answers1

2

Since the \foreach loop variable does not surive the loop, you need to expand the value that you are trying to save.. Below I use an \xdef to do that:

enter image description here

Notes:

  • I removed the underscores from the paths as that may cause some problems, but there should be solutions on this site that should help.

Code:

\documentclass{article}
\usepackage{tikz}

\newcommand\bibliographyfile{}

\foreach \FileNameWithPath in { ../central-bibtex/Central.bib, central-bibtex/Central.bib, ../../zentrale-BibTeX/Central.bib, /home/mu/Dokumente/Studium/zentrale-BibTeX/Central.bib }{% %\AtEndDocument{\path}% <-- not sure what this was for \IfFileExists{\FileNameWithPath}{% %\xdef\bibliographyfile{\FileNameWithPath}% % Per egreg's suggestions, replaced the above with: \expandafter\gdef\expandafter\bibliographyfile\expandafter{\FileNameWithPath} }{}% }

\begin{document}

Located file: \bibliographyfile

\end{document}

Peter Grill
  • 223,288
  • I'd be more conservative and say \xp\gdef\xp\bibliographyfile\xp{\FileNameWithPath} (where \xp stands for \expandafter). Special characters should now be safe. – egreg May 09 '14 at 09:15