[Tex/LaTex] Reverse Numbering with Biblatex and Moderncv

biblatexmoderncv

This question is a follow-up to an earlier question. I'm using ModernCV with Biblatex to make my CV. I've used the solution given to create a list of publications, sorted by type and by year.

My question is how I can reverse the numbering of the publications (highest first) without changing anything else. Solutions exist for how to do this from multiple .bib files and I may consider that option if this is impossible, but an option where I could continue using a single bib file would be ideal.

Best Answer

If you just wish to reverse the numbering - not the order - then you can use the following approach:

enter image description here

\documentclass{moderncv}
\usepackage{biblatex,refcount}

\makeatletter
% https://tex.stackexchange.com/q/66829/5764
\newcounter{numbibentries}
\renewbibmacro*{finentry}{\stepcounter{numbibentries}\finentry}
% https://tex.stackexchange.com/q/123805/5764
\defbibenvironment{bibliography}
  {\list
     {\printtext[labelnumberwidth]{% label format from numeric.bbx
        \printfield{prefixnumber}%
        \number\numexpr\getrefnumber{num-bib-entries}-\abx@field@labelnumber+1\relax}}
     {\setlength{\topsep}{0pt}% layout parameters from moderncvstyleclassic.sty
      \setlength{\labelwidth}{\hintscolumnwidth}%
      \setlength{\labelsep}{\separatorcolumnwidth}%
      \leftmargin\labelwidth%
      \advance\leftmargin\labelsep}%
      \sloppy\clubpenalty4000\widowpenalty4000}
  {\endlist}
  {\item}
\AtEndDocument{% Add reference at end of document to remember number of bib-entries.
  \edef\@currentlabel{\thenumbibentries}\label{num-bib-entries}}
\makeatother

\moderncvstyle{classic}
\moderncvcolor{blue}
\firstname{John}
\familyname{Doe}

\addbibresource{biblatex-examples.bib}
\begin{document}
\makecvtitle
\nocite{companion,knuth:ct:a,knuth:ct:b}
\printbibliography[title={Publications}]
\end{document}

The idea is to count the number of references by tapping into each "\bibitem". Once all is counted, we insert a \label at the end of the document (using \AtEndDocument). This label is retrieved and used in a calculation (thanks to the expandable \getrefnumber from refcount) to reverse the numbering.


For multiple bibliographies per document, you could use \citesinthissection{<num>} as defined below:

\documentclass{moderncv}
\usepackage[defernumbers=true]{biblatex}

\makeatletter
\newcommand{\citesinthissection}[1]{\xdef\@totalcites{#1}}
% https://tex.stackexchange.com/q/123805/5764
\defbibenvironment{bibliography}
  {\list
     {\printtext[labelnumberwidth]{% label format from numeric.bbx
        \printfield{prefixnumber}%
        \number\numexpr\@totalcites-\abx@field@labelnumber+1\relax}}
     {\setlength{\topsep}{0pt}% layout parameters from moderncvstyleclassic.sty
      \setlength{\labelwidth}{\hintscolumnwidth}%
      \setlength{\labelsep}{\separatorcolumnwidth}%
      \leftmargin\labelwidth%
      \advance\leftmargin\labelsep}%
      \sloppy\clubpenalty4000\widowpenalty4000}
  {\endlist}
  {\item}
\makeatother

\moderncvstyle{classic}
\moderncvcolor{blue}
\firstname{John}
\familyname{Doe}

\addbibresource{biblatex-examples.bib}
\begin{document}
\makecvtitle
\nocite{companion,knuth:ct:a,knuth:ct:b}
\citesinthissection{3}% There are 3 cites in this section
\printbibliography[notkeyword=primary,title={Publications}]
\nocite{aristotle:anima,aristotle:physics}
\citesinthissection{2}% There are 2 cites in this section
\printbibliography[keyword=primary,resetnumbers=true,title={Other Publications}]
\end{document}

Remember to update the count of items whenever you add elements to your bibliography. Use the defernumbers=true global option and resetnumbers=true option from the second \printbibliography onwards. You may need to use options like keyword/notkeyword options in order to print exactly only what you want in each bibliography part.