2

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 :=
}
  • (1) Welcome, (2) where does \PrintBib come from? Never seen that before. Please provide a full but minimal example instead of a sniplet like this. Basically provide something others can copy and use as is in order to test whatever issue you are having. – daleif Jun 13 '19 at 13:58

1 Answers1

2

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 :)

Marijn
  • 37,699
  • I think I done the intructions as you said, but I only have the citation as: [1], [2], etc. Could you link using pastebin your version of the .bst please? – user190963 Jun 13 '19 at 17:19
  • 1
    @user190963 it's here: https://pastebin.com/jxNAruRM - note that you also need to specify the bibliographystyle in your .tex file. – Marijn Jun 13 '19 at 20:07
  • Stange, now I'm getting the citation like this: [Waseem and Hovy(WH16)Waseem and Hovy] – user190963 Jun 14 '19 at 09:36
  • Did you try with the MWE or with your own .tex file? – Marijn Jun 14 '19 at 09:52
  • I used it in my .tex, like this \bibliographystyle{alphanat} – user190963 Jun 14 '19 at 09:59
  • And what happens when you try the .tex file from my answer, without any modifications (with pdflatex), with a .bib file containing only the waseem16 entry and nothing else? If that works then there is something in your file that modifies the behavior, otherwise the problem might be how you apply the solution, where you store your files, etc. Or maybe you did not remove all auxiliary files before trying the solution? – Marijn Jun 14 '19 at 10:38
  • Yes, it was some conditions that were setting other styles, Thanks, problem solved!! – user190963 Jun 14 '19 at 11:30
  • @Marijn: Thanks a lot for the effort and for providing that solution. However, when I try your MWE, I get ERROR: Package natbib Error: Bibliography not compatible with author-year citations. I've used your file from pastebin in the same directory as the MWE, with a bibfile only containing a fake waseem16 entry. Getting the same error with my own Latex document and my own bibfile, btw. – mfg Jul 19 '20 at 11:01
  • @Mario strange, that error is intended for a different situation. If you want we can discuss the issue in chat (https://chat.stackexchange.com/rooms/info/110782/discussion-between-marijn-and-mario?tab=general). – Marijn Jul 19 '20 at 11:25