TransWikia.com

Apply a string replacement on the citation as returned by cite

TeX - LaTeX Asked by MannyC on September 18, 2020

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.

One Answer

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
defhlblx@bibfile@name{jobname -replacenames.bib}
newwritehlblx@bibfile
immediateopenouthlblx@bibfile=hlblx@bibfile@name

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

newcommand*{hlblx@writenametobib}[2]{%
  stepcounter{hlblx@name}%
  edefhlblx@tmp@nocite{%
    noexpandAfterPreamble{%
      noexpandsetbox0noexpandvbox{%
        noexpandhlblx@getmethehash{hlblx@name@thevalue{hlblx@name}}}}%
  }%
  hlblx@tmp@nocite
  immediatewritehlblx@bibfile{%
    @misc{hlblx@name@thevalue{hlblx@name},
          author = {unexpanded{#1}}, %
          note   = {unexpanded{#2}}, %
          options = {dataonly=true},}%
  }%
}

AtEndDocument{%
  closeouthlblx@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

Correct answer by moewe on September 18, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP