1

I am using the unsrturl bibliographystyle which does exactly what I want except one thing:

I would like to have ONLY the DOI link as default and if no DOI is available for the reference, then the URL should be displayed. I guess I found the function which I have to modify. Unfortunately, I have no idea how:

FUNCTION {output.web.refs}
{
  new.block
  inlinelinks
    'skip$ % links were inline -- don't repeat them
    { % If the generated DOI will be the same as the URL,
      % then don't print the URL (thanks to Joseph Wright for this code,
      % at http://tex.stackexchange.com/questions/5660)
      adddoiresolver
          doiurl doi empty$ { "X" } { doi } if$ * % DOI URL to be generated
          url empty$ { "Y" } { url } if$          % the URL, or "Y" if empty
          =                                       % are the strings equal?
          and
        'skip$
        { output.url }
      if$
      addeprints eprint empty$ not and
        { format.eprint output.nonnull }
        'skip$
      if$
      adddoiresolver doi empty$ not and
        { format.doi output.nonnull }
        'skip$
      if$
      addpubmedresolver pubmed empty$ not and
        { format.pubmed output.nonnull }
        'skip$
      if$
    }
  if$
}
0

1 Answer 1

1

BibTeX style language (so niche it doesn't even have an agreed name) can seem a little odd, and this question is an excellent prompt for a brief language tutorial, focused on how this particular function works (it illustrates quite a lot of the language).

You have indeed found the right function. I'm not completely sure I understand your requirement, but in any case it's probably better to give a compact language tutorial. Full details are in the ‘btxhak.pdf’ document at CTAN.

Background:

  • The language is a postfix stack language, like Postscript -- functions pop zero or more items off the stack, act on them, possibly with side-effects, and push zero or more results back.
  • Functions like new.block produce output, in this case the <newline> \newblock at the beginning of components of the formatted output.
  • Functions with trailing $ are built-in, others are defined locally.

More specifically:

  • This output.web.refs function, defined in this .bst file, is called at various points, to format URL/DOI/other links.
  • It doesn't expect to find anything on the stack, and doesn't leave anything on the stack (that is, it's all side-effect).
  • inlinelinks is a constant number, defined at the top of the file. Its use here results in the corresponding number being placed on the stack.
  • adddoiresolver and friends are also constant switches, defined at the top of the file.
  • if$ pops three things from the stack, namely a condition, a true-branch, and a false-branch; each of these branches can be a function call, a sequence of calls in {...}, or the no-op 'skip$. It evaluates the condition (0/blank are false, everything else true), and executes the appropriate branch.
  • The function empty$ pops one thing from the stack and pushes true if the corresponding BibTeX field was empty. Thus doi empty$ evaluates to true if the doi="..." field in the .bib file was absent or empty.
  • The function = pops two stack values and pushes true if they're equal.
  • Each of the output.nonnull calls pops one item off the stack (left there by eg format.doi) and prints it. Thus this function doesn't leave anything on the stack.

(Check the btxhak.pdf document – I may have misremembered a couple of these points).

With this information, you might be able work out what the function is doing, and adjust it appropriately. If you do adjust it, make sure to rename the file and put a note to this effect at the top (this is the standard LaTeX ‘change-control’ process, very much in scare-quotes).

I suspect that adding doi empty$ not and to the line after the first adddoiresolver might achieve your goal.

Tips when writing .bst files:

  • Create a test.bib file of entries which cover all of your test cases.
  • Create a test.aux file which contains \bibstyle{myunsrturl.bst} \bibdata{test} \citation{*}, and then do bibtex test.
  • Confirm that bibtex test works without error before you start changing things, and then make little changes and retest.
  • By far the most common error is to end up with too many or too few things left on the stack. BibTeX's error messages aren't very helpful.

Good luck.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .