TransWikia.com

BibLaTeX custom citation string with custom hyperlink

TeX - LaTeX Asked by CPBL on December 8, 2020

I want a custom citation style in which the citation looks like a short name of the journal and year, and is hyperlinked to the DOI or journal web site, rather than to the reference list (bibliography).

One way to get close is by using style=alphabetic and specifying the shorthand value to be the citation format I want.

Below is a MWE that does not do what I want yet:

documentclass[english]{article}
usepackage[style=alphabetic,maxnames=999,minalphanames=3,firstinits=true]{biblatex}
usepackage{hyperref}
begin{filecontents*}{jobname.bib}
@book{tabmet,
  author    = {Ulrich Fischer and Max Heinzler and Roland Kilgus and Friedrich Näher and Stefan Oesterle and Heinz Paetzold and Werner Röhrer and Andreas Stephan and Ralf Winkow},
  title     = {Tabellenbuch Metall},
  edition   = {42},
  year      = {2002},
  publisher = {Verlag Europa-Lehrmittel},
  location  = {Haan-Gruiten},
  shorthand={Verlag 2002}
}
@article{fakeart,
  author    = {Walter Schnell and Dietmar Gross and Werner Hauger},
  title = {Technische Mechanik},
  volume    = {2},
  title     = {Elastostatik},
  edition   = {4},
  publisher = {Springer},
  date      = {2002},
  location  = {Berlin},
  journal={Journal of BibTeX examples},
  shorthand={J Bib Examp 2002}
}
end{filecontents*}
%shorthand={href{http://doi.org/10.1257/jep.23.1.221}{J Bib Examp 2002}}

DeclareLabelalphaTemplate{
  labelelement{
    field[final]{shorthand}
    field[strwidth=4,strside=left,ifnames=1]{labelname}
    field[strwidth=2,strside=left,ifnames=2]{labelname}
    field[strwidth=1,strside=left]{labelname}
  }
}
%            field{usera}
addbibresource{jobname.bib}
begin{document}
Citing these   cite{tabmet,fakeart}
  printbibliography
end{document}

This produces a custom (actually, individually-specified in the database, so not ideal) citation format, but it does not let me link outside.

That is, I would like to write

  shorthand={href{http://doi.org/10.1257/jep.23.1.221}{J Bib Examp 2002}}

in place of

  shorthand={J Bib Examp 2002}

But doing that gives errors.
What I am really after is to do this without using shorthand.
Can I define a style that uses the url field in the database and the, say, journal field in the database to make citations behave like

(href{url}{journal year})

One Answer

I think it is best to separate the shorthand and its link into different fields. Then you don't have to mess with href and avoid having to pass through unnecessary formatting commands.

If we don't want to reuse existing fields like url, doi etc., we need to define a new field that holds the link for the citation label. In the example below I called it shorthandlink. New fields need to be declared to biblatex in .dbx files (in the MWE below the .dbx file is generated with filecontents, in a real-world application one would just generate the file once and put it somewhere LaTeX can find it).

With shorthandfield available things are as easy as changing the field format responsible for linking citations to the bibliography.

documentclass[english]{article}

begin{filecontents*}{shorthandlink.dbx}
ProvidesFile{shorthandlink.dbx}[2020/11/15 arbitrary links for shorthands]
DeclareDatamodelFields[type=field, datatype=uri]{shorthandlink}
DeclareDatamodelEntryfields{shorthandlink}
end{filecontents*}

usepackage[style=alphabetic,
  maxnames=999, minalphanames=3, giveninits=true,
  datamodel=shorthandlink]{biblatex}

usepackage{hyperref}

DeclareFieldFormat{bibhyperref}{%
  iffieldundef{shorthandlink}
    {bibhyperref{#1}}
    {ifhyperref
       {href{thefield{shorthandlink}}{#1}}
       {#1}}}

begin{filecontents*}{jobname.bib}
@book{tabmet,
  author        = {Ulrich Fischer and Max Heinzler and Roland Kilgus
                   and Friedrich Näher and Stefan Oesterle
                   and Heinz Paetzold and Werner Röhrer
                   and Andreas Stephan and Ralf Winkow},
  title         = {Tabellenbuch Metall},
  edition       = {42},
  year          = {2002},
  publisher     = {Verlag Europa-Lehrmittel},
  location      = {Haan-Gruiten},
  shorthand     = {Verlag 2002},
  shorthandlink = {https://example.com/verlag/2002},
}
@article{fakeart,
  author        = {Walter Schnell and Dietmar Gross and Werner Hauger},
  title         = {Technische Mechanik},
  volume        = {2},
  title         = {Elastostatik},
  edition       = {4},
  publisher     = {Springer},
  date          = {2002},
  location      = {Berlin},
  journal       = {Journal of BibTeX examples},
  shorthand     = {J Bib Examp 2002},
  shorthandlink = {https://doi.org/10.1257/jep.23.1.221},
}
end{filecontents*}
addbibresource{jobname.bib}

begin{document}
  Citing thesecite{tabmet,fakeart}
  printbibliography
end{document}

Linked shorthand citation.


If you want to reuse the existing doi and url fields, you don't need the .dbx file, but the bibhperref field format needs to be slightly more complex.

documentclass[english]{article}

usepackage[style=alphabetic,
  maxnames=999, minalphanames=3, giveninits=true]{biblatex}

usepackage{hyperref}

DeclareFieldFormat{bibhyperref}{%
  iffieldundef{doi}
    {iffieldundef{url}
       {bibhyperref{#1}}
       {ifhyperref
          {href{thefield{url}}{#1}}
          {#1}}}
    {ifhyperref
       {href{https://doi.org/thefield{doi}}{#1}}
       {#1}}}

begin{filecontents*}{jobname.bib}
@book{tabmet,
  author        = {Ulrich Fischer and Max Heinzler and Roland Kilgus
                   and Friedrich Näher and Stefan Oesterle
                   and Heinz Paetzold and Werner Röhrer
                   and Andreas Stephan and Ralf Winkow},
  title         = {Tabellenbuch Metall},
  edition       = {42},
  year          = {2002},
  publisher     = {Verlag Europa-Lehrmittel},
  location      = {Haan-Gruiten},
  shorthand     = {Verlag 2002},
  url           = {https://example.com/verlag/2002},
}
@article{fakeart,
  author        = {Walter Schnell and Dietmar Gross and Werner Hauger},
  title         = {Technische Mechanik},
  volume        = {2},
  title         = {Elastostatik},
  edition       = {4},
  publisher     = {Springer},
  date          = {2002},
  location      = {Berlin},
  journal       = {Journal of BibTeX examples},
  shorthand     = {J Bib Examp 2002},
  doi           = {10.1257/jep.23.1.221},
}
end{filecontents*}
addbibresource{jobname.bib}

begin{document}
  Citing thesecite{tabmet,fakeart}
  printbibliography
end{document}

Correct answer by moewe on December 8, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP