對 \cite 傳回的引文套用字串替換

對 \cite 傳回的引文套用字串替換

我有biblatex選項

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

用 進行的引用\cite看起來像這樣:Smith, 2019orSmith and Baker, 2019等。我想定義一個命令,自動用我的姓名首字母替換我的名字。即我需要

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

我嘗試過用這個包xstring來做類似的事情

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

問題是我不知道如何告訴 LaTeX 擴充\cite{#1}。結果引文出來了,沒有替換。如果我\cite{#1}簡單地改變#1並調用它,就會按預期\newcite{bla Manny bla}給出。bla MC bla

它不應該相關,但我在投影機文件類中使用它。

答案1

作為技能獸在評論中說:\cite...命令不可擴展,因此您無法(輕鬆)xstring對它們執行替換。相反,我會嘗試biblatex直接進行替換。

一種方法是使用以下方法我的答案使用 biblatex 將特定作者加粗(也可以看看使用 biblatex 在參考書目中突出顯示作者,允許參考書目樣式對其進行格式化)。這個想法是獲取 Biber 為您要替換的名稱計算的唯一名稱哈希,然後將其替換為 中的首字母縮寫\mkbibcompletename

請注意,此特定實作至少需要biblatex版本 3.13 (2019-08-17)。

您可以請求更換姓名\replacenamewith{<name>}{<replacement>}

\replacenamewith{Emma Sigfridsson}{ES}

可以透過重新定義來設計替換的樣式\mkbinamereplacement

可以在上面的連結中找到更多解釋。

警告,此文件將覆蓋該文件<name of the main TeX file/\jobname>-replacenames.bib而不發出警告。可以透過重新定義 來變更此覆蓋的幫助程式檔案的名稱\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 和萊德 1998

相關內容