13

MWE for the problem I encountered:

\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
}
    \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
}
\end{filecontents}

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

%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]
    }
  }
}

%Declare new sorting scheme for refernence bibliography.
\DeclareSortingScheme{appearance}{
    \sort{\citeorder}
}

%Copied from numeric.cbx to imitate numerical citations.
\providebool{bbx:subentry}
\newbibmacro*{citenum}{%Note: the original macro was called "cite". I did not redefine "cite", but instead defined a new macro "citenum", because the author-year citations use the "cite" macro too. "\renewbibmacro*{cite}" would have caused all the author-year citations to become numeric too.
  \printtext[bibhyperref]{%If you ever want to use hyperref
    \printfield{prefixnumber}%
    \printfield{labelnumber}%
    \ifbool{bbx:subentry}
      {\printfield{entrysetcount}}
      {}}}

%Copied from numeric.cbx to define a new numeric citation command for @online entries.
\DeclareCiteCommand{\cnume}[\mkbibbrackets]
  {\usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \usebibmacro{citenum}}%Note: this was originally "cite" but I changed it to "citenum" to avoid clashes with the author-year style.
  {\multicitedelim}
  {\usebibmacro{postnote}}


\begin{document}

The first two citations \cnume{LinkD01} and \cite{BookB02}. 
The others are \cnume{LinkC04} and \cite{BookA03}.

\printbibliography[title=Bibliography, keyword=primary]

%Redefine the bibliography environment to imitate the numeric citation style
\defbibenvironment{bibliographyNUM}
    {\list
    {\printfield[labelnumberwidth]{labelnumber}}
    {\setlength{\labelwidth}{\labelnumberwidth}%
    \setlength{\leftmargin}{\labelwidth}%
    \setlength{\labelsep}{\biblabelsep}%
    \addtolength{\labelsep}{1em}
    \addtolength{\leftmargin}{\labelsep}%
    \setlength{\itemsep}{\bibitemsep}%
    \setlength{\parsep}{\bibparsep}}%
    \renewcommand*{\makelabel}[1]{\hss##1}}
    {\endlist}
    {\item}
    \DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{#1}\hspace{-1.1em}}

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

The problem: I need two bibliographies, the first one with alphabetic style and sorted by author name. The second in numeric style sorted by cite order. Both have no common references. Somehow I'm searching for something like this in biblatex:

\bibliographystyle{style = alphabetic}
\printbibliography{books.bib} 
\bibliographystyle{style = numerical, sorting = none}  
\printbibliography{links.bib}

I thought biblatex is especially written to work with multiple bibliographies, but I haven't found any other way than the one shown in the MWE (which is obviously a hack). Actually it works pretty good until I want the other sorting. When I use

\newrefcontext[sorting=none]

The cite numbers turn to zero.

Jannik
  • 133
  • biblatex is written to support split/multiple bibliographies (amongst many other features), but it doesn't official support mixing different bibliography styles in one document. – moewe Mar 15 '16 at 07:33

3 Answers3

14

If you pass the option labelnumber to biblatex you can use numeric citations even with style=alphabetic.

The cite command can switch based on keywords, so you can use \cite for all entries regardless of keyword.

For the numeric bibliography we will have to define a new bibliography environment that prints numeric citations. bibliographyNUM is directly copied from numeric.bbx. To set the sorting for the bibliography, just say

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

before the numeric bibliography. The previous bibliography will use the global sorting scheme anyt that is appropriate for alpha-style bibliography. In case it is necessary to keep the sorting scheme assignment local (because the numeric bibliography comes before the alpha bibliography), you would use

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

MWE

\documentclass{article}
\usepackage[style=alphabetic, labelnumber, defernumbers=true,  backend=biber]{biblatex}
\usepackage{hyperref}

% Append keywords to identify different bibliography entries. % appendstrict only appends if the field is nonempty, % we use that to add a comma to avoid mushing together two keywords \DeclareSourcemap{ \maps[datatype=bibtex, overwrite]{ \map{ \perdatasource{biblatextest1.bib} \step[fieldset=KEYWORDS, fieldvalue={, }, appendstrict] \step[fieldset=KEYWORDS, fieldvalue=primary, append] } \map{ \perdatasource{biblatextest2.bib} \step[fieldset=KEYWORDS, fieldvalue={, }, appendstrict] \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{labelprefix}% \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}

\begin{filecontents}{biblatextest1.bib} @BOOK{BookA03, author = {Author Aaa}, title = {Some Title}, publisher = {Some Publisher}, year = 2003, keywords = {hello}, } @BOOK{BookB02, author = {Author Bbb}, 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}, keywords = {bye}, } @MISC{LinkD01, author = {Author Ddd}, title = {Some Title}, year = 2001, url = {www.test2.com/bild.jpg}, } \end{filecontents} \addbibresource{biblatextest1.bib} \addbibresource{biblatextest2.bib}

\begin{document} The first two citations \cite{LinkD01} and \cite{BookB02}. The others are \cite{LinkC04} and \cite{BookA03}.

\printbibliography[title=Bibliography, keyword=primary]

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

example output

moewe
  • 175,683
  • Works perfectly fine and looks elegant after I re-installed biblatex + biber package (MikTex user). Just wanted to leave this if somebody wonders about "uninitialized value" errors too. Also make sure to delete all files except of .tex as they can cause proplems as well. Nice workaround, thanks. – Jannik Mar 15 '16 at 10:44
  • @Jannik With an up to date version of both biblatex and BIber there should be no "uninitialized values" flying about. If the versions are older or don't match, however, the above will not work. After an update, it rarely hurts to delete the temporary files to make sure no old stuff sticks and causes problems. – moewe Mar 15 '16 at 10:48
  • 1
    If you add \newcommand{\letbibmacro}[2]{% \csletcs{abx@macro@#1}{abx@macro@#2}% } \letbibmacro{original-cite}{cite} you can use \usebibmacro{original-cite} inside your new \cite definition to insert whatever style you selected on load (e.g. authoryear, ...) (replaces \printfield{labelalpha}\printfield{extraalpha}). – opatut Jun 10 '17 at 16:05
  • @opatut Nice idea. In some situations the placement of the code is crucial though. It might also not always be a great idea to simply not call the standard macro at all (especially if it gets more complicated and performs clean-up work, I'm thinking of -icomp styles here). Here there would not be a problem, I could only save a line by moving the \if... around, but you could separate that again. – moewe Jun 10 '17 at 16:11
  • It's nice though in some cases if you're using more complex styles (such as authoryear, which is 163 lines of code in my distro) and you don't need to redefine them. – opatut Jun 10 '17 at 16:13
  • @opatut Mhhh, yes. Though the part of authoryear.cbx that needs modification (\newbibmacro*{cite}) is a mere eight lines long (before changes). – moewe Jun 10 '17 at 16:16
  • Ah right, I thought I wouldn't include it, so cite:* wouldn't be available. Well, then it probably doesn't matter. Still, redefining things should IMO be avoided if possible ;) (Thanks for original answer btw) – opatut Jun 10 '17 at 16:19
  • @Arne For Biber there is no difference between files created with filecontents and files created manually. There is currently a Biber bug that affects file path matching, so the file paths may be relevant here. Have a look at https://github.com/plk/biblatex/issues/922. – moewe Oct 11 '19 at 14:36
  • Works very well for me as long as my bibtex entries don't have keywords to begin with. But if an entry has a keyword "hello" it gets changed to "helloprimary" and the entry is not printed in the bibliography. Any idea how to fix that? – Gandalf Lechner Aug 05 '20 at 07:42
  • To answer my own question: Just don't use the "append" option - then existing keywords are overwritten and the solutions works perfectly – Gandalf Lechner Aug 05 '20 at 08:03
  • 1
    @GandalfLechner If you want to retain your original keywords, you can do slightly better. See the edited answer, please. – moewe Aug 05 '20 at 09:15
2

Thanks a lot. I wanted to share my adapted version of the problem. I divided my Bib in categories by defining different cite words. It was copied firm driftnet sources especially biblatex: multiple bibliographies categorised by different .bib files so it is not the best practice but it works. Hopefully biblatex will native support mixed bibliography styles in the future.

\documentclass[fontsize=11pt,a4paper,bibliography=totoc]{scrbook}
\usepackage[style=alphabetic,backend=biber,defernumbers = true]{biblatex} 
\addbibresource{bib/references.bib}

\DeclareBibliographyCategory{pubA}
\DeclareBibliographyCategory{contA}
\DeclareBibliographyCategory{refs}
\DeclareBibliographyCategory{relT}

\defbibheading{pubA}{\section*{Publications of the Author}}
\defbibheading{contA}{\section*{Contributions of the Author}}
\defbibheading{refs}{\section*{References}}
\defbibheading{relT}{\section*{Related Theses}}

\newcommand*{\citeA}[2][]{\addtocategory{pubA}{#2}\cite[#1]{#2}}
\newcommand*{\citeC}[2][]{\addtocategory{contA}{#2}\cite[#1]{#2}}
\newcommand*{\citeR}[2][]{\addtocategory{refs}{#2}\cite[#1]{#2}}

\renewbibmacro*{cite}{%
\printtext[bibhyperref]{%
 \printfield{labelprefix}%
    \ifcategory{refs}
   {\printfield{labelnumber}}
   {\printfield{labelalpha}%
    \printfield{extraalpha}}}}

\newcommand*{\citeT}[2][]{\addtocategory{relT}{#2}\cite[#1]{#2}}

\DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{#1}} % important to get [] in bib
\defbibenvironment{refsEnv}
{\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}
\begin{document}

some text \citeR{<citekey>} \citeT{<citekey>}...


\printbibheading[heading=bibintoc]
\printbibliography[heading=subbibliography,title={Publications of the Author},category=pubA]
\printbibliography[heading=subbibliography,title={Contributions of the Author},category=contA]
\newrefcontext[sorting=none]
\printbibliography[heading=subbibliography,title={References},category=refs,env=refsEnv,resetnumbers]
\printbibliography[heading=subbibliography,title={Related Theses},category=relT]

\end{document}
Gražvi
  • 21
  • 1
0

Do what @moewe says to use numeric citations even with style=alphabetic.

For different sort orders, there's a much easier way, the refcontext environment, which takes a sorting option. First, use Biblatex to generate your main bibliography:

\printbibliography[title=Bibliography, keyword=primary]

Then, create your second one with different sorts, filters, and styles by enclosing it in the refcontext with those as parameters.

\begin{refcontext}[sorting=ydnt]{} % sort chronologically
    \printbibliography[title=References, keyword=secondary, resetnumbers] 
\end{refcontext}

The empty {} as the last parameter of refcontext lets you create an ad hoc environment without declaring anything elsewhere.

I used this when my school required me to include a CV at the end of my thesis, so I needed to generate a bibliography of my works in reverse chronological order in addition to the usual A-Z bibliography for the paper. It was much cleaner than what I'd had.

Merchako
  • 135