plk / biber

Backend processor for BibLaTeX
Artistic License 2.0
335 stars 37 forks source link

sortkey is ignored #208

Closed moewew closed 6 years ago

moewew commented 6 years ago

Consider the following MWE

\documentclass{article}
\usepackage{filecontents}
\usepackage[backend=biber, style=authoryear, minxrefs=1]{biblatex} 

\begin{filecontents}{\jobname.bib}
@collection{pauly,
  editor     = {August Pauly and Georg Wissowa},
  title      = {Pauly’s Real-Encyclopaedie der classischen Altertumswissenschaft},
  date       = {1894/1980},
  location   = {Stuttgart},
  shorthand  = {RE},
  shorttitle = {RE},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\addbibresource{biblatex-examples.bib}

\DeclareSourcemap{
  \maps{
    \map{
      \step[fieldsource=shorthand, final]
      \step[fieldsource=entrykey, match=\regexp{(.*)}]
      \step[fieldset=xref, fieldvalue=shorthand-of-$1]
      \step[entrynew=shorthand-of-$1, entrynewtype=book]
      \step[fieldsource=shorthand]
      \step[fieldset=sortkey, origfieldval, entrytarget=shorthand-of-$1]
      \step[fieldset=note, origfieldval, entrytarget=shorthand-of-$1]
    }
  }
}

\begin{document}
  \nocite{pauly,sigfridsson}
  \printbibliography
\end{document}

Produces

(N.d.). RE.

Pauly, August and Georg Wissowa, eds. (1894–1980). Pauly’s Real-Encyclopaedie der classischen Altertumswissenschaft. Stuttgart.

Sigfridsson, Emma and Ulf Ryde (1998). “Comparison of methods for deriving atomic charges from the electrostatic potential and moments”. In: Journal of Computational Chemistry 19.4, pp. 377–395.

For me. But since the sortkey field of the new entry is set to the shorthand, i.e. 'RE'. I would expect the entries to sort as

Pauly, August and Georg Wissowa, eds. (1894–1980). Pauly’s Real-Encyclopaedie der classischen Altertumswissenschaft. Stuttgart.

(N.d.). RE.

Sigfridsson, Emma and Ulf Ryde (1998). “Comparison of methods for deriving atomic charges from the electrostatic potential and moments”. In: Journal of Computational Chemistry 19.4, pp. 377–395.

In fact the .bbl says that the sort initial of the new entry is 'R'.

A work-around is to use sortname instead of sortkey.

Tested with 3.10/2.10.

moewew commented 6 years ago

OK. It is much simpler than that.

Consider

\documentclass{article}
\usepackage{filecontents}
\usepackage[backend=biber, style=authoryear, minxrefs=1]{biblatex} 

\begin{filecontents}{\jobname.bib}
@collection{pauly,
  note = {foo},
  sortkey = {zzzz},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\addbibresource{biblatex-examples.bib}

\begin{document}
  \nocite{pauly,sigfridsson}
  \printbibliography
\end{document}

Due to the sortkey I would expect pauly to sort last. But it sorts first. A look at --trace shows

sortdata => {
 pauly => ["mm,,,,,zzzz", ["mm", "", "", "", "", "zzzz"]],
 sigfridsson => [
   "mm,,Sigfridsson!Emma#Ryde!Ulf,1998,Comparison of methods for deriving atomic charges from the electrostatic potential and moments,19",
   [
     "mm",
     "",
     "Sigfridsson!Emma#Ryde!Ulf",
     1998,
     "Comparison of methods for deriving atomic charges from the electrostatic potential and moments",
     19,
   ],
 ],
},

While I would have expected something like

pauly => ["mm,,zzzz,zzzz,zzzz,zzzz", ["mm", "", "zzzz", "zzzz", "zzzz", "zzzz"]],

So sortkey gets in too late if the entry has no fields that would determine sorting.

plk commented 6 years ago

Interesting edge case. Should be fixed in 3.11 DEV now.

moewew commented 6 years ago

Works brilliantly now. Thank you.

To shoehorn in a feature request here: Would it be possible to use a sourcemap function to add an entry to the .bbl/'cited' references, i.e. to \nocite an entry via sourcemaps? In the MWE above it was necessary to add shorthand-of-$1 to the xref of its parent and set minxrefs=1 to get it to appear in the .bbl. Could there just be a flag to set in the sourcemap to automatically write that entry to the .bbl?

plk commented 6 years ago

Hmm, is this possible with an auto-created related entry instead as that will automatically add it as a dataonly element which is sort of what this is designed for.

moewew commented 6 years ago

Wouldn't that mean that the parent entry can't have any other related elements though?

The thing is that with sourcemaps we can create new entries, but we can't really force them to be written out to the .bbl. And sometimes it is unreasonable to use an xref, crossref or related feature to tie two entries together - and it might also not be possible to \nocite the entry from the document (not sure if that even works with sourcemap-generated entries). Since Biber already processes these entries it seems at least possible to tell Biber to also write them to the .bbl file. (Of course it would also be nice to do the same things for all entries in a .bib file, but I can imagine that that might be even harder.)

plk commented 6 years ago

Alright, I see your point - please try 3.11/2.11 dev versions now.

If all keys are already included (due to \nocite{*} in the doc), this is automatic (and always was). There is now entrynocite which applies to entrynew and entryclone steps. This allows you to manually \nocite any created key:

\DeclareSourcemap{
  \maps{
    \map{
      \step[fieldsource=shorthand, final]
      \step[fieldsource=entrykey, match=\regexp{(.*)}]
      \step[entrynew=shorthand-of-$1, entrynewtype=book, entrynocite]
      \step[fieldsource=shorthand]
      \step[fieldset=sortkey, origfieldval, entrytarget=shorthand-of-$1]
      \step[fieldset=note, origfieldval, entrytarget=shorthand-of-$1]
    }
  }
}
moewew commented 6 years ago

Works just as expected. Thank you very much.