7

I have a document using a labeled version of multibib, i.e. \usepackage[labeled]{multibib}. However, hyperref does not work on the cites and according to "Why does hyperlink point to a wrong page when option “labeled” for multibib is activated?", it seems one should use biblatex.

How can I achieve the same result as \usepackage[labeled]{multibib} using biblatex and have hyperref work correctly?

To be concrete, how can the minimal example document below be translated to biblatex?

\documentclass{article}
\usepackage[labeled]{multibib}
\usepackage{hyperref}

\begin{filecontents}{A.bbl}
    \begin{thebibliography}{1}
        \bibitem{slam2001}
        Thomas Ball and Sriram~K. Rajamani.
        \newblock Automatically validating temporal safety properties of interfaces.
        \newblock In {\em Proceedings of the 8th International SPIN Workshop on Model
          Checking of Software}, 2001.
    \end{thebibliography}
\end{filecontents}

\begin{filecontents}{B.bbl}
    \begin{thebibliography}{1}
        \bibitem{blast2007}
        Dirk Beyer, Thomas~A. Henzinger, Ranjit Jhala, and Rupak Majumdar.
        \newblock The software model checker blast: Applications to software
          engineering.
        \newblock {\em International Journal on Software Tools for Technology
          Transfer}, 2007.
    \end{thebibliography}
\end{filecontents}

\newcites{A,B}{Primary,Secondary}

\begin{document}
CiteA~\citeA{slam2001}, CiteB~\citeB{blast2007}

\bibliographyA{refs}
\bibliographyB{refs}
\end{document}

The example produces the following using pdflatex where the hyperref does not work correctly on cites:

Output from above tex document

foens
  • 285

1 Answers1

5

You could try the following.

We will presume we have two .bib files: \jobname-1.bib and \jobname-2.bib, here created via filecontents*

\begin{filecontents*}{\jobname-1.bib}
@inproceedings{slam2001,
  author    = {Thomas Ball and Sriram K. Rajamani},
  title     = {Automatically Validating Temporal Safety Properties of Interfaces},
  booktitle = {Proceedings of the 8th International SPIN Workshop on Model Checking of Software},
  editor    = {Matthew Dwyer},
  date      = {2001},
}
\end{filecontents*}
\begin{filecontents*}{\jobname-2.bib}
@article{blast2007,
  author    = {Dirk Beyer and Thomas A. Henzinger and Ranjit Jhala and Rupak Majumdar},
  title     = {The software model checker {BLAST}},
  subtitle  = {Applications to software engineering},
  journal   = {International Journal on Software Tools for Technology Transfer},
  date      = {2007},
  volume    = {9},
}
\end{filecontents*}

\addbibresource{\jobname-1.bib} \addbibresource{\jobname-2.bib}

These two files will be added to different keywords with Biber's sourcemapping

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \perdatasource{\jobname-1.bib}
      \step[fieldset=keywords, fieldvalue={, primary}, append]
    }
    \map{
      \perdatasource{\jobname-2.bib}
      \step[fieldset=keywords, fieldvalue={, secondary}, append]
    }
  }
}

\jobname-1.bib gets the keyword primary, \jobname-2.bib gets secondary. (I chose primary and secondary here, but you can pick anything you like. Just make sure that these keywords are not used for anything else in your .bib file or in your bibliography setup.) Note that the names of the .bib files are 'hard-coded' in \perdatasource. If you have different files names, you need to adapt the code accordingly.

If you insist on continuous numbering (i.e. do not want the second bibliography to start at [B1]) we'll need to put an end to biblatex's auto-detection of resetnumbers in case labelprefix is used:

\makeatletter
\providerobustcmd*{\blx@kv@defkey}{\define@key}

\blx@kv@defkey{blx@bib1}{noresetnumbersforlabelprefix}[true]{% \ifstrequal{#1}{true} {\let\blx@saved@refcontext@labelprefix\blx@refcontext@labelprefix \let\blx@saved@refcontext@labelprefix\blx@refcontext@labelprefix@real \let\blx@refcontext@labelprefix@empty \let\blx@refcontext@labelprefix@real@empty} {}}

\blx@kv@defkey{blx@bib2}{noresetnumbersforlabelprefix}[true]{% \ifstrequal{#1}{true} {\let\blx@refcontext@labelprefix\blx@saved@refcontext@labelprefix \let\blx@refcontext@labelprefix@real\blx@saved@refcontext@labelprefix} {}} \makeatother

All the entries can be cited via \cite (so the keyword does not matter, there is not \citeA or \citeB).

The bibliography is printed with

\newrefcontext[labelprefix=A]
\printbibliography[keyword=primary, noresetnumbersforlabelprefix, title=Primary]
\newrefcontext[labelprefix=B]
\printbibliography[keyword=secondary, noresetnumbersforlabelprefix, title=Secondary]

That is the first bibliography only contains entries with the keyword primary (effectively that is entries from \joabname-1.bib), while the second contains secondary entries (\jobname-2.bib). I have added prefixes to the labels via the labelprefix option of \newrefcontext (that comes closer to your example). The heading/title is controlled by the title option.

MWE

\documentclass{article}
\usepackage[style=numeric, defernumbers=true, backend=biber, maxnames=999]{biblatex}
\usepackage{hyperref}

\DeclareSourcemap{ \maps[datatype=bibtex]{ \map{ \perdatasource{\jobname-1.bib} \step[fieldset=keywords, fieldvalue={, primary}, append] } \map{ \perdatasource{\jobname-2.bib} \step[fieldset=keywords, fieldvalue={, secondary}, append] } } }

\makeatletter \providerobustcmd*{\blx@kv@defkey}{\define@key}

\blx@kv@defkey{blx@bib1}{noresetnumbersforlabelprefix}[true]{% \ifstrequal{#1}{true} {\let\blx@saved@refcontext@labelprefix\blx@refcontext@labelprefix \let\blx@saved@refcontext@labelprefix\blx@refcontext@labelprefix@real \let\blx@refcontext@labelprefix@empty \let\blx@refcontext@labelprefix@real@empty} {}}

\blx@kv@defkey{blx@bib2}{noresetnumbersforlabelprefix}[true]{% \ifstrequal{#1}{true} {\let\blx@refcontext@labelprefix\blx@saved@refcontext@labelprefix \let\blx@refcontext@labelprefix@real\blx@saved@refcontext@labelprefix} {}} \makeatother

\begin{filecontents}{\jobname-1.bib} @inproceedings{slam2001, author = {Thomas Ball and Sriram K. Rajamani}, title = {Automatically Validating Temporal Safety Properties of Interfaces}, booktitle = {Proceedings of the 8th International SPIN Workshop on Model Checking of Software}, editor = {Matthew Dwyer}, date = {2001}, } @article{blast2008, author = {Dirk Beyer and Thomas A. Henzinger and Ranjit Jhala and Rupak Majumdar}, title = {BLAST II}, journal = {International Journal on Software Tools for Technology Transfer}, date = {2008}, volume = {18}, } \end{filecontents} \begin{filecontents}{\jobname-2.bib} @article{blast2007, author = {Dirk Beyer and Thomas A. Henzinger and Ranjit Jhala and Rupak Majumdar}, title = {The software model checker {BLAST}}, subtitle = {Applications to software engineering}, journal = {International Journal on Software Tools for Technology Transfer}, date = {2007}, volume = {9}, } @inproceedings{slam2002, author = {Thomas Ball and Sriram K. Rajamani}, title = {Interfaces are Cool!}, booktitle = {Proceedings of the 8th International SPIN Workshop on Model Checking of Software}, date = {2002}, } \end{filecontents} \addbibresource{\jobname-1.bib} \addbibresource{\jobname-2.bib}

\begin{document} CiteA~\cite{slam2001,slam2002}, CiteB~\cite{blast2007,blast2008}

\newrefcontext[labelprefix=A] \printbibliography[keyword=primary, noresetnumbersforlabelprefix, title=Primary] \newrefcontext[labelprefix=B] \printbibliography[keyword=secondary, noresetnumbersforlabelprefix, title=Secondary] \end{document}

CiteA [A1, B3], CiteB [B4, A2]

Hyperlinking seemed to work rather well here.

edit The answer was updated to work with more recent versions of biblatex (tested with v3.14, but it should work from v3.12 onward). If you are using an ancient version of biblatex (v3.3 or below), please refer to the edit history.

moewe
  • 175,683
  • I now have your example working. However, is it possible to have Primary use numbers [1, ..., n] and Secondary [n+1, ..., m] like in the original example showint CiteA [A1] and CiteB [B2]? – foens Apr 15 '14 at 08:08
  • @foens The prefixnumbers option implicitly calls resetnumbers (causing the numbering to start again from 1), maybe there is a workaround, I'm investigating. – moewe Apr 15 '14 at 13:23
  • @foens The updated answer shows a way to disable the implicit resetnumbers=true. – moewe Apr 16 '14 at 05:29
  • I couldn't get the updated version to work. Your MWE works fine, however, I wan't to save the bibliographies to primary.bib and secondary.bib, and then it blows up :) Anyways, thank you for your time, I can live with the numbers being a bid off. – foens Apr 16 '14 at 16:35
  • 1
    @foens You have to replace all occurrences of \jobname-1(.bib) in the MWE by primary(.bib) and all of \jobname-2(.bib) by secondary(.bib). You furthermore cannot use the keywords primary and secondary in any other context (if you want to, you need to replace every occurrence of primary and secondary above by other appropriate keywords). – moewe Apr 17 '14 at 16:35
  • @foens Could you be a bit more specific about what blows off, ideally with the error message/warning (or the expected output, in contrast to what you got)? What versions of biblatex/Biber are you using (it's version 2.8/1.8 here)? Have you tried an update? – moewe Apr 17 '14 at 16:36
  • I researched it a bit. It was a problem with TeXstudio and filecontents overwriting the .bib files each time. Building via the commands pdflatex, bibtex, pdflatex, pdflatex everything works correctly. Sorry for the confusion. – foens Apr 18 '14 at 11:47
  • @foens No worries. Note though that "in the wild" you would not use filecontents, you will just have the two files \jobname-1.bib and \jobname-2.bib (whatever their names be) in a place LaTeX can find them. (I'm not even sure you were implying that you use filecontents in your actual document, but better safe than sorry, I suppose). – moewe Apr 18 '14 at 12:57
  • If I run the MWE without the lines between \makeatletter .. \makeatother I still get continuous numbering.... – alexis Jul 22 '20 at 20:27
  • 1
    @alexis Oh yeah. Thanks for pointing that out. Some internals changed that meant that the complete answer wasn't working as intended any more (and indeed the \makeatletter...\makeatother code does nothing in newer versions of biblatex). That should be fixed now, the code should once again produce the result from the screenshot. – moewe Jul 23 '20 at 06:05
  • Thank you @moewe. I think your comment "You furthermore cannot use the keywords primary and secondary in any other context" is so important that has to be in the answer too. I was all day fighting with apparently random zeros in my tex, and now figure out that mendeley (used to created the .bib) sometimes set the keywords field. – alexis Jul 23 '20 at 19:54
  • @moewe So now to avoid continuous numbering we just avoid the lines between \makeatletter...\makeatother? It is strange... some times it compiles like that, sometimes puts a lot of zeros.... not sure how to reproduce what I see. Also, is there a way to avoid the B label? (i.e. A0, A1, and then 1, 2, 3)... sorry for ask all this details.. not sure if is better to open a new question. – alexis Jul 23 '20 at 20:53
  • I found it! \newrefcontext[labelprefix=A] \printbibliography[keyword=primary, title=Primary] \newrefcontext[] \printbibliography[keyword=secondary, title=Secondary, resetnumbers=true] – alexis Jul 23 '20 at 21:08
  • 1
    @alexis Yes. Usually when labelprefix is active, biblatex automatically resets the numbering of the bibliography entries in each new bibliography (see also my very first comment above). The code in \makeatletter...\makeatother was specifically put in to avoid that. Because the MWE uses defernumbers you will need an additional LaTeX run to get correctly numbered citations and bibliography entries. Sometimes when things go wrong or you experiment with many options and settings, defernumbers numbering can 'get stuck' with wrong numbers: possibly all zeros, possibly something more subtle – moewe Jul 24 '20 at 06:43
  • 1
    @alexis ... In those cases it helps to delete the .aux, .bbl and .bcf file and recompile. That shouldn't happen often. Instead of \newrefcontext[labelprefix=A] \printbibliography[keyword=primary, title=Primary] \newrefcontext[] \printbibliography[keyword=secondary, title=Secondary, resetnumbers=true] you could also say \begin{refcontext}[labelprefix=A] \printbibliography[keyword=primary, title=Primary] \end{refcontext} \printbibliography[keyword=secondary, title=Secondary, resetnumbers=true]. Which feels more natural will depend on your situation. – moewe Jul 24 '20 at 06:49