Search code examples
latexoverleaf

Overleaf / Latex: reference several bib files in parallel and "merge" them into a common literature directory


Overleaf / Latex:

How can I reference several parallel *.bib files in one single Latex project and create a common literature directory at the end without having to merge these two files?

Thanks a lot for your help!

I have already seen approaches that create several (mostly 1:1) bibliographies from several bib files using different packages (for example), but I have not yet found an approach or solution to my question above. If there is already a suitable entry, then I simply haven't found it and apologise for this... I hope I have defined the requirements clearly enough. If there is any missing information, please ask again and I will add it immediately.


Solution

  • If you are ok to use biblatex, you don't have to merge your libraries. Just add as many files as you need via multiple instances of \addbibresournce{<folder>/<file>}. Repeat the command for all required files that contain your references. This will work as long as <folder name> exists and is in the current project folder.

    Consider the code below. Let's say all files are gathered in the bib/, which is in the current project folder. Run the example below and all references should appear in the PDF, all loaded from separate files. Make sure bin/ folder exists.

    Notice the blocks of \begin{filecontents*}...\end{filecontents}`, which are only required to make the example self-contained as they create the file during compilation. In real case, the files would already exist.

    The code

    %%% --- THIS BLOCK CREATES files ---
    \begin{filecontents*}[overwrite]{bib/sample1.bib}
    @article{ref:einstein,
        author = "Albert Einstein",
        title = "{Zur Elektrodynamik bewegter K{\"o}rper}. ({German})
        [{On} the electrodynamics of moving bodies]",
        journal = "Annalen der Physik",
        volume = "322",
        number = "10",
        pages = "891--921",
        year = "1905",
        DOI = "http://dx.doi.org/10.1002/andp.19053221004",
        keywords = "physics"
    }
    \end{filecontents*}
    \begin{filecontents*}[overwrite]{bib/sample2.bib}
    @book{ref:dirac,
        title = {The Principles of Quantum Mechanics},
        author = {Paul Adrien Maurice Dirac},
        isbn = {9780198520115},
        series = {International series of monographs on physics},
        year = {1981},
        publisher = {Clarendon Press},
        keywords = {physics}
    }
    \end{filecontents*}
    \begin{filecontents*}[overwrite]{bib/sample3.bib}
    @online{ref:knuthwebsite,
        author = "Donald Knuth",
        title = "Knuth: Computers and Typesetting",
        url  = "http://www-cs-faculty.stanford.edu/~uno/abcde.html",
        addendum = "(accessed on 20/09/2023)",
        keywords = "latex,knuth"
    }
    \end{filecontents*}
    \begin{filecontents*}[overwrite]{bib/sample4.bib}
    @inbook{ref:knuth-fa,
        author = "Donald E. Knuth",
        title = "Fundamental Algorithms",
        publisher = "Addison-Wesley",
        year = "1973",
        chapter = "1.2",
        keywords  = "knuth,programming"
    }
    \end{filecontents*}
    %%% --- END --- %%%
    
    %%% REGULAR LATEX DOCUMENT STARTS HERE 
    \documentclass{article}
    \usepackage[
      backend=biber,
      style=apa,
      url=false,
    ]{biblatex}
    \usepackage[colorlinks]{hyperref}
    \usepackage[nopar]{kantlipsum}
    
    \addbibresource{bib/sample1.bib}
    \addbibresource{bib/sample2.bib}
    \addbibresource{bib/sample3.bib}
    \addbibresource{bib/sample4.bib}
    
    
    \begin{document}
    \section{Introduction}\label{sec:intro}
    \kant[1][1] \autocite{ref:einstein}.
    
    \section{Main section}\label{sec:main}
    \kant[1][2] \autocite{ref:dirac}.
    
    \section{Simple implementation}\label{sec:implementation}
    \kant[1][3] \autocite{ref:knuthwebsite}.
    
    \section{Conclusion}\label{sec:conclusion}
    \kant[1][4] \autocite{ref:knuth-fa}.
    
    \printbibliography
    \end{document}