0

I have biblatex with options

\usepackage[backend=bibtex, style=authoryear]{biblatex}

The citations made with \cite look like this: Smith, 2019 or Smith and Baker, 2019 and so on. I would like to define a command that automatically replaces the occurrence of my name with my initials. Namely I need

\cite{SmithsPaper}  % --> Smith, 2019
\cite{MyPaper}      % --> Manny, 2019
%%%
\newcite{SmithsPaper}  % --> Smith, 2019
\newcite{MyPaper}      % --> MC, 2019

I have tried with the package xstring to do something like

\newcommand{\newcite}[1]{%
  \begingroup
  \edef\@temp{\cite{#1}}%
  \StrSubstitute{\@temp}{Manny}{MC}
  \endgroup
}

The problem is that I can't figure out how to tell LaTeX to expand \cite{#1}. As a result the citation comes out without the replacement. If I change \cite{#1} by simply #1 and I call \newcite{bla Manny bla} it gives bla MC bla as expected.

It shouldn't be relevant but I'm using this within the beamer documentclass.

moewe
  • 175,683
MannyC
  • 173
  • 1
    \cite isn't completely expandable and defined \protected, so the short answer is, you can't do this by expanding \cite. But there are some real biblatex experts here, so maybe one of those has an idea. – Skillmon Dec 08 '19 at 00:25

1 Answers1

3

As Skillmon says in the comments: \cite... commands are not expandable, so you can't (easily) perform xstring replacements on them. Instead I would try and let biblatex do the replacement directly.

One way to do that is by using the approach of my answer to Make specific author bold using biblatex (see also Highlight an author in bibliography using biblatex allowing bibliography style to format it). The idea is to obtain the unique name hash computed by Biber for the name you want to replace and then replace it with the initials in \mkbibcompletename.

Note that this particular implementation requires at least biblatex version 3.13 (2019-08-17).

You can request replacement of a name with \replacenamewith{<name>}{<replacement>}

\replacenamewith{Emma Sigfridsson}{ES}

The replacement can be styled by redefining \mkbinamereplacement.

Some more explanations can be found in the links above.

Warning, this document will overwrite the file <name of the main TeX file/\jobname>-replacenames.bib without warning. The name of this overwritten helper file can be changed by redefining \hlblx@bibfile@name.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[backend=biber, style=authoryear]{biblatex}

\addbibresource{biblatex-examples.bib}

\makeatletter \def\hlblx@bibfile@name{\jobname -replacenames.bib} \newwrite\hlblx@bibfile \immediate\openout\hlblx@bibfile=\hlblx@bibfile@name

\newcounter{hlblx@name} \setcounter{hlblx@name}{0}

\newcommand*{\hlblx@writenametobib}[2]{% \stepcounter{hlblx@name}% \edef\hlblx@tmp@nocite{% \noexpand\AfterPreamble{% \noexpand\setbox0\noexpand\vbox{% \noexpand\hlblx@getmethehash{hlblx@name@\the\value{hlblx@name}}}}% }% \hlblx@tmp@nocite \immediate\write\hlblx@bibfile{% @misc{hlblx@name@\the\value{hlblx@name}, author = {\unexpanded{#1}}, % note = {\unexpanded{#2}}, % options = {dataonly=true},}% }% }

\AtEndDocument{% \closeout\hlblx@bibfile}

\addbibresource{\hlblx@bibfile@name}

\newcommand*{\hlblx@hashextract@i}[1]{% \csgdef{replacename@\thefield{fullhash}}{#1}}

\DeclareNameFormat{hlblx@hashextract}{% \usefield{\hlblx@hashextract@i}{note}}

\DeclareCiteCommand{\hlblx@getmethehash} {} {\printnames[hlblx@hashextract][1-999]{author}} {} {}

\renewcommand*{\mkbibcompletename}[1]{% \ifcsundef{replacename@\thefield{hash}} {#1} {\mkbinamereplacement{\csuse{replacename@\thefield{hash}}}}}

% {<name>}{<replacement>} \newcommand*{\replacenamewith}{\hlblx@writenametobib} \makeatother

% formatting for the replacement \newcommand*{\mkbinamereplacement}[1]{\textbf{#1}}

% declare a replacement for a name % this command can be used several times \replacenamewith{Emma Sigfridsson}{ES}

\begin{document} \cite{sigfridsson}

\printbibliography \end{document}

ES and Ryde 1998

moewe
  • 175,683