2

How can I customize the names and year part of the labelalpha field separately? For example, I want the name part in small caps, while the number representing the year in old style numerals.

Desired and current result of the MWE.

MWE:

\documentclass{article}

\usepackage[style=alphabetic]{biblatex}
\addbibresource{biblatex-examples.bib}

% Using colors to clarify
\usepackage{xcolor}

\DeclareFieldFormat{labelalpha}{\textsc{\oldstylenums{\MakeLowercase{#1}}}}

% None of the following works :-(
\DeclareFieldFormat{labelname}{\textcolor{green}{#1}}
\DeclareFieldFormat{labelnumber}{\textcolor{red}{#1}}
\DeclareFieldFormat{labelyear}{\textcolor{red}{#1}}
\DeclareFieldFormat{labeldate}{\textcolor{red}{#1}}
\DeclareFieldFormat{labeldateparts}{\textcolor{red}{#1}}

\begin{document}
Desired: [\textcolor{green}{\textsc{sr}}\oldstylenums{\textcolor{red}{98}}]

Current: \cite{sigfridsson}

\printbibliography
\end{document}
2

The alphabetic label gets produced by Biber and is passed on to biblatex in the .bbl file. biblatex doesn't really have a sense of what the label is composed of and only sees the complete label, so the formatting has to be shoehorned in at label creation.

We can modify the label format with \DeclareLabelalphaTemplate and apply formatting there. But we have to take into account that the label definitions are written to the .bcf file, which makes it tricky to add macros to the definition, since they usually get expanded when writing to files. That's definitely not what we want here. To avoid expansion we need a fair bit of \string in the definition, the braces for the arguments are even trickier, they need \@charlb and \@charrb (see Writing { and } to a file with LaTeX). We can avoid some headaches for more complex macros if we define two helper macros to format the label parts.

With

\documentclass{article}

\usepackage[style=alphabetic]{biblatex}
\addbibresource{biblatex-examples.bib}

% Using colors to clarify
\usepackage{xcolor}

\newcommand*{\mklabelalphaname}[1]{\color{green}{\textsc{#1}}}
\newcommand*{\mklabelalphayear}[1]{\textcolor{red}{\oldstylenums{#1}}}

\makeatletter
\DeclareLabelalphaTemplate{
  \labelelement{
    \literal{\string\mklabelalphaname\@charlb}
  }
  \labelelement{
    \field[final]{shorthand}
    \field{label}
    \field[strwidth=3,strside=left,ifnames=1,lowercase=true]{labelname}
    \field[strwidth=1,strside=left,lowercase=true]{labelname}
  }
  \labelelement{
    \literal{\@charrb\string\mklabelalphayear\@charlb}
  }
  \labelelement{
    \field[strwidth=2,strside=right]{year}
  }
  \labelelement{
    \literal{\@charrb}
  }
}
\makeatother

\begin{document}
Desired: [\textcolor{green}{\textsc{sr}}\oldstylenums{\textcolor{red}{98}}]

Current: \cite{sigfridsson}

\printbibliography
\end{document}

the .bbl contains

\field{labelalpha}{\mklabelalphaname{sr}\mklabelalphayear{98}}

and together with the definitions of \mklabelalphaname and \mklabelalphayear produces

alphabetic label "SR98" "SR" is in green small caps, "98" in red old-style numerals

  • I really like this approach, thanks! I’m hitting a bug I cannot solve so quickly myself: This workaround breaks shorthand because of the use of final. I.e., \cite{kant:kpv} will write \field{labelalpha}{\mklabelalphaname{KpV} with one } missing. I tried to fix it by removing final and including shorthand with strwidth=0 another time. However, biber will use strwidth=1 instead. For some other reason I now also receive an error Encoding scheme ‘oml’ unknown., but that can be ignored it seems. – timothymctim Aug 15 at 18:10
  • Okay, \DeclareLabelalphaTemplate{ […] \labelelement{ \field[strwidth=-1]{shorthand} […] } \labelelement{ \literal{\@charrb} } \labelelement{ \field[strwidth=0,strside=right,final]{shorthand} […] } \labelelement{ \field[strwidth=2,strside=right]{year} } \labelelement{ \literal{\@charrb} } } is what I wanted, but this of course doesn’t work as now I get \mklabelalphaname{Kp}V. :( – timothymctim Aug 15 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.