0

Based on the solutions to Biblatex: Two bibliographies with different styles and sortings II and Biblatex: Two bibliographies with different styles and sortings II Part 2 given by gusbrs and moewe, respectively, I would like to add the option labelprefix to the style numeric-comp with the following behavior:

  1. Delete the second labelprefix from the citation range if the citations follow the integer sequence of natural numbers, for example, tech. 1-3 instead of tech. 1-tech.3
  2. Also delete labelprefix in the case of combination of single with range citations or different non-consecutive citation ranges, for instance, tech. 1, 3-4 instead of tech. 1, tech. 3-4 or tech. 1-2, 4-6 instead of tech. 1-2, tech. 4-6

The first point can be achieved redifining the cite:dump macro of numeric.comp.cbx, i.e., deleting \printtext[labelprefix]{\cbx@lastprefix}:

\renewbibmacro*{cite:dump}{%
\ifnumgreater{\value{cbx@tempcnta}}{0}
{\ifnumgreater{\value{cbx@tempcnta}}{1}
    {\bibrangedash}
{\multicitedelim}%
\bibhyperref[\cbx@lastkey]{%
    \ifdef\cbx@lastprefix
           %{\printtext[labelprefix]{\cbx@lastprefix}} %deleted
            {} % added
            {}%
    \printtext[labelnumber]{\cbx@lastnumber}
    }}
{}%
\setcounter{cbx@tempcnta}{0}%
\global\undef\cbx@lastprefix}

What about the second one? Any ideas?

MWE:


\documentclass{article}

\usepackage{filecontents} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc}

\begin{filecontents}{biblatextest1.bib} @BOOK{BookA03, author = {Author Ät{"a}}, title = {Some Title}, publisher = {Some Publisher}, year = 2003, } @BOOK{BookB02, author = {Author Bb"{a}}, 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{LinkC01, author = {Author Ccc},
title = {Some Title}, year = 2004, url = {www.test1.com/bild.jpg}, } @MISC{LinkD04, 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}, } @MISC{LinkD09, author = {Author Iii}, title = {Some Title}, year = 2001, url = {www.test6.com/bild.jpg}, } \end{filecontents}

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

\usepackage[colorlinks=true,citecolor=blue,urlcolor=red]{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}{% \thefield{labelprefix}\thefield{labelalpha}\thefield{extraalpha}}} % \renewbibmacro{cite:dump}{% \ifnumgreater{\value{cbx@tempcnta}}{0} {\ifnumgreater{\value{cbx@tempcnta}}{1} {\bibrangedash} {\multicitedelim}% \bibhyperref[\cbx@lastkey]{% \ifdef\cbx@lastprefix % {\printtext[labelprefix]{\cbx@lastprefix}} %deleted {} % added {}% \printtext[labelnumber]{\cbx@lastnumber} }} {}% \setcounter{cbx@tempcnta}{0}% \global\undef\cbx@lastprefix} % \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,LinkC01} and \cite{BookB10,BookB11,BookB09}.

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

The others are \cite{LinkC01,LinkD08,LinkD05,LinkD06,LinkD07} as well as \cite{LinkC01,LinkD04,LinkD05,LinkD07,LinkD08, LinkD09} and \cite{BookA03}.

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

\newrefcontext[labelprefix=tech.\space] \printbibliography[title=References, keyword=secondary, resetnumbers]

\end{document}

enter image description here

epR8GaYuh
  • 2,432
Ñako
  • 3,626

1 Answers1

3

You may want to try the following solution. It requires severe rewriting of internal commands, but the style you want is quite different from what numeric-comp normally does.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[backend=biber, style=numeric-comp, labelalpha, defernumbers, locallabelwidth]{biblatex}

\usepackage[colorlinks=true,citecolor=blue,urlcolor=red]{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] } } }

\renewcommand*{\multicitedelim}{\addsemicolon\space}

\letbibmacro{cite:num:comp}{cite:comp}

\newbibmacro{cite:alpha}{% \printtext[bibhyperref]{% \printfield{labelprefix}% \printfield{labelalpha}% \printfield{extraalpha}% \ifbool{bbx:subentry} {\printfield{entrysetcount}} {}}}

\renewbibmacro*{cite:comp}{% \ifkeyword{primary} {\usebibmacro{cite:dump}% \ifnumgreater{\value{cbx@tempcntb}}{-1} {\multicitedelim} {\iffirstcitekey {} {\multicitedelim}}% \usebibmacro{cite:alpha}% \iflastcitekeylocal {} {\multicitedelim}% \setcounter{cbx@tempcnta}{0}% \setcounter{cbx@tempcntb}{-2}} {\usebibmacro{cite:num:comp}}}

\makeatletter \newcommand*\iflastcitekeylocal{% \ifnumequal\c@citecount\c@citetotal}

\renewbibmacro*{cite:init}{% \global\boolfalse{cbx:parens}% \global\undef\cbx@lasthash \global\undef\cbx@lastnumber \global\undef\cbx@lastprefix \global\undef\cbx@secondtolastprefix \setcounter{cbx@tempcnta}{0}% \setcounter{cbx@tempcntb}{-2}}

\newcommand*{\ifsamelabelprefix}{% \iffieldundef{labelprefix} {\ifundef\cbx@lastprefix} {\iffieldequals{labelprefix}{\cbx@lastprefix}}}

\renewbibmacro{cite:comp:comp}{% \renewcommand{\multicitedelim}{\addcomma\space}% \ifsamelabelprefix {\ifnumequal{\thefield{labelnumber}}{\value{cbx@tempcntb}} {\savefield{entrykey}{\cbx@lastkey}% \savefield{labelnumber}{\cbx@lastnumber}% \addtocounter{cbx@tempcnta}{1}} {\ifnumequal{\thefield{labelnumber}}{\value{cbx@tempcntb}-1} {} {\usebibmacro{cite:dump}% \ifnumgreater{\value{cbx@tempcntb}}{-1} {\multicitedelim} {}% \printtext[bibhyperref]{% \iffieldequals{labelprefix}{\cbx@lastprefix} {} {\printfield{labelprefix}}% \printfield{labelnumber}}}}} {\usebibmacro{cite:comp:end}}% \setcounter{cbx@tempcntb}{\thefield{labelnumber}}% \global\let\cbx@secondtolastprefix\cbx@lastprefix \savefield{labelprefix}{\cbx@lastprefix}}

\renewbibmacro*{cite:dump}{% \ifnumgreater{\value{cbx@tempcnta}}{0} {\ifnumgreater{\value{cbx@tempcnta}}{1} {\bibrangedash} {\multicitedelim}% \bibhyperref[\cbx@lastkey]{% \ifdef\cbx@lastprefix {\ifdefequal\cbx@lastprefix\cbx@secondtolastprefix {} {\printtext[labelprefix]{\cbx@lastprefix}}} {}% \printtext[labelnumber]{\cbx@lastnumber}}} {}% \setcounter{cbx@tempcnta}{0}} \makeatother

\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{filecontents}{biblatextest1.bib} @BOOK{BookA03, author = {Author Ät{"a}}, title = {Some Title}, publisher = {Some Publisher}, year = 2003, } @BOOK{BookB02, author = {Author Bb"{a}}, 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{LinkC01, author = {Author Ccc}, title = {Some Title}, year = 2004, url = {www.test1.com/bild.jpg}, } @MISC{LinkD04, 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}, } @MISC{LinkD09, author = {Author Iii}, title = {Some Title}, year = 2001, url = {www.test6.com/bild.jpg}, } \end{filecontents} \addbibresource{biblatextest1.bib} \addbibresource{biblatextest2.bib}

\begin{document} The first two citations \autocite{LinkD07,LinkC01} and \autocite{BookB10,BookB11,BookB09}.

Mixing both primary and secondary \autocite{LinkD08,LinkD04,LinkD05,BookB10,BookB11,BookB09}.

The others are \autocite{LinkC01,LinkD08,LinkD05,LinkD06,LinkD07} as well as \autocite{LinkC01,LinkD04,LinkD05,LinkD07,LinkD08, LinkD09} and \autocite{BookA03}.

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

\newrefcontext[labelprefix=tech\string\adddotspace] \printbibliography[title=References, keyword=secondary, resetnumbers] \end{document}

The first two citations [tech. 1, 5] and [Jjj02, Kkk02, Lll02].//Mixing both primary and secondary [tech. 2, 3, 6, Jjj02, Kkk02, Lll02].//The others are [tech. 1, 3–6] as well as [tech. 1–3, 5–7] and [Ätä03].

moewe
  • 175,683
  • If we change the author name Jjj to Aaa of the bibentry BookB09 in the file biblatextest1.bib, then Aaa02 is placed before tech. 2, 3, 6 without comma or space between them. I have tried to add \addcomma\space after. \usebibmacro{cite:alpha} Unfortunately, it places a comma also at the end of the alpha citations. It seems that a if-else statement is needed. – Ñako Aug 06 '20 at 22:09
  • 1
    @Ñako Have a look at the edit, please. – moewe Aug 07 '20 at 06:14
  • Thanks! Now it works as expected: [Aaa02, tech. 2,3,6, Kkk02, Lll02] . Hence, the commas are maybe slightly confusing. Is there a possibility to achieve this output: [Aaa02; tech. 2,3,6; Kkk02; Lll02], i.e., changing the commas with semicolons? Namely, the idea is to use semicolon globally: [Jjj02;Kkk02;Lll02] (the same output with,\renewcommand*{\multicitedelim}{\addsemicolon\space}) and comma in the numeric-comp cases: [tech. 1,5] or [tech. 1,3-6] or [tech. 1-3,5-7] – Ñako Aug 07 '20 at 16:16
  • 1
    @Ñako Try the edit again, please. – moewe Aug 07 '20 at 16:42
  • I´ve tried it firstly in the MWE and secondly in my document. The output is the desired one. However, it changes the output of \cites, too. Usually, I use it to cite sources and pages: \cites[page n°]{source_1}...[page n°]{source_n}. For example, \cites[2-15]{BookB09}[3,5,23]{BookB10} with Aaa the author name of BookB09 produces: [Aaa02; pp. 2–15, Kkk02, pp. 3, 5, 23]. A semicolon after the first author Aaa and then commas. What I want is a semicolon after the page numbers: [Aaa02, pp. 2–15; Kkk02, pp. 3, 5, 23]. Is there also a solution for this? Thanks in advance – Ñako Aug 07 '20 at 19:29
  • 1
    @Ñako Hrmm. That's going to be tricky. I think it would be easier with the upcoming changes to numeric-comp in biblatex 3.15 [not yet released] (which probably will require quite some changes to this answer anyway), but with the current biblatex it looks a bit ... involved. I'll see what I can do. But probably not today (my time). – moewe Aug 07 '20 at 19:47
  • 1
    @Ñako And again, please. As I said above, this is a bit tricky, so I can't guarantee that I haven't missed anything. For now I can only promise that things should get easier in biblatex 3.15. – moewe Aug 08 '20 at 16:01
  • Thanks for your support and time. Your new approach works as desired with \cite or \autocite but unfortunately not with \cites. For example: \cites[2--15]{BookB09}[3,5,23]{BookB10}[1--6]{BookB11} with Aaa the author name of BookB09 produces: [Aaa02; pp. 2–15; Kkk02; pp. 3, 5, 23; Lll02, pp. 1–6]. It places a non desired semicolon between author name and pages except in the last entry. Maybe I can open a new question with the \cites issue or just wait to biblatex 3.15. What do you think? – Ñako Aug 08 '20 at 21:21
  • 1
    @Ñako And again, please. – moewe Aug 09 '20 at 06:28
  • Great, now it works in the MWE as well as in my phd document. Thanks a lot! – Ñako Aug 09 '20 at 10:45
  • The linebreaks in a long citation with \cite of about 3 lines (multiple authors all in alpha style) overflow the text margins. The effect is considerable when replacing all \space with \addthinspace. It didn´t happen before rewriting of internal commands, i.e., with your current solution. \usepackage{breakcites} or \emergencystretch or \allowbreak or \sloppy don´t give the desired output. Is there also a solution for this? Thanks in advance. – Ñako Aug 18 '20 at 13:13
  • @Ñako Please ask a new question about that with an MWE that reproduces the issue. – moewe Aug 18 '20 at 14:04