TransWikia.com

Formatting cite to: author [ref]

TeX - LaTeX Asked by user190963 on November 1, 2021

I am doing citations like cite{waseem16} where it appears like: Waseem and Hovy [2016].

But I want to make it appear like: Wassem and Hovy[WH16].

How do I format my citation to appear like so?
Thanks

usepackage{natbib} 
bibliographystyle{plainnat}

% bibliography is in ``myrefs.bib''
PrintBib{myrefs}

newcommand{PrintBib}[1]{%
renewcommand{bibname}{Refer^{e}ncias}%
if@numericrefs%
  bibliographystyle{unsrt-pt}%
else%
  if@alpharefs%
    bibliographystyle{alpha-pt}%
  else%
    bibliographystyle{plainnat-pt}%
  fi%
fi%
cleardoublepage%
phantomsection%
addcontentsline{toc}{chapter}{Refer^{e}ncias}%
begin{singlespace}bibliography{#1}end{singlespace}}

The follwing code are just the few fist lines of painnat-pt file:

ENTRY
  { address
    author
    booktitle
    chapter
    doi
    eid
    edition
    editor
    howpublished
    institution
    isbn
    issn
    journal
    key
    month
    note
    number
    organization
    pages
    publisher
    school
    series
    title
    type
    url
    volume
    year
  }
  {}
  { label extra.label sort.label short.list }

INTEGERS { output.state before.all mid.sentence after.sentence after.block }

FUNCTION {init.state.consts}
{ #0 'before.all :=
  #1 'mid.sentence :=
  #2 'after.sentence :=
  #3 'after.block :=
}

One Answer

You can edit abbrvnat.bst to be some sort of a mix between alpha and abbrv. Save the resulting file as alphanat.bst. Note that this is only a proof of concept, it works for the MWE but it may break at any point.

The modifications to abbrvnat.bst are as follows.

  1. Copy the function format.lab.names (the label formatting function) and a few lines above that, unmodified, to alphanat.bst and rename the function to format.lab.alphanames:
INTEGERS { et.al.char.used }

FUNCTION {initialize.et.al.char.used}
{ #0 'et.al.char.used :=
}

EXECUTE {initialize.et.al.char.used}

FUNCTION {format.lab.alphanames}
{ 's :=
  s num.names$ 'numnames :=
  numnames #1 >
    { numnames #4 >
        { #3 'namesleft := }
        { numnames 'namesleft := }
      if$
      #1 'nameptr :=
      ""
        { namesleft #0 > }
        { nameptr numnames =
            { s nameptr "{ff }{vv }{ll}{ jj}" format.name$ "others" =
                { "{etalchar{+}}" *
                  #1 'et.al.char.used :=
                }
                { s nameptr "{v{}}{l{}}" format.name$ * }
              if$
            }
            { s nameptr "{v{}}{l{}}" format.name$ * }
          if$
          nameptr #1 + 'nameptr :=
          namesleft #1 - 'namesleft :=
        }
      while$
      numnames #4 >
        { "{etalchar{+}}" *
          #1 'et.al.char.used :=
        }
        'skip$
      if$
    }
    { s #1 "{v{}}{l{}}" format.name$
      duplicate$ text.length$ #2 <
        { pop$ s #1 "{ll}" format.name$ #3 text.prefix$ }
        'skip$
      if$
    }
  if$
}
  1. Change the function author.key.label to concatenate the output of the regular label formatting, an open parentheses, and the output of the function copied from alpha.bst:
FUNCTION {author.key.label}
{ author empty$
    { key empty$
        { cite$ #1 #3 substring$ }
        'key
      if$
    }
    { author format.lab.names "(" * author format.lab.alphanames * }
  if$
}
  1. In the comments it was noted that there was an issue with a different .bib file, it turned out that the solution did not work for @book entries. These entries call the function author.editor.key.label instead of author.key.label. Therefore that function should be adapted as well, similar to the modification to author.key.label:
FUNCTION {author.editor.key.label}
{ author empty$
    { editor empty$
        { key empty$
            { cite$ #1 #3 substring$ }
            'key
          if$
        }
        { editor format.lab.names "(" * editor format.lab.alphanames * }
      if$
    }
    { author format.lab.names "(" * author format.lab.alphanames * }
  if$
}

Note that the functions author.key.organization.label and editor.key.organization.label should also be modified accordingly for entries where organizations are involved.

  1. Modify the function calc.label in order to remove the parenthesis and to print only the last two digits of the year:
FUNCTION {calc.label}
{ calc.short.authors
  short.list
  year #-1 #2 substring$ duplicate$ empty$
  short.list key field.or.null = or
     { pop$ "" }
     'skip$
  if$
  *
  'label :=
}

Finally, load natbib with the option square to get square brackets, and load the new alphanat style:

documentclass{article}
usepackage[square]{natbib}
bibliographystyle{alphanat}
begin{document}
See cite{waseem16}.

bibliography{myrefs}
end{document}

Result:

enter image description here

Edit: Of course it is nice if the alpha key is also shown in the bibliography itself. To do that, you should copy a slightly modified version of the format.lab.alphanames function somewhere above the format.authors function around line 250 (so the modified function is defined when format.authors needs it):

FUNCTION {format.lab.anames}
{ 's :=
  s num.names$ 'numnames :=
  numnames #1 >
    { numnames #4 >
        { #3 'namesleft := }
        { numnames 'namesleft := }
      if$
      #1 'nameptr :=
      ""
        { namesleft #0 > }
        { nameptr numnames =
            { s nameptr "{ff }{vv }{ll}{ jj}" format.name$ "others" =
                { "+" * }
                { s nameptr "{v{}}{l{}}" format.name$ * }
              if$
            }
            { s nameptr "{v{}}{l{}}" format.name$ * }
          if$
          nameptr #1 + 'nameptr :=
          namesleft #1 - 'namesleft :=
        }
      while$
      numnames #4 >
        { "+" * }
        'skip$
      if$
    }
    { s #1 "{v{}}{l{}}" format.name$
      duplicate$ text.length$ #2 <
        { pop$ s #1 "{ll}" format.name$ #3 text.prefix$ }
        'skip$
      if$
    }
  if$
}

The difference to format.lab.alphanames is that the symbol for more than 4 authors (+) is hardcoded.

Then you can modify format.authors to use the label and the year:

FUNCTION {format.authors}
{ author empty$
    { "" }
    { "[" author format.lab.anames * year #-1 #2 substring$ * "] " * author format.names * }
  if$
}

Result:

enter image description here

Note that this would be much easier with BibLaTeX - but with BibTeX the question is more interesting :)

Answered by Marijn on November 1, 2021

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