[Tex/LaTex] Bibtex .bst file modification to include alphabetical ordering

bibtexsorting

Is it possible to modify the JHEP bibliography style in a simple way such that it displays bibliographical entries in alphabetical order, instead of basing on first appearence in the main text?

(Addendum) Is it possible to add in the same way a function that prints a long horizontal line —– instead of the name of the author(s) if these are repeated, as is done e.g. by the amsplain.bst style?

Best Answer

The bibliography style JHEP seems to be a lightly modified version of the venerable unsrt style. To enable alphabetical sorting of the entries, you should modify JHEP.bst as follows. (Aside: I'm not sure if this procedure satisfies your objective that the modifications be done "in a simple way". However, I've tried out the code, and it seems to work!)

  • Make a copy of the file JHEP.bst. Call the copy (say) JHEPsort.bst. (Don't edit the original file directly.)

  • Open the file JHEPsort.bst in a text editor. The editor you use to edit your tex files will do fine.

  • Locate the lines

    MACRO {pr} {"Phys. Rev."}
    
    READ
    
    STRINGS { longest.label }
    

    in this file. Hint: In my copy of this file, this excerpt starts on l. 1048.

  • Insert the following 145 or so lines between the line READ and the line STRINGS { longest.label }:

    FUNCTION {sortify}
    { purify$
      "l" change.case$
    }
    
    INTEGERS { len }
    
    FUNCTION {chop.word}
    { 's :=
      'len :=
      s #1 len substring$ =
        { s len #1 + global.max$ substring$ }
        's
      if$
    }
    
    FUNCTION {sort.format.names}
    { 's :=
      #1 'nameptr :=
      ""
      s num.names$ 'numnames :=
      numnames 'namesleft :=
        { namesleft #0 > }
        { nameptr #1 >
            { "   " * }
            'skip$
          if$
          s nameptr "{vv{ } }{ll{ }}{  ff{ }}{  jj{ }}" format.name$ 't :=
          nameptr numnames = t "others" = and
            { "et al" * }
            { t sortify * }
          if$
          nameptr #1 + 'nameptr :=
          namesleft #1 - 'namesleft :=
        }
      while$
    }
    
    FUNCTION {sort.format.title}
    { 't :=
      "A " #2
        "An " #3
          "The " #4 t chop.word
    chop.word
      chop.word
      sortify
      #1 global.max$ substring$
    }
    
    FUNCTION {author.sort}
    { author empty$
        { key empty$
            { "to sort, need author or key in " cite$ * warning$
              ""
            }
            { key sortify }
          if$
        }
        { author sort.format.names }
      if$
    }
    
    FUNCTION {author.editor.sort}
    { author empty$
        { editor empty$
            { key empty$
                { "to sort, need author, editor, or key in " cite$ * warning$
                  ""
                }
                { key sortify }
              if$
            }
            { editor sort.format.names }
          if$
        }
        { author sort.format.names }
      if$
    }
    
    FUNCTION {author.organization.sort}
    { author empty$
        { organization empty$
            { key empty$
                { "to sort, need author, organization, or key in " cite$ * warning$
                  ""
                }
                { key sortify }
              if$
            }
            { "The " #4 organization chop.word sortify }
          if$
        }
        { author sort.format.names }
      if$
    }
    
    FUNCTION {editor.organization.sort}
    { editor empty$
        { organization empty$
            { key empty$
                { "to sort, need editor, organization, or key in " cite$ * warning$
                  ""
                }
                { key sortify }
              if$
            }
            { "The " #4 organization chop.word sortify }
          if$
        }
        { editor sort.format.names }
      if$
    }
    
    FUNCTION {presort}
    { type$ "book" =
      type$ "inbook" =
      or
        'author.editor.sort
        { type$ "proceedings" =
        'editor.organization.sort
            { type$ "manual" =
                'author.organization.sort
                'author.sort
              if$
            }
          if$
        }
      if$
      "    "
      *
      year field.or.null sortify
      *
      "    "
      *
      title field.or.null
      sort.format.title
      *
      #1 entry.max$ substring$
      'sort.key$ :=
    }
    
    ITERATE {presort}
    
    SORT
    

    Just in case you're curious where this code comes from: I certainly did not create it myself! Instead, I copied it from the bibliography style file plain.bst.

  • Save the file JHEPsort.bst either to the directory that contains your main tex file or to a directory that's searched by BibTeX. If you choose the second option, you will probably need to update the filename database of your TeX distribution, in a way that's appropriate for your TeX distribution.

  • Start using the new bibliography style by providing the instruction

    \bibliographystyle{JHEPsort}
    

    instead of \bibliographystyle{JHEP}. Be sure to run LateX, BibTeX, and LaTeX twice more so that all changes are fully propagated.

Happy BibTeXing!