0

A paper I am currently writing references some online sources I'd like to snapshot for reference. My bib file could be enhanced to include file entries for such saved snapshots.

I was wondering if/how I could use the output of the main document to create a second document acting as an index for the snapshots. Ideally one would open the index PDF and have just the subset of references with file entries which could be clicked on to access the snapshot.

Unfortunately searching the internet didn't prove that helpful using keywords as biblatex, reuse, bibliogpraphy...

1

There is no official interface to reuse the bibliography from a different document directly. Of course you can use the same .bib file, but that is by for not enough to get exactly the same output as another document.

In How to import / print a bibliography created from a separate / external document? and How to use refsection and xcite together? you can find a strategy to reuse the bibliography of another document by reading its .bbl file. In principle this might help you here as well. There are a lot of caveats attached to this strategy, but those appear to be acceptable for what you have in mind.

Assume the parent file myparent.tex looks roughly like this

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[backend=biber, style=authoryear]{biblatex}

\begin{filecontents}{\jobname.bib}
@online{elk,
  author    = {Anne Elk},
  title     = {A Theory on Brontosauruses},
  year      = {1972},
  url       = {https://example.edu/~elk/bronto.pdf},
  file      = {./elk-bronto.pdf},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\addbibresource{biblatex-examples.bib}

\begin{document}
Lorem \autocite{sigfridsson,elk,worman,markey}

\printbibliography
\end{document}

Several bibliography entries.

Then you can use \importbibfrom as defined in the links to import the .bbl file of myparent into a child document. (Note that for that to work either both documents must live in the same working directory or you must manually copy myparent.bbl into the same directory as the child document.)

With the data from the parent file imported, it is just a matter of printing the file field (which is ignored by default) and filtering only those entries with a file field.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[backend=biber, style=authoryear]{biblatex}

\DefineBibliographyStrings{english}{
  file = {file archived at},
}

\DeclareFieldFormat{file}{\bibstring{file}\addspace\url{#1}}

\renewbibmacro*{finentry}{%
  \newunit
  \printfield{file}%
  \finentry}

\defbibcheck{hasfile}{%
  \iffieldundef{file}{\skipentry}{}}

\makeatletter
\newcommand*{\importbibfrom}[1]{%
  \def\blx@bblfile{%
    \blx@secinit
    \begingroup
    \blx@bblstart
    \InputIfFileExists{#1.bbl}
      {\blx@info@noline{... file '#1.bbl' found}%
       \global\toggletrue{blx@bbldone}}
      {\blx@info@noline{... file '#1.bbl' not found}%
       \typeout{No file #1.bbl.}}%
    \blx@bblend
    \endgroup
    % global sorting as this is called at BeginDocument
    \csnumgdef{blx@labelnumber@\the\c@refsection}{0}}}

\global\let\blx@rerun@biber\relax
\makeatother

\importbibfrom{myparent}

\begin{document}
\printbibliography[check=hasfile]
\end{document}

Only one entry with file field.

1
  • Many, many thanks! This solution is awesome! It does exactly what I wanted. I had to change one minor thing though: \DeclareFieldFormat{file}{\bibstring{file}\addspace\url{#1}} didn't work for me with hyperref. Changing it to \DeclareFieldFormat{file}{\bibstring{file}\addspace\href{./#1}{#1}} solved it and the referenced file opens in TexStudio (SumatraPDF fails but this is due to a current bug in 3.2)
    – ViToni
    Dec 5 '20 at 21:17

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.