Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I use BibTeX not only to create bbl files for my LaTeX documents, but also to produce HTML of my whole collection of articles. It works pretty well for the last 12 years but since I decided to add a DOI field I've got a minor problem.

BibTeX, outputs

(<small>doi:<a href=http://dx.doi.org/10.1051/jp2:1995145>10.1051/jp2:1995145</a></small>)

as I programmed it like

FUNCTION {startdoienc}
{ duplicate$ empty$
    { pop$ "" }
    { " (<small>doi:<a href=http://dx.doi.org/" swap$ * ">" * }
  if$
}

FUNCTION {enddoienc}
{ duplicate$ empty$
    { pop$ "" }
    { "</a></small>)" * }
  if$
}

FUNCTION {article}
{ output.bibitem
  .... <cut out>
  new.block
  doi startdoienc  output
  doi enddoienc  pureoutput 
  new.block
  .... <cut out>
}

So the HTML result looks like

A. Ajdari, F. Brochard-Wyart, C. Gay, P. G. de Gennes, and J. L. Viovy Drag on a tethered chain moving in a polymer melt J. Phys. II France 1995 5 491–495 (doi:10.1051/jp2:1995145)

where the DOI link is clickable.

However, some journals (e.g. J. Polym. Sci. B: Polym. Phys.) have characters in their DOI's, which need to be escaped to become a valid URL: <, >, and #. Example:

10.1002/(SICI)1099-0488(19990701)37:13<1449::AID-POLB11>3.0.CO;2-T

Question: is it possible to program BibTeX to substitute, e.g., "<" by "%3c" before outputting it?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Search-and-replace is possible using BibTeX. The excellent Tame the BeaST suggests

INTEGERS{ l }
FUNCTION{ string.length }
{
  #1 'l :=
  {duplicate$ duplicate$ #1 l substring$ = not}
    {l #1 + 'l :=}
  while$
  pop$ l
}

STRINGS{replace find text}
INTEGERS{find_length}
FUNCTION{find.replace}
{ 'replace :=
  'find :=
  'text :=
  find string.length 'find_length :=
  ""
    { text empty$ not }
    { text #1 find_length substring$ find =
        {
          replace *
          text #1 find_length + global.max$ substring$ 'text :=
        }
        { text #1 #1 substring$ *
          text #2 global.max$ substring$ 'text :=
        }
      if$
    }
  while$
}

So you should be able to do something like

FUNCTION {startdoienc}
{ duplicate$ empty$
    { pop$ "" }
    {
       " (<small>doi:<a href=http://dx.doi.org/" swap$ 
       "<" "\%3c" find.replace 
       * ">" *
    }
  if$
}

You may also need to escape the % to stop it being read as a comment by LaTeX.

share|improve this answer
    
Works like a charm! Thanks! Just a small remark: I do not need to escape "%" in my particular case as I output HTML, not TeX. –  amorua Sep 11 '11 at 12:06
    
After a bit more thorough testing I do run into a problem: the trick is okay with "<" and ">" but fails on "#". I tried using "\#" instead but it does not work either: bibtex does not complaint but does not substitute '#' by '%23'either. –  amorua Sep 11 '11 at 13:43
    
@user7788: I suspect you may be out of luck, as # is 'special' for BibTeX, and I'm not sure this can be turned off. –  Joseph Wright Sep 12 '11 at 18:22
    
I think I'm going to solve that '#'-issue with a simple perl script. Anyway, thanks again. –  amorua Sep 13 '11 at 8:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.