[Tex/LaTex] biblatex: Reverse numbering (i.e., count down)

biblatexsorting

I was wondering if it is possible to get the citation numbers to count down

[3] newest item
[2] item
[1] oldest item

in biblatex? I am sorting using either using sorting=none and entering in reverse order or using sorting=ydnt and I would like the number of the newest (top) entry to be the highest and at the top of the list.

This is for the publications section of a CV.

If needed, a full example can be seen in my github repo for my CV, in the main.tex file.

Best Answer

The desired numbering scheme can be achieved by printing the labelnumber as the total number of entries in the current reference section, minus the actual label number, plus one.

This approach is demonstrated in the document below. It relies on commands from etoolbox and allows for an arbitrary number of reference sections.

\documentclass{article}
\usepackage[backend=bibtex,style=numeric,sorting=ydnt]{biblatex}

% Count total number of entries in each refsection
\AtDataInput{%
  \csnumgdef{entrycount:\therefsection}{%
    \csuse{entrycount:\therefsection}+1}}

% Print the labelnumber as the total number of entries in the
% current refsection, minus the actual labelnumber, plus one
\DeclareFieldFormat{labelnumber}{\mkbibdesc{#1}}    
\newrobustcmd*{\mkbibdesc}[1]{%
  \number\numexpr\csuse{entrycount:\therefsection}+1-#1\relax}

\addbibresource[label=biblatex]{biblatex-examples.bib}
\addbibresource[label=base]{xampl.bib}

\begin{document}
\begin{refsection}[biblatex]
  \nocite{markey,companion,ctan,knuth:ct}
  \printbibliography[title={biblatex examples}]
\end{refsection}
\begin{refsection}[base]
  \nocite{whole-set,inbook-minimal,book-minimal}
  \printbibliography[title={Base examples}]
\end{refsection}
\end{document}

enter image description here

If the document is saved as doc.tex, compile with:

latex doc
bibtex doc1-blx
bibtex doc2-blx
latex doc

With backend=biber, there is no need to run biber on each of the reference sections. You can also access bib files remotely from github.

Some caveats:

  • Shorthands. The labelnumber is overridden by the shorthand field. So with shorthands the maximum labelnumber in a given reference section doesn't correspond to the total number of cited entries. To handle this you can clear the shorthand field with biber by adding the following source map to the preamble:

    \DeclareSourcemap{
      \maps[datatype=bibtex]{
        \map{\step[fieldset=shorthand,null]}
      }
    }
    
  • Data-only entries. The code in \AtDataInput is executed for each entry in the bbl file. This includes related entries or other entries suppressed in the bibliography via skipbib or dataonly. To omit these from the entry total, you should condition on the skipbib setting.

    \makeatletter
    \def\ifskipbib{\iftoggle{blx@skipbib}}
    \makeatother
    
    \AtDataInput{\ifskipbib{}{%
      \csnumgdef{entrycount:\therefsection}{\csuse{entrycount:\therefsection}+1}}}
    
  • Multiple sorting schemes. If your document uses more than one sorting scheme in a reference section, the bbl file will contain duplicate entries - one for each scheme. To avoid counting duplicates, you can track the entries already counted in a list.

    \AtDataInput{%
      \xifinlistcs{\thefield{entrykey}}{entrylist:\therefsection}{}{%
        \listcsxadd{entrylist:\therefsection}{\thefield{entrykey}}%
        \csnumgdef{entrycount:\therefsection}{%
          \csuse{entrycount:\therefsection}+1}}}
    
  • Multiple versus single bib files. Using a single bib file for each reference section is convenient, but not necessary - as long as the entries accessed in each section correspond to those printed in the bibliography. For an alternative approach with a single reference section and labelnumber prefixes, refer to this post.