1

The answer of moewe to the question Biblatex: Two bibliographies with different styles and sortings is almost what I want to achieve. Because of the large number of entries in my document, I would prefer to use the style numeric-comp instead of just numeric. How can I get this?
Extended MWE:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents}{biblatextest1.bib}
@BOOK{BookA03,
  author    = {Author Aaa},
  title     = {Some Title},
  publisher = {Some Publisher},
  year      = 2003,
}
@BOOK{BookB02,
  author    = {Author Bbb},
  title     = {Some Title},
  publisher = {Some Publisher},
  year      = 2002,
}
@BOOK{BookB09,
    author    = {Author Jjj},
    title     = {Some Title},
    publisher = {Some Publisher},
    year      = 2002,
}
@BOOK{BookB10,
    author    = {Author Kkk},
    title     = {Some Title},
    publisher = {Some Publisher},
    year      = 2002,
}
@BOOK{BookB11,
    author    = {Author Lll},
    title     = {Some Title},
    publisher = {Some Publisher},
    year      = 2002,
}
\end{filecontents}

\begin{filecontents}{biblatextest2.bib}
@MISC{LinkC04,
  author  = {Author Ccc},  
  title   = {Some Title},
  year    = 2004,
  url     = {www.test1.com/bild.jpg},
}
@MISC{LinkD01,
  author  = {Author Ddd},
  title   = {Some Title},
  year    = 2001,
  url     = {www.test2.com/bild.jpg},
}
@MISC{LinkD05,
    author  = {Author Eee},  
    title   = {Some Title},
    year    = 2004,
    url     = {www.test3.com/bild.jpg},
}
@MISC{LinkD06,
    author  = {Author Fff},
    title   = {Some Title},
    year    = 2001,
    url     = {www.test4.com/bild.jpg},
}
@MISC{LinkD07,
    author  = {Author Ggg},  
    title   = {Some Title},
    year    = 2004,
    url     = {www.test5.com/bild.jpg},
}
@MISC{LinkD08,
    author  = {Author Hhh},
    title   = {Some Title},
    year    = 2001,
    url     = {www.test6.com/bild.jpg},
}
\end{filecontents}

\usepackage[style = alphabetic, labelnumber, defernumbers = true,  backend = biber]{biblatex}
\addbibresource{biblatextest1.bib}
\addbibresource{biblatextest2.bib}

\usepackage{hyperref}

%Append keywords to identify different bibliography entries.
\DeclareSourcemap{
    \maps[datatype=bibtex, overwrite]{
        \map{
            \perdatasource{biblatextest1.bib}
            \step[fieldset=KEYWORDS, fieldvalue=primary, append]
        }
        \map{
            \perdatasource{biblatextest2.bib}
            \step[fieldset=KEYWORDS, fieldvalue=secondary, append]
        }
    }
}

\DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{#1}}
\renewbibmacro*{cite}{%
    \printtext[bibhyperref]{%
        \printfield{labelprefix}%
        \ifkeyword{secondary}
        {\printfield{labelnumber}}
        {\printfield{labelalpha}%
            \printfield{extraalpha}}}}


\defbibenvironment{bibliographyNUM}
{\list
    {\printtext[labelnumberwidth]{%
            \printfield{prefixnumber}%
            \printfield{labelnumber}}}
    {\setlength{\labelwidth}{\labelnumberwidth}%
        \setlength{\leftmargin}{\labelwidth}%
        \setlength{\labelsep}{\biblabelsep}%
        \addtolength{\leftmargin}{\labelsep}%
        \setlength{\itemsep}{\bibitemsep}%
        \setlength{\parsep}{\bibparsep}}%
    \renewcommand*{\makelabel}[1]{\hss##1}}
{\endlist}
{\item}

\assignrefcontextkeyws[sorting=none]{secondary}
\begin{document}

    The first two citations \cite{LinkD07,LinkC04} and \cite{BookB10,BookB11,BookB02,BookB09}. 
    The others are \cite{LinkD08,LinkD01,LinkD05,LinkD06} and \cite{BookA03}.

    \printbibliography[title=Bibliography, keyword=primary]

    \newrefcontext[sorting=none]
    \printbibliography[env=bibliographyNUM,title=References, keyword=secondary, resetnumbers]
\end{document}

enter image description here

Thank you for your help in advance.

  • 2
    Did you misname the author of the answer or did you post the wrong link? There is no answer by moewe to the question you linked. There is a relevant-seeming answer by lockstep, but I don't know if it is the one you meant to reference or not. – cfr Feb 16 '18 at 0:01
  • Sorry, I posted the wrong link. But now is correct. Thank you for your remark. – Ñako Feb 16 '18 at 7:01
  • @cfr. In short, I need two bibliographies. The first one in alphabetic style sorted by author name and the second one in numeric-comp style sorted by citations order. – Ñako Feb 16 '18 at 7:16
  • 2
    Mhhh, to do the same with numeric-comp would be quite a bit more complicated, since numeric-comp's cite macros are vastly more complicated than numeric's. What happens if you cite numeric and alphabetic citations in the same \cite? – moewe Feb 16 '18 at 8:11
  • @moewe . I know, I have checked the codes of alphabetic.cbx, numeric.cbx and numeric-comp.cbx. It seems to work well: The first two citations \cite{BookB10,LinkD07,LinkC04} and \cite{BookB10,BookB11,LinkD07,LinkC04,BookB02,BookB09}. The others are \cite{LinkD08,LinkD01,BookA03,LinkD05,LinkD06} and \cite{BookA03,LinkC04}. Just replacing style=alphabetic with bibstyle = alphabetic, citestyle = numeric gives the same result. But with bibstyle = alphabetic, citestyle = numeric-comp is the entire citation style numeric. – Ñako Feb 16 '18 at 13:07
3

I think I managed to get somewhere based on the numeric-comp and than trying to bring labelalpha information to the shorthand field, which is dealt with natively by the numeric-comp, so you don't need to redefine the cite macros/commands.

I initially tried to do this in the source map, but somehow it didn't work, so I ended up using \AtEveryCitekey for that.

You will probably still have to tune sorting. And you will also notice that the mixed citation of primary and secondary references is less than ideal (I couldn't find out what was determining the sortcite in this case). But the overall result looks reasonable.

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents}{biblatextest1.bib}
@BOOK{BookA03,
  author    = {Author Aaa},
  title     = {Some Title},
  publisher = {Some Publisher},
  year      = 2003,
}
@BOOK{BookB02,
  author    = {Author Bbb},
  title     = {Some Title},
  publisher = {Some Publisher},
  year      = 2002,
}
@BOOK{BookB09,
    author    = {Author Jjj},
    title     = {Some Title},
    publisher = {Some Publisher},
    year      = 2002,
}
@BOOK{BookB10,
    author    = {Author Kkk},
    title     = {Some Title},
    publisher = {Some Publisher},
    year      = 2002,
}
@BOOK{BookB11,
    author    = {Author Lll},
    title     = {Some Title},
    publisher = {Some Publisher},
    year      = 2002,
}
\end{filecontents}

\begin{filecontents}{biblatextest2.bib}
@MISC{LinkC04,
  author  = {Author Ccc},  
  title   = {Some Title},
  year    = 2004,
  url     = {www.test1.com/bild.jpg},
}
@MISC{LinkD01,
  author  = {Author Ddd},
  title   = {Some Title},
  year    = 2001,
  url     = {www.test2.com/bild.jpg},
}
@MISC{LinkD05,
    author  = {Author Eee},  
    title   = {Some Title},
    year    = 2004,
    url     = {www.test3.com/bild.jpg},
}
@MISC{LinkD06,
    author  = {Author Fff},
    title   = {Some Title},
    year    = 2001,
    url     = {www.test4.com/bild.jpg},
}
@MISC{LinkD07,
    author  = {Author Ggg},  
    title   = {Some Title},
    year    = 2004,
    url     = {www.test5.com/bild.jpg},
}
@MISC{LinkD08,
    author  = {Author Hhh},
    title   = {Some Title},
    year    = 2001,
    url     = {www.test6.com/bild.jpg},
}
\end{filecontents}

\usepackage[style = numeric-comp, labelalpha, defernumbers]{biblatex}
\addbibresource{biblatextest1.bib}
\addbibresource{biblatextest2.bib}

\usepackage{hyperref}

%Append keywords to identify different bibliography entries.
\DeclareSourcemap{
    \maps[datatype=bibtex, overwrite]{
        \map{
            \perdatasource{biblatextest1.bib}
            \step[fieldset=KEYWORDS, fieldvalue=primary]
        }
        \map{
            \perdatasource{biblatextest2.bib}
            \step[fieldset=KEYWORDS, fieldvalue=secondary]
        }
    }
}

\makeatletter
\newcommand*{\labelalphatoshorthand}{\csdef{abx@field@shorthand}{\strfield{labelprefix}\strfield{labelalpha}\strfield{extraalpha}}}
\makeatother

\AtEveryCitekey{\ifkeyword{primary}{\labelalphatoshorthand}{}}

\DeclareFieldFormat{labelalphawidth}{\mkbibbrackets{#1}}

\defbibenvironment{bibliographyALPHA}
  {\list
     {\printtext[labelalphawidth]{%
        \printfield{labelprefix}%
        \printfield{labelalpha}%
        \printfield{extraalpha}}}
     {\setlength{\labelwidth}{\labelalphawidth}%
      \setlength{\leftmargin}{\labelwidth}%
      \setlength{\labelsep}{\biblabelsep}%
      \addtolength{\leftmargin}{\labelsep}%
      \setlength{\itemsep}{\bibitemsep}%
      \setlength{\parsep}{\bibparsep}}%
      \renewcommand*{\makelabel}[1]{##1\hss}}
  {\endlist}
  {\item}

\begin{document}

The first two citations \cite{LinkD07,LinkC04} and
\cite{BookB10,BookB11,BookB02,BookB09}.

Mixing both primary and secondary
\cite{LinkD08,LinkD01,LinkD05,LinkD06,BookB10,BookB11,BookB02,BookB09}.

The others are \cite{LinkD08,LinkD01,LinkD05,LinkD06,LinkD07}
and \cite{BookA03}.

\printbibliography[env=bibliographyALPHA, title=Bibliography, keyword=primary]

\newrefcontext
\printbibliography[title=References, keyword=secondary, resetnumbers]

\end{document}

enter image description here

Off-topic: notice I removed your append instructions from your DeclareSourcemap steps. The idea of appending is a good one. But suppose you have one field with a keyword mykeyword and append primary to it. You will get mykeywordprimary, which is neither mykeyword nor primary, and your keyword based mechanism for separating the bibliographies will fail (actually you will have missing bibliography entries). So to do that, you would have to include a comma between the keywords and handle the case of the field being empty or not, and so on.

| improve this answer | |
  • Thanks, that is exactly what I was looking for. I considered to define firstly the stylenumeric-compinstead of alphabetic, but I thought it was more difficult to redefine the macros/commands. Actually I do not need to mixed citations, but it is nice to know, that it also works. Is there a reason why did you remove the option backend=biber from biblatex package definition? I have also included the option [labelprefix=a] to \newrefcontext (it works with the option backend=biber). – Ñako Feb 19 '18 at 10:47
  • In case of the ranges, it produces a2-a4 or a2-a6. Is it possible to remove the 2nd labelprefix to obtain a2-4 or a2-6, but maintaining the labelprefix in the bibliography, that is: [a4] or [a6]? – Ñako Feb 19 '18 at 10:54
  • @Ñako, the reason I removed the option backend=biber is that it is already the default. But there is no harm in leaving it, if you prefer. – gusbrs Feb 19 '18 at 10:55
  • 1
    By the way, I found how to remove the "2nd" labelprefix; redefining the cite:dump macro of numeric-comp.cbx and erasing \printtext[labelprefix]{\cbx@lastprefix}, that is, \renewbibmacro*{cite:dump}{% \ifnumgreater{\value{cbx@tempcnta}}{0} {\ifnumgreater{\value{cbx@tempcnta}}{1} {\bibrangedash} {\multicitedelim}% \bibhyperref[\cbx@lastkey]{% \ifdef\cbx@lastprefix {} {}% \printtext[labelnumber]{\cbx@lastnumber} }} {}% \setcounter{cbx@tempcnta}{0}% \global\undef\cbx@lastprefix} – Ñako Feb 19 '18 at 16:21
  • 1
    @Ñako Yes, in this case, \strfield{extraalpha} will produce a number and \printfield{extraalpha} is its "formatted" version, which by default will be a letter. Either is good practice, depending on the desired result. – gusbrs Apr 20 '19 at 1:30

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.