Search code examples
markdownpandocbibtex

Generate Bibliography file from multiple files with Pandoc


I have several chapters with citations. My citation file is in bibtex format. I would like to create a formatted bibliography that includes all the citations from the chapters in a single file (publisher prefers DOCX). How can I do this?


Solution

  • If the bibtex file contains no additional citations, then it would be sufficient to have a small nocite.md Markdown file to generate the bibliography:

    ---
    nocite: '@*'
    ---
    
    # Bibliography
    

    Calling pandoc --output=bibliography.docx --bibliography YOUR_BIBTEX.bib nocite.md will generate a docx file with formatted entries for all items in YOUR_BIBTEX.bib.


    The more general case is that the bibtex file contains additional entries which should be omitted from the bibliography. One will need a way to limit output to citations used in the document(s). A good method is to use a Lua filter to rewrite the document as needed.

    -- save this file as "bib-only.lua"
    
    local cites = {}
    
    -- collect all citations
    function Cite (cite)
      table.insert(cites, cite)
    end
    
    -- use citations, but omit rest of the document
    function Pandoc (doc)
      doc.meta.nocite = cites
      doc.blocks = {}
      return doc
    end
    

    Running

    pandoc --lua-filter bib-only.lua -o bib.docx chapter1.md chapter2.md chapter3.md
    

    should give the desired output.