8

I am trying to get my bibliography to show the references as [ABCD01], where:

  • ABCD are the first 4 letters of the first author's last name (with an underscore replacing the missing characters if the last name is shorter than 4 letters),

  • 01 are the last 2 digits of the year.

How can I do this?

PS: If ABCD could be in smallcaps, that would be even better but I don't know if this can be done.

wizplum
  • 557

3 Answers3

8

You ask several things in the question. Here is my proposed solution, with an explanation following.

Sample output

\documentclass{article}

\usepackage{xstring}
\usepackage[style=alphabetic,maxbibnames=99]{biblatex}
\addbibresource{ref.bib}

\DeclareLabelalphaTemplate{
  \labelelement{
    \field[strwidth=4,strside=left,names=1,lowercase]{labelname}
    }
  \labelelement{
    \field[strwidth=2,strside=right]{year}
    }
}

\renewcommand{\labelalphaothers}{}

\DeclareFieldFormat{labelalpha}{\textsc{\padded{#1}}}
%\DeclareFieldFormat{extraalpha}{\textsc{\mknumalph{#1}}}

\newcommand{\padded}[1]{\begingroup%
\expandarg\StrGobbleRight{#1}{2}[\mystr]%
\StrLeft{\mystr \textunderscore\textunderscore\textunderscore\textunderscore}{4}%
\StrRight{#1}{2}%
\endgroup}

\begin{document}

\cite{one,oneo,two,three,four}
\printbibliography

\end{document}

with ref.bib given at the end of my answer.

Firstly, the alphabetic style is the closest to the one you request and is modifiable via the \DeclareLabelalphaTemplate. The code above pulls off up to four characters from the last name of the first author, adds to the two final digits of the year, and final a letter to disambiguate if necessary. We make the letters lowercase in preparation for makeing them small caps.

The command setting \labelalphaothers to null, ensures that no + sign is added when there are several authors.

The formatting of the labels as small caps is acheived via changing the field format of labelalpha. This is the part of the label up to and including the year digits, but not the disambiguation letter. If you want that in small caps then uncomment the line with extraalpha.

Now I use the xstring package to deal with the padding issue via a command \padded. We pull off the year, add on underscores and take just the first four characters. Then we put the year back on. The \expandarg command ensures that only \mystring gets expanded in the argument to \StrLeft, expanding the underscores would give problems.

@Article{one,
  author =   {Author, A. N. and Brother, K. and Style, D.},
  title =    {One article},
  journal =  {J. J.},
  year =     2003
}

@Article{oneo,
  author =   {Any, A. N. and Brother, K. and Stile, D.},
  title =    {One article},
  journal =  {J. J.},
  year =     2003
}

@Misc{three,
  author =   {Ma, A. and Many, B. and Many, C. and Many, D. and
                  Many, E.},
  title =    {One page},
  year =     2005
}

@Misc{four,
  author =   {Ma, A. and Many, B. and Many, C. and Many, D. and
                  Many, F.},
  title =    {One page},
  year =     2005
}

@Book{two,
  author =   {Weeks, P. and Days, X. and Years, R. and Months, S.},
  editor =   {May, X.},
  title =    {One book},
  publisher =    {U. Publ.},
  year =     2001
}
Andrew Swann
  • 95,762
  • Thanks, I tried this but I'm still having some issues. All of the 4-character strings are getting cut down to 3 characters with 1 underscore at the end. Additionally, when there are 2 or 3 authors I'm still getting the initials (when there is only 1 or 4+, I do get the first letters of the first author's last name though). I am testing this with \nocite{*}, but trying with \cite{} doesn't make any difference. – wizplum Mar 28 '13 at 23:01
  • 1
    \expandarg is a declaration that applies to all commands of xstring from that point on. I'd suggest to say \begingroup\expandarg\StrGobbleRight{#1}{2}[\mystr]\StrLeft{\mystr \textunderscore\textunderscore\textunderscore\textunderscore}{4}\StrRight{#1}{2}\endgroup – egreg Mar 28 '13 at 23:21
  • @wizplum May be you have older versions of biblatex or xstring. The output above is fine with \nocite{*} too. The versions on my system are biblatex.sty 2013/01/10 v2.5 and xstring.sty 2013/2/28 v1.7a. You can find the versions you have by putting \listfiles after your \documentclass and looking in the log file. – Andrew Swann Mar 29 '13 at 14:57
  • @egreg Thanks. I was clear about the global effect of this command. I have changed my code to yours, so #1 is treated consistently, though it should make no difference as used in this case as what we pass to #1 will be a plain string of characters. The alternative for general use would be to switch back to standard (full) expansion for the final string operation. – Andrew Swann Mar 29 '13 at 15:00
  • Still I would put the thing in a group, so the effect of \expandarg doesn't propagate. – egreg Mar 29 '13 at 15:11
  • I finally figured out what the problem was. Apparently the reason why it wasn't working was that I was running backend=bibtex and I needed backend=biber instead. Thanks for everything again! – wizplum Mar 29 '13 at 15:16
  • 1
    Yes, this requires biber to work. – Andrew Swann Mar 29 '13 at 15:19
  • @egreg Now I understand. Code updated accordingly with an extra level of grouping. – Andrew Swann Mar 29 '13 at 15:21
  • The next version of biblatex/biber (2.6/1.6) will probably have a "padchar" option in \DeclareLabelalphaTemplate to allow padding of label elements to the strwidth value. – PLK Mar 29 '13 at 15:57
  • @PLK Very useful. Will it cope with underscore? I had far simpler solutions to the padding question when the character involved was an ordinary alphabetic/numeric. – Andrew Swann Mar 29 '13 at 16:11
  • I'll check - I will probably have this in the beta versions in an hour or so. Will update here – PLK Mar 29 '13 at 16:14
3

Biblatex 2.6+biber 1.6 (both in development folders on SourceForge) has this built in now - there are new options for declaring labels (see biblatex docs). For example, in your case, just put this in your preamble:

\DeclareLabelalphaTemplate{
  \labelelement{
    \field[strwidth=4, strside=left, padchar=_, lowercase]{labelname}
    }
  \labelelement{
    \field[strwidth=2, strside=right]{year}
    }
}

\renewcommand{\labelalphaothers}{}

A hidden benefit of this is that the sorting for the labels will be correct as internally, biber has to do a lot of quoting/unquoting to make this work between TeX, XML (the .bcf file) and the sorting algorithms.

PLK
  • 22,776
1

I've not had a chance to test this out, but you should be able to use the alphabetic style, while adding the following lines to your preamble:

\DeclareLabelalphaTemplate{
    \labelelement{
      \field[final]{shorthand}
      \field{label}
      \field[strwidth=4,strside=left]{labelname}}
    \labelelement{\field[strwidth=2,strside=right]{year}}}

\DeclareFieldFormat{labelalpha}{\textsc{#1}}%

It probably won't add underscore lines to those authors with a name of under four letters, but those cases will be uncommon. In the case of an author named simply "Lee," for example, you should be able to define shorthand = {Lee_} for that entry in your .bib file.

jmclawson
  • 890
  • Thanks, this solution gives me the same error but without the underscores obviously. What could be the problem? – wizplum Mar 28 '13 at 23:05
  • 1
    If you test this, you will see that you need to add a names=1 to the processing of labelname to restrict this to first author as requested by the poster. Also underscores in the shorthand will give an error, replacing them by \textunderscore will work, and you will need to add the year digits there too. – Andrew Swann Mar 29 '13 at 16:17