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

1 Answer 1

3
+50

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].

12
  • 1
    @Ñako Have a look at the edit, please.
    – moewe
    Aug 7, 2020 at 6:14
  • 1
    @Ñako Try the edit again, please.
    – moewe
    Aug 7, 2020 at 16:42
  • 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 7, 2020 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 8, 2020 at 16:01
  • 1
    @Ñako And again, please.
    – moewe
    Aug 9, 2020 at 6:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .