# Custom Biblatex reference style

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.

-
Does tex.stackexchange.com/questions/68862/… provide any help? –  Joseph Wright Mar 30 '13 at 7:09

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

\documentclass{article}

\usepackage{xstring}
\usepackage[style=alphabetic,maxbibnames=99]{biblatex}

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

\renewcommand{\labelalphaothers}{}

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

\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
}

-
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
\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
Yes, this requires biber to work. –  Andrew Swann Mar 29 '13 at 15:19

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{
}
\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.

-

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.

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