1

I have nifty common.sty that I use is all my .tex papers (using \input) to provide uniform formatting, etc.

That common.sty uses \addbibresource to include a general bibliography that I use frequently in all my papers.

But, additionally, I'd like to automatically include specific bibliographies for some of my papers, even without having to explicitly do so in my .tex file. Specifically, I'd like the file my-funky-paper.bib to be automatically included, if and only if such a file lives in the same directory as my-funky-paper.tex file.

The catch is that I'd like to place the code that does this in the common.sty file.

Can you teach me how? This would be easy to do in any programming language I know, but ss such a thing possible in Tex?

  • 1
    \IfFileExists{\jobname.bib}{\addbibresource{\jobname.bib}}{} perhaps? – user31729 Jan 15 '17 at 12:03
  • 3
    Personally, i am against a file that suits all possible cases and rules them all. You can do it, but it will be a big lump of code that doesn't do anything useful. Consider 20 lines of code versus just on \addbibressource line in your tex. – Johannes_B Jan 15 '17 at 12:06
  • @JoaoTavora: I've changed my suggestion in the meantime ;-) ... and what Johannes_B said is so true! – user31729 Jan 15 '17 at 12:06
  • Johannes_B. It's a fair comment, but I'm a programmer,so I like to abstract away everything I can. And even in your specific example, it starts to pay off after 20 papers, which isn't much. – Joao Tavora Jan 15 '17 at 12:08
  • Johannes_B, if you can indeed do it, I ask you to please give me a hint as to how. Doesn't have to be a complete answer, I learn quickly :-) – Joao Tavora Jan 15 '17 at 12:10
1

Here is a possible way to do achieve such \jobname.bib - related behaviour, with \IfFileExists.

Please note that the whole TeX input path (i.e. available in $TEXINPUTS environment variable, for example) is searched, so having two or more files with the same \jobname and the relevant .bib files as well this procedure might fail (i.e. it will use the first file it finds ;-))

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mysophisticatedcommonstuff}

\RequirePackage{biblatex}% use with options
% More packages to be loaded

\IfFileExists{\jobname.bib}{%
  \addbibresource{\jobname.bib}% Please note that there is an optional argument to \addbibresource!
}{ \typeout{No \jobname.bib file found}}

% more stuff

\endinput

Some importer document:

\documentclass{article}

\usepackage{mysophisticatedcommonstuff}

\begin{document}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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