typst / hayagriva

Rusty bibliography management.
Apache License 2.0
339 stars 54 forks source link

Handling of `shorthand` field in bibtex files #190

Open JKRhb opened 8 months ago

JKRhb commented 8 months ago

Description

Hi everyone,

In LaTeX, at least when using BibLaTeX, you can define a "shorthand" field in bibliography entries that can be used in alphanumeric bibliography styles as a custom citekey. A minimal example can be seen below:

image

LaTeX source code for the example ```tex \documentclass[preview]{standalone} \usepackage[backend=biber,style=alphabetic]{biblatex} \addbibresource{sample.bib} \begin{filecontents}[overwrite]{sample.bib} @techreport{RFC2324, series = {Request for Comments}, type = {RFC}, number = {2324}, doi = {10.17487/RFC2324}, author = {Larry M Masinter}, title = {{Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0)}}, year = {1998}, month = {4}, institution = {Internet Engineering Task Force}, shorthand = {RFC2324}, } @techreport{RFC2549, series = {Request for Comments}, type = {RFC}, number = {2549}, doi = {10.17487/2549}, author = {David Waitzman}, title = {{IP over Avian Carriers with Quality of Service}}, year = {1999}, month = {4}, institution = {Internet Engineering Task Force}, } \end{filecontents} \begin{document} The Hyper Text Coffee Pot Control Protocol is specified in \cite{RFC2324}. In the case of RFC 2549 \cite{RFC2549}, there is no shorthand defined. \printbibliography \end{document} ```

If I see it correctly, the shorthand field is currently being ignored in Typst when using an alphanumeric style.

Use Case

Especially for writing technical reports or papers referencing documents like RFCs, it could be very useful to define your own custom citekey, as a reference to [RFC2549] is clearer than one to [Wai99] in that context.

cpg314 commented 5 months ago

I think this can be done as follows (on https://github.com/typst/hayagriva):

diff --git a/src/csl/citation_label.rs b/src/csl/citation_label.rs
index f50808b..0f9c1b2 100644
--- a/src/csl/citation_label.rs
+++ b/src/csl/citation_label.rs
@@ -80,6 +80,9 @@ impl Alphanumerical {
     }

     pub fn citation(self, entry: &Entry) -> String {
+        if let Some(shorthand) = &entry.shorthand {
+            return shorthand.to_string();
+        }
         let full_entry = entry.get_full();

         let creators = self.creators(full_entry);
diff --git a/src/interop.rs b/src/interop.rs
index ffb4f17..fe2dcad 100644
--- a/src/interop.rs
+++ b/src/interop.rs
@@ -541,6 +541,10 @@ impl TryFrom<&tex::Entry> for Entry {
             item.set_annote(annote.into())
         }

+        if let Some(shorthand) = map_res(entry.shorthand())? {
+            item.set_shorthand(shorthand.into())
+        }
+
         if let Some(series) = map_res(entry.series())? {
             let title: FormatString = series.into();
             let mut new = Entry::new(&entry.key, item.entry_type);
diff --git a/src/lib.rs b/src/lib.rs
index ada473b..619d809 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -558,6 +558,8 @@ entry! {
     /// a PhD thesis; “NIH Publication” for an NIH technical report);
     /// Do not use for topical descriptions or categories (e.g. “adventure” for an adventure movie).
     "genre" => genre: FormatString,
+    /// Shorthand, overriding the computed label
+    "shorthand" => shorthand: FormatString,
 }

 impl Entry {

The question is whether the citation label should always be overridden when shorthand is present.

JKRhb commented 5 months ago

Thank you for the patch, @cpg314! Looks very promising :)

The question is whether the citation label should always be overridden when shorthand is present.

Maybe this could be configurable somewhere? (For example, via the bibliography function.)

laurmaedje commented 5 months ago

To me, this seems like something that the citation style should be in control of, i.e. it only happens for citation styles that would otherwise print the synthesized Author-Year shorthand.