2

I managed to add all bibfiles in a bib/ directory to my default resource list by using \write18 and pdflatex -shell-escape.

Is there a less cumbersome way to do add all bibfiles in a directory to the resource list?

\RequirePackage{filecontents}

  \begin{filecontents}{bib/1.bib}
   @book{key1, title="title1", author="author1", year="2001"}
  \end{filecontents}

  \begin{filecontents}{bib/2.bib}
   @book{key2, title="title2", author="author2", year="2002"}
  \end{filecontents}

  \begin{filecontents}{bib/3.bib}
   @book{key3, title="title3", author="author3", year="2003"}
  \end{filecontents}


\documentclass{article}
\usepackage[backend=biber]{biblatex}

\immediate\write18
{for i in bib/*; do echo "\string\\\string\\addbibresource\string{$i\string}"; done >bibresources.tex}

\input{bibresources}

\begin{document}
 \nocite{*}
 \printbibliography
\end{document}
n.r.
  • 4,942
  • If that works for you (on my windows machine, I could not get the echo to work just now), I think that is a very good solution. And it is not very cumbersome in the end, do you think? Without \write18, LaTeX has no way of knowing about the files in the ./bib folder without resorting to some way to scour the folder. – moewe Aug 27 '15 at 17:12
  • What I find really cumbersome is having to run pdflatex with a -shell-escape flag. Correct me if I'm wrong: not even biblatex itself would be able to list a bib directory for bibfiles if the user didn't pass -shell-escape to pdflatex ? – n.r. Aug 27 '15 at 17:19
  • As far as I know (and I'm not one hundred percent firm on these TeX core questions) TeX can only find files it knows (of) by name, so it can search in the input paths or via kpsewhich. If you don't specify a name, we need a way to obtain the name and that is what we need the shell-escape for; you need access to the entire file system. See also Inputting multiple files in LaTeX, How to iterate through the name of files in a folder. – moewe Aug 27 '15 at 17:29
  • 1
    Running pdflatex with -shell-escape does not appear more cumbersome that running it without (you could probably have your favourite editor create a new build tool with shell escape, or have a sot of shortcut in your command line). Fundamentally, biblatex cannot do more than LaTeX in finding files (it is a LaTeX package after all; if you leave out Biber, there could be a way using Biber, but that would then require - if at all possible - a much higher number of compiler passes.) I would like to mention though that there appears to be a LuaTeX solution. – moewe Aug 27 '15 at 17:32

1 Answers1

4

You don't need the shell escape, because you can do it from the shell.

\RequirePackage{filecontents}

  \begin{filecontents}{bib/1.bib}
   @book{key1, title="title1", author="author1", year="2001"}
  \end{filecontents}

  \begin{filecontents}{bib/2.bib}
   @book{key2, title="title2", author="author2", year="2002"}
  \end{filecontents}

  \begin{filecontents}{bib/3.bib}
   @book{key3, title="title3", author="author3", year="2003"}
  \end{filecontents}


\documentclass{article}
\usepackage{xparse}
\usepackage[backend=biber]{biblatex}

\ExplSyntaxOn
\NewDocumentCommand{\addbibresources}{m}
 {
  \clist_set:NV \l_tmpa_clist \bibresources
  \clist_map_inline:Nn \l_tmpa_clist
   {
    \addbibresource{#1/##1}
   }
 }
\ExplSyntaxOff
\addbibresources{bib}

\begin{document}
\nocite{*}
\printbibliography
\end{document}

If rostov.tex is the file, you can call the LaTeX run with

pdflatex "\def\bibresources{$(ls -m bib)}\input{rostov}"
egreg
  • 1,121,712