[Tex/LaTex] Including numeric moderntimeline list of publications in moderncv

biblatexmoderncvmoderntimeline

I am writing my CV with moderncv and the moderntimeline package.
I would like to have the publications list, numbered in reverse order and ordered from most recent to oldest, to show both the publication label number and the moderntimeline picture in the moderncv left column. Francois Mouton has provided a first integration of moderntimeline with bibliography, (and I've started from there), but his code does not work for numeric bibliography styles.
Maybe the number plus timeline is ugly but I have been strongly advised to do so, therefore I have to comply.

My attempt has been to redefine the bibliography environment to include both numeric label and timeline in the left column and to define a new command, \tldatecventryV, that creates a timeline of length \hintscolumnwidthV (based on the template length \hintscolumnwidth) and has no other parameters than year and colour (so that it just creates the timeline picture).
To redefine the bibliography indentation, I have used the answers Left-margin alignment of biblatex's bibliography list and Move biblatex label into margin and randomly changed stuff that I don't really understand.

The reverse numbering comes from biblatex: Reverse numbering (i.e., count down).

\documentclass[sans]{moderncv} 
\moderncvstyle{classic}
\moderncvcolor{orange} 
\usepackage[scale=0.75]{geometry}
\usepackage{moderntimeline}
\tlmaxdates{1888}{1929}     
\usepackage[backend = biber,
            style   = nature, 
            sorting = none 
            ]{biblatex}                                    
\usepackage{filecontents}    
\begin{filecontents*}{\jobname.bib}
    @Article{Jones1929,
      author       = {Jones, Grinnell and Dole, Malcolm},
      title        = {THE VISCOSITY OF AQUEOUS SOLUTIONS OF STRONG ELECTROLYTES},
      journaltitle = {J. Am. Chem. Soc.},
      date         = {1929},
      volume       = {51},
      number       = {10},
      pages        = {2950--2964},
      doi          = {10.1021/ja01385a012}
    }   
    @Article{Drude1894,
      author       = {Drude, P. and Nernst, W.},
      title        = {{\"U}ber Elektrostriktion durch freie Ionen},
      journaltitle = {Z. Phys. Chem.},
      date         = {1894},
      volume       = {15},
      pages        = {79--85},
    }   
    @Article{Hofmeister1888,
      author       = {Hofmeister, Franz},
      title        = {Zur Lehre von der Wirkung der Salze},
      journaltitle = {Arch. Exp. Pathol. Pharmakol.},
      date         = {1888},
      volume       = {25},
      number       = {1},
      pages        = {1--30},
      doi          = {10.1007/BF01838161},
    }
\end{filecontents*}    
\addbibresource{\jobname.bib} 
%-----------------------------------------------------------
\newlength{\hintscolumnwidthV}
\setlength{\hintscolumnwidthV}{.7\hintscolumnwidth} 

\makeatletter 
\newcommand*{\cventryV}[1][.25em]{}
% for numeric bibliography
\newcommand{\tldatecventryV}[2][color1]{%
\issincefalse
\tl@formatstartyear{#2}
\cventryV{\tikz[baseline=0pt]{
    \useasboundingbox (2ex,0ex) rectangle (\hintscolumnwidthV,1ex);
    \fill [\tl@runningcolor] (0,0)
       rectangle (\hintscolumnwidthV,\tl@runningwidth);
    \fill [#1] (0,0)
       ++(\tl@startfraction*\hintscolumnwidthV,0pt)
       node [tl@singleyear] {#2}
       node {$\bullet$};
  }}}
\makeatother

% 'numeric' bibliography environment (from numeric.bbx)
\defbibenvironment{bibliography}
  {\list
   {\printtext[labelnumberwidth]{%
      \printfield{prefixnumber}%
      \printfield{labelnumber}}
    \tldatecventryV{%
      \thefield{year} % actual year from bibitem
       }}
     {\setlength{\topsep}{0pt}% layout parameters based on moderncvstyleclassic.sty
      \setlength{\labelwidth}{\hintscolumnwidth}%
      \setlength{\labelsep}{\separatorcolumnwidth}%
      \setlength{\itemsep}{\bibitemsep}%
      \leftmargin\labelwidth%
       \addtolength{\leftmargin}{-.35\labelsep}% <-- changed
      \advance\leftmargin\labelsep
      }%
      \sloppy\clubpenalty4000\widowpenalty4000}
  {\endlist}
  {\item}
%-----------------------
\AtEveryBibitem{\clearfield{year}} 
%-------------------------------------------------------------------------------
%  reverse numbering of publications
%-------------------------------------------------------------------------------
% 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}   
%------------------- Personal Data for CV header -------------------------------
\name{Joan}{Doe}
%-------------------------------------------------------------------------------
\begin{document}

\makecvtitle    
\nocite{*}                       
\printbibliography[type=article,title={Journal Publications}]

\end{document}

It produces this:

enter image description here
My first question is whether the various lengths can be defined in a way that makes more sense than my random fiddling. Maybe so that it can automatically adjust to other numeric bibliography formats?

My second problem is that I would also like to maintain the possibility to switch to a non-numeric bibliography format quickly (and without using Mouton's solution, which has some indentation problmes). I've put together something that works with authoryear:

\documentclass[sans]{moderncv} 
\moderncvstyle{classic}
\moderncvcolor{orange} 
\usepackage[scale=0.75]{geometry}
\usepackage{moderntimeline}
\tlmaxdates{1888}{1929}     
\usepackage[backend = biber,
            style   = authoryear,
            sorting = none  % ydnt
            ]{biblatex}                                    
\usepackage{filecontents}    
\begin{filecontents*}{\jobname.bib}
    @Article{Jones1929,
      author       = {Jones, Grinnell and Dole, Malcolm},
      title        = {THE VISCOSITY OF AQUEOUS SOLUTIONS OF STRONG ELECTROLYTES},
      journaltitle = {J. Am. Chem. Soc.},
      date         = {1929},
      volume       = {51},
      number       = {10},
      pages        = {2950--2964},
      doi          = {10.1021/ja01385a012}
    }   
    @Article{Drude1894,
      author       = {Drude, P. and Nernst, W.},
      title        = {{\"U}ber Elektrostriktion durch freie Ionen},
      journaltitle = {Z. Phys. Chem.},
      date         = {1894},
      volume       = {15},
      pages        = {79--85},
    }   
    @Article{Hofmeister1888,
      author       = {Hofmeister, Franz},
      title        = {Zur Lehre von der Wirkung der Salze},
      journaltitle = {Arch. Exp. Pathol. Pharmakol.},
      date         = {1888},
      volume       = {25},
      number       = {1},
      pages        = {1--30},
      doi          = {10.1007/BF01838161},
    }
\end{filecontents*}    
\addbibresource{\jobname.bib} 
%-----------------------------------------------------------
\newlength{\hintscolumnwidthV}
\setlength{\hintscolumnwidthV}{.7\hintscolumnwidth} 

\makeatletter 
\newcommand*{\cventryV}[1][.25em]{}
% for general bibliography
\newcommand{\tldatecventryVn}[2][color1]{%
\issincefalse
\tl@formatstartyear{#2}
\cventryV{\tikz[baseline=0pt]{
    \useasboundingbox (10ex,0) rectangle (\hintscolumnwidth,1ex);
    \fill [\tl@runningcolor] (0,0)
       rectangle (\hintscolumnwidth,\tl@runningwidth);%%V
    \fill [#1] (0,0)
       ++(\tl@startfraction*\hintscolumnwidth,0pt)
       node [tl@singleyear] {#2}
       node {$\bullet$};
  }}}
\makeatother

\defbibenvironment{bibliography} 
  {\list
     {\tldatecventryVn{%
      \thefield{year} % actual year from bibitem
       }}%
     {\leftmargin\bibhang 
      \itemindent\leftmargin 
      \setlength{\labelwidth}{\hintscolumnwidth}
      \setlength{\labelsep}{\separatorcolumnwidth}%
      \itemsep\bibitemsep
       \addtolength{\leftmargin}{6.6\labelsep}
       \advance\itemindent-\labelsep
      \parsep\bibparsep}}
  {\endlist}
  {\item}
%-----------------------
\AtEveryBibitem{\clearfield{year}} 
%-------------------------------------------------------------------------------
%  reverse numbering of publications
%-------------------------------------------------------------------------------
% 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}   
%------------------- Personal Data for CV header -------------------------------
\name{Joan}{Doe}
%-------------------------------------------------------------------------------
\begin{document}

\makecvtitle    
\nocite{*}                       
\printbibliography[type=article,title={Journal Publications}]

\end{document}

And the output :
enter image description here

But, in addition to the problem of defining the lengths in a better way, I have no idea how to switch automatically between the two options – right now I have both in the same file and I comment/uncomment the numeric bibliography environment definition. Maybe there is a simpler way of doing all this too.


Update — 15 November

Thanks to @Astrinus suggestions, I got to this:

\documentclass[sans]{moderncv} 
\moderncvstyle{classic}
\moderncvcolor{orange} 
\usepackage[scale=0.75]{geometry}
\usepackage{moderntimeline}
\tlmaxdates{1886}{1931} 
\usepackage{filecontents}    
\begin{filecontents*}{\jobname.bib}
    @Article{Jones1929,
      author       = {Jones, Grinnell and Dole, Malcolm},
      title        = {THE VISCOSITY OF AQUEOUS SOLUTIONS OF STRONG ELECTROLYTES},
      journaltitle = {J. Am. Chem. Soc.},
      date         = {1929},
      volume       = {51},
      number       = {10},
      pages        = {2950--2964},
      doi          = {10.1021/ja01385a012}
    }   
    @Article{Drude1894,
      author       = {Drude, P. and Nernst, W.},
      title        = {{\"U}ber Elektrostriktion durch freie Ionen},
      journaltitle = {Z. Phys. Chem.},
      date         = {1894},
      volume       = {15},
      pages        = {79--85}
    }   
    @Article{Hofmeister1888,
      author       = {Hofmeister, Franz},
      title        = {Zur Lehre von der Wirkung der Salze},
      journaltitle = {Arch. Exp. Pathol. Pharmakol.},
      date         = {1888},
      volume       = {25},
      number       = {1},
      pages        = {1--30},
      doi          = {10.1007/BF01838161}
    }
    @Article{Jones19292,
      author       = {Jones, Grinnell and Dole, Malcolm},
      title        = {THE VISCOSITY OF AQUEOUS SOLUTIONS OF STRONG ELECTROLYTES},
      journaltitle = {J. Am. Chem. Soc.},
      date         = {1929},
      volume       = {51},
      number       = {10},
      pages        = {2950--2964},
      doi          = {10.1021/ja01385a012}
    }   
    @Article{Drude18942,
      author       = {Drude, P. and Nernst, W.},
      title        = {{\"U}ber Elektrostriktion durch freie Ionen},
      journaltitle = {Z. Phys. Chem.},
      date         = {1894},
      volume       = {15},
      pages        = {79--85}
    }   
    @Article{Hofmeister18882,
      author       = {Hofmeister, Franz},
      title        = {Zur Lehre von der Wirkung der Salze},
      journaltitle = {Arch. Exp. Pathol. Pharmakol.},
      date         = {1888},
      volume       = {25},
      number       = {1},
      pages        = {1--30},
      doi          = {10.1007/BF01838161}
    }
    @Article{Jones19293,
      author       = {Jones, Grinnell and Dole, Malcolm},
      title        = {THE VISCOSITY OF AQUEOUS SOLUTIONS OF STRONG ELECTROLYTES},
      journaltitle = {J. Am. Chem. Soc.},
      date         = {1929},
      volume       = {51},
      number       = {10},
      pages        = {2950--2964},
      doi          = {10.1021/ja01385a012}
    }   
    @Article{Drude18943,
      author       = {Drude, P. and Nernst, W.},
      title        = {{\"U}ber Elektrostriktion durch freie Ionen},
      journaltitle = {Z. Phys. Chem.},
      date         = {1894},
      volume       = {15},
      pages        = {79--85}
    }   
    @Article{Hofmeister18883,
      author       = {Hofmeister, Franz},
      title        = {Zur Lehre von der Wirkung der Salze},
      journaltitle = {Arch. Exp. Pathol. Pharmakol.},
      date         = {1888},
      volume       = {25},
      number       = {1},
      pages        = {1--30},
      doi          = {10.1007/BF01838161}
    }
    @Article{Jones19294,
      author       = {Jones, Grinnell and Dole, Malcolm},
      title        = {THE VISCOSITY OF AQUEOUS SOLUTIONS OF STRONG ELECTROLYTES},
      journaltitle = {J. Am. Chem. Soc.},
      date         = {1929},
      volume       = {51},
      number       = {10},
      pages        = {2950--2964},
      doi          = {10.1021/ja01385a012}
    }   
    @Article{Drude18944,
      author       = {Drude, P. and Nernst, W.},
      title        = {{\"U}ber Elektrostriktion durch freie Ionen},
      journaltitle = {Z. Phys. Chem.},
      date         = {1894},
      volume       = {15},
      pages        = {79--85}
    }   
    @Article{Hofmeister18884,
      author       = {Hofmeister, Franz},
      title        = {Zur Lehre von der Wirkung der Salze},
      journaltitle = {Arch. Exp. Pathol. Pharmakol.},
      date         = {1888},
      volume       = {25},
      number       = {1},
      pages        = {1--30},
      doi          = {10.1007/BF01838161}
    }
\end{filecontents*}    
%-----------------------------------------------------------
\newif\ifnumericCVbibliography
\numericCVbibliographytrue % replace true with false to disable
\newlength{\hintscolumnwidthV}
\setlength\hintscolumnwidthV{\hintscolumnwidth}
\newlength{\labelkern}
\ifnumericCVbibliography
  \setlength\labelkern{-2ex}
  \usepackage[backend = biber,
              style   = nature, 
              sorting = none 
             ]{biblatex}  
  \newcommand{\printbibnumber}{%\hspace* here has no effect so I removed it
      \llap{\printtext[labelnumberwidth]{%
              \printfield{labelprefix}%
              \printfield{labelnumber}
              }\kern\labelkern% to reduce space between label and timeline image
            }%
  }                                  
\else
  \usepackage[backend = biber,
              style   = authoryear, 
              sorting = none 
             ]{biblatex}  
  \newcommand{\printbibnumber}{\relax} % do nothing   
  \setlength\labelkern{0ex} 
  \setlength\hintscolumnwidthV{\hintscolumnwidth} % this length needs 
  % to be re-set or it keeps the numeric  version value if that has been run before   
\fi
\addbibresource{\jobname.bib} 
%-----------------------------------------------------------
\makeatletter 
\newcommand*{\cventryV}[1][.25em]{}
% timeline for bibliography
\newcommand{\tldatecventryV}[2][color1]{%
\issincefalse
\tl@formatstartyear{#2}
\cventryV{\tikz[baseline=0pt]{
    \useasboundingbox (0ex,0ex) rectangle (\hintscolumnwidthV,1ex); 
    %changed origin of boundingbox. previous was (2ex,0ex) but this 
    % creates alignment problems when switching between numeric and non numeric.
    \fill [\tl@runningcolor] (0,0)
       rectangle (\hintscolumnwidthV,\tl@runningwidth);
    \fill [#1] (0,0)
       ++(\tl@startfraction*\hintscolumnwidthV,0pt)
       node [tl@singleyear] {#2}
       node {$\bullet$};
  }}}
\makeatother
%-----------------------------------
\defbibenvironment{bibliography}
  {\list
   {\printbibnumber% here you see the macro in action
    \tldatecventryV{%
      \thefield{year} % actual year from bibitem
       }}
       {%     
        \setlength{\topsep}{0pt}% layout parameters based on moderncvstyleclassic.sty      
        \addtolength\hintscolumnwidthV{-\labelnumberwidth}% num - changes timeline image length
        \addtolength\hintscolumnwidthV{\labelkern}% num  - changes timeline image length
        \setlength{\bibhang}{\hintscolumnwidthV} % custom bibhang
        \setlength{\labelsep}{\separatorcolumnwidth} %  horizontal distance between label and entry
        \setlength{\leftmargin}{\hintscolumnwidth} % sets where the left margin is 
        \addtolength{\leftmargin}{\separatorcolumnwidth} %
        \setlength{\itemindent}{-\bibhang} % this sets indentation of the second line of the entry. 
        % changing it moves also the first line left or right
        \addtolength{\itemindent}{-\separatorcolumnwidth} % to align the second line exactly
        \setlength{\itemsep}{\bibitemsep} % vertical distance between bib items
        \setlength{\parsep}{\bibparsep}}}
  {\endlist}
  {\item}    
%-----------------------
\AtEveryBibitem{\clearfield{year}} 
%-------------------------------------------------------------------------------
%  reverse numbering of publications
%-------------------------------------------------------------------------------
% 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}   
%------------------- Personal Data for CV header -------------------------------
\name{Joan}{Doe}
%-------------------------------------------------------------------------------
\begin{document}
\makecvtitle    
\nocite{*}                       
\printbibliography[type=article,title={Journal Publications}]
\end{document}

With respect to @Astrinus answer, I adjusted spacing and indentation of bibitems, trying to make them consistent between numeric and non-numeric bibliography styles. (and changed \def with \newcommand – I already edited Astrinus' answer with those). I am not sure whether it's the best way of defining lengths, neither if it's a general way of adapting the template (i.e. when changing fonts) and I am happy to receive suggestions and comments about the definition of lengths in the bibliography.

These are the results with this MWE for numeric (\numericCVbibliographytrue) and non-numeric (\numericCVbibliographyfalse) style:

enter image description here

enter image description here

Best Answer

You could write something like that

\newif\ifnumericCVbibliography
\numericCVbibliographytrue % replace true with false to disable
\newlength{\hintscolumnwidthV}
\setlength\hintscolumnwidthV{\hintscolumnwidth}
\ifnumericCVbibliography
  \usepackage[backend = biber,
              style   = nature, 
              sorting = none 
             ]{biblatex}                                    
   \def\printbibnumber{\hspace*{1.5em}%
      \llap{\printtext[labelnumberwidth]{%
              \printfield{prefixnumber}%
              \printfield{labelnumber}%
             }%
           }%
   }
  \addtolength\hintscolumnwidthV{-1.75em}
\else
  \usepackage[backend = biber,
              style   = authoryear, 
              sorting = none 
             ]{biblatex}                                    
  \def\printbibnumber\relax
\fi

and then use the \printnumber macro before your \tldatecventryV.

The \hspace{1.5em} makes room for two-digit numbers.

Complete (working, I hope) example:

\documentclass[sans]{moderncv} 
\moderncvstyle{classic}
\moderncvcolor{orange} 
\usepackage[scale=0.75]{geometry}
\usepackage{moderntimeline}
\tlmaxdates{1888}{1929}     
\usepackage{filecontents}    
\begin{filecontents*}{\jobname.bib}
    @Article{Jones1929,
      author       = {Jones, Grinnell and Dole, Malcolm},
      title        = {THE VISCOSITY OF AQUEOUS SOLUTIONS OF STRONG ELECTROLYTES},
      journaltitle = {J. Am. Chem. Soc.},
      date         = {1929},
      volume       = {51},
      number       = {10},
      pages        = {2950--2964},
      doi          = {10.1021/ja01385a012}
    }   
    @Article{Drude1894,
      author       = {Drude, P. and Nernst, W.},
      title        = {{\"U}ber Elektrostriktion durch freie Ionen},
      journaltitle = {Z. Phys. Chem.},
      date         = {1894},
      volume       = {15},
      pages        = {79--85},
    }   
    @Article{Hofmeister1888,
      author       = {Hofmeister, Franz},
      title        = {Zur Lehre von der Wirkung der Salze},
      journaltitle = {Arch. Exp. Pathol. Pharmakol.},
      date         = {1888},
      volume       = {25},
      number       = {1},
      pages        = {1--30},
      doi          = {10.1007/BF01838161},
    }
\end{filecontents*}    
%-----------------------------------------------------------
\newif\ifnumericCVbibliography
\numericCVbibliographytrue % replace true with false to disable
\newlength{\hintscolumnwidthV}
\setlength\hintscolumnwidthV{\hintscolumnwidth}
\ifnumericCVbibliography
  \usepackage[backend = biber,
              style   = nature, 
              sorting = none 
             ]{biblatex}                                    
  \def\printbibnumber{\hspace*{1.5em}%
      \llap{\printtext[labelnumberwidth]{%
              \printfield{prefixnumber}%
              \printfield{labelnumber}%
             }%
           }%
   }
  \addtolength\hintscolumnwidthV{-1.75em}
\else
  \usepackage[backend = biber,
              style   = authoryear, 
              sorting = none 
             ]{biblatex}                                    
  \def\printbibnumber\relax % do nothing
\fi
\addbibresource{\jobname.bib} 
%-----------------------------------------------------------
\makeatletter 
\newcommand*{\cventryV}[1][.25em]{}
% for numeric bibliography
\newcommand{\tldatecventryV}[2][color1]{%
\issincefalse
\tl@formatstartyear{#2}
\cventryV{\tikz[baseline=0pt]{
    \useasboundingbox (2ex,0ex) rectangle (\hintscolumnwidthV,1ex);
    \fill [\tl@runningcolor] (0,0)
       rectangle (\hintscolumnwidthV,\tl@runningwidth);
    \fill [#1] (0,0)
       ++(\tl@startfraction*\hintscolumnwidthV,0pt)
       node [tl@singleyear] {#2}
       node {$\bullet$};
  }}}
\makeatother

% 'numeric' bibliography environment (from numeric.bbx)
\defbibenvironment{bibliography}
  {\list
   {\printbibnumber % here you see the macro in action
    \tldatecventryV{%
      \thefield{year} % actual year from bibitem
       }}
     {\setlength{\topsep}{0pt}% layout parameters based on moderncvstyleclassic.sty
      \setlength{\labelwidth}{\hintscolumnwidth}%
      \setlength{\labelsep}{\separatorcolumnwidth}%
      \setlength{\itemsep}{\bibitemsep}%
      \leftmargin\labelwidth%
       \addtolength{\leftmargin}{-.35\labelsep}% <-- changed
      \advance\leftmargin\labelsep
      }%
      \sloppy\clubpenalty4000\widowpenalty4000}
  {\endlist}
  {\item}
%-----------------------
\AtEveryBibitem{\clearfield{year}} 
%-------------------------------------------------------------------------------
%  reverse numbering of publications
%-------------------------------------------------------------------------------
% 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}   
%------------------- Personal Data for CV header -------------------------------
\name{Joan}{Doe}
%-------------------------------------------------------------------------------
\begin{document}

\makecvtitle    
\nocite{*}                       
\printbibliography[type=article,title={Journal Publications}]

\end{document}
Related Question