如何在 biblatex 中为特定作者的姓名加下划线?

如何在 biblatex 中为特定作者的姓名加下划线?

我的目标是在由 biber/biblatex 处理的参考书目中强调特定作者的姓名。

我可以将它们以粗体显示如下https://tex.stackexchange.com/a/73246/127979https://tex.stackexchange.com/a/274571/127979

以下是 MWE:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@inproceedings{boldref, 
  AUTHOR = {InBold, to Put and Author, Non bold  and Highlight, Shine and  Ano, therOne},
  TITLE = {The title},
  BOOKTITLE = {The conference},
  PAGES = {65--78},
  YEAR = 2014}
\end{filecontents}

\usepackage{xpatch}
\usepackage[normalem]{ulem}

\usepackage[backend=biber,bibencoding=utf8,style=numeric-comp,maxbibnames=99]{biblatex}

\addbibresource{\jobname.bib}

\newbibmacro*{name:bbold}[2]{%
  \def\do##1{\iffieldequalstr{hash}{##1}{\bfseries\bgroup \listbreak}{}}%
  \dolistloop{\boldnames}%
}

\newbibmacro*{name:ebold}[2]{%
  \def\do##1{\iffieldequalstr{hash}{##1}{\egroup \listbreak}{}}%
  \dolistloop{\boldnames}%
}

\xpretobibmacro{name:given-family}{\begingroup\usebibmacro{name:bbold}{#1}{#2}}{}{}
\xpretobibmacro{name:delim}{\begingroup\normalfont}{}{}

\xapptobibmacro{name:given-family}{\usebibmacro{name:ebold}{#1}{#2}\endgroup}{}{}
\xapptobibmacro{name:delim}{\endgroup}{}{}

\newcommand*{\boldnames}{}
\forcsvlist{\listadd\boldnames}{
  {4fcd4bca11ef811f3aef17c792b6ef3e}, % InBold
  {01b588ba4e4ad753feae6c81709fc04b}} % Highlight

\begin{document}
\nocite{*}

\printbibliography

\end{document}

但如果我试图强调它们,

  • 通过替换\bfseriesby \uline,我收到错误:

    ./MWE.tex:47: Extra }, or forgotten \endgroup.
    \UL@stop ... \UL@putbox \fi \else \egroup \egroup 
                                                  \UL@putbox \fi \ifnum   \[email protected] 
    
    ./MWE.tex:47: Undefined control sequence.
    \UL@leaders ...L@skip \tw@ \UL@pixel \UL@leadtype 
                                                  \LA@hskip \UL@skip    \[email protected] 
    
    ./MWE.tex:47: Missing } inserted. 
    <inserted text> 
                    } 
    
  • \useunder{\uline}{\bfseries}{}通过在之前添加\printbibliography,会导致错误:

    Runaway argument?
    {{\@hangfrom {\hskip \z@ }\interlinepenalty \@M \refname \@@par }\endgroup \ETC.
    ! File ended while scanning use of \UL@on.
    <inserted text> 
                    \par 
    <*> \input MWE.tex
    
    ! Emergency stop.
    <*> \input MWE.tex
    
    !  ==> Fatal error occurred, no output PDF file produced!
    

我怀疑这与需要一段\uline{}时间{\bf }才足够大胆有关,但我可能遗漏了一些东西。

有什么方法可以实现这一点吗?我是否必须修补特定于样式的宏,例如https://tex.stackexchange.com/a/31202/127979

答案1

使用 uline 非常困难。以下解决方案提供了一种不允许在突出显示的作者处换行的方法。

使用此功能你必须删除以下行

\xpretobibmacro{name:given-family}{\begingroup\usebibmacro{name:bbold}{#1}{#2}}{}{}

因此,您需要一个仅包含名称的其他补丁。

\xpatchbibmacro{name:given-family}{\usebibmacro{name:delim}{#2#3#1}}{\usebibmacro{name:delim}{#2#3#1}\begingroup\usebibmacro{name:bbold}{#1}{#2}}{}{}

此后,您必须将突出显示的名称保存在保存框中以供\uline稍后使用:

\newbox\savenamebox

\newbibmacro*{name:bbold}[2]{%
  \def\do##1{\iffieldequalstr{hash}{##1}{\bfseries\setbox\savenamebox\hbox\bgroup \listbreak}{}}%
  \dolistloop{\boldnames}%
}

\newbibmacro*{name:ebold}[2]{%
  \def\do##1{\iffieldequalstr{hash}{##1}{\egroup\uline{\usebox\savenamebox}\listbreak}{}}%
  \dolistloop{\boldnames}%
}

完整示例如下:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@inproceedings{boldref, 
  AUTHOR = {InBold, to Put and Author, Non bold  and Highlight, Shine and  Ano, therOne},
  TITLE = {The title},
  BOOKTITLE = {The conference},
  PAGES = {65--78},
  YEAR = 2014}
\end{filecontents}

\usepackage{xpatch}
\usepackage[normalem]{ulem}

\usepackage[backend=biber,bibencoding=utf8,style=numeric-comp,maxbibnames=99]{biblatex}

\addbibresource{\jobname.bib}

\newbox\savenamebox

\newbibmacro*{name:bbold}[2]{%
  \def\do##1{\iffieldequalstr{hash}{##1}{\bfseries\setbox\savenamebox\hbox\bgroup \listbreak}{}}%
  \dolistloop{\boldnames}%
}

\newbibmacro*{name:ebold}[2]{%
  \def\do##1{\iffieldequalstr{hash}{##1}{\egroup\uline{\usebox\savenamebox}\listbreak}{}}%
  \dolistloop{\boldnames}%
}

\xpatchbibmacro{name:given-family}{\usebibmacro{name:delim}{#2#3#1}}{\usebibmacro{name:delim}{#2#3#1}\begingroup\usebibmacro{name:bbold}{#1}{#2}}{}{}

%\xpretobibmacro{name:given-family}{\begingroup\usebibmacro{name:bbold}{#1}{#2}}{}{}
\xapptobibmacro{name:delim}{\begingroup\normalfont}{}{}

\xapptobibmacro{name:given-family}{\usebibmacro{name:ebold}{#1}{#2}\endgroup}{}{}
\xapptobibmacro{name:delim}{\endgroup}{}{}

\newcommand*{\boldnames}{}
\forcsvlist{\listadd\boldnames}{
  {4fcd4bca11ef811f3aef17c792b6ef3e}, % InBold
  {01b588ba4e4ad753feae6c81709fc04b}} % Highlight

\begin{document}
\nocite{*}

\printbibliography

\end{document}

在此处输入图片描述

答案2

要回答如何使用 来做到这一点的问题style=apa:您只需修补bibmacro与 Marco Daniel 的出色答案不同的 s 即可。以下设置应仅在特定作者姓名不在第一个位置时为其加下划线:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}

@inproceedings{ref01, 
  AUTHOR = {Bloggs, Fred and Doe, John and Highlight, Shine},
  TITLE = {First Marvellous Paper},
  BOOKTITLE = {Amazing Book},
  PAGES = {11--111},
  YEAR = 2020}
  
@inproceedings{ref02, 
  AUTHOR = {Highlight, Shine and Doe, John and Bloggs, Fred},
  TITLE = {Second Marvellous Paper},
  BOOKTITLE = {Amazing Book},
  PAGES = {111--211},
  YEAR = 2020}
  
\end{filecontents}

\usepackage{xpatch}
\usepackage[normalem]{ulem}

\usepackage{doi}
\usepackage[backend=biber,
    style=apa,
    sortlocale=en_GB,
    sorting=ydnt,
    natbib=true,
    doi=true,
    bibencoding=latin1,
    defernumbers=true]{biblatex}
\DeclareLanguageMapping{english}{english-apa}

\addbibresource{\jobname.bib}

\newbox\savenamebox

\newcount\emphcount
\emphcount 0\relax

\newbibmacro*{name:bemph}[2]{%
 \ifnum\emphcount>0%
  \def\do##1{\iffieldequalstr{hash}{##1}{\setbox\savenamebox\hbox\bgroup \listbreak}{}}%
  \dolistloop{\emphnames}%
 \fi%
}

\newbibmacro*{name:eemph}[2]{%
 \ifnum\emphcount>0%
  \ifthenelse{\value{listcount}=\value{listtotal}}%
   {\def\do##1{\iffieldequalstr{hash}{##1}{\egroup\uline{\usebox\savenamebox}\nopunct\listbreak}{}}%
    \dolistloop{\emphnames}\emphcount -1\relax}%
   {\def\do##1{\iffieldequalstr{hash}{##1}{\egroup\uline{\usebox\savenamebox}\listbreak}{}}%
    \dolistloop{\emphnames}}%
 \else%
  \ifthenelse{\value{listcount}=\value{listtotal}}%
   {\emphcount -1\relax}{}%
 \fi%
 \global\advance\emphcount 1\relax%
}

\xpatchbibmacro{name:apa:family-given}{\usebibmacro{name:delim:apa:family-given}{#4#1}}{\usebibmacro{name:delim:apa:family-given}{#4#1}\begingroup\usebibmacro{name:bemph}{#1}{#2}}{}{}
\xapptobibmacro{name:delim}{\begingroup\normalfont}{}{}

\xapptobibmacro{name:apa:family-given}{\usebibmacro{name:eemph}{#1}{#2}\endgroup}{}{}
\xapptobibmacro{name:delim}{\endgroup}{}{}

\newcommand*{\emphnames}{}
\forcsvlist{\listadd\emphnames}{
 {01b588ba4e4ad753feae6c81709fc04b}% 
}

\begin{document}
\nocite{*}

\printbibliography

\end{document}

输出(使用 LuaLaTeX 和 Biber 编译):

在此处输入图片描述


编辑

该文件apa.bbx已随时间发生变化。截至今天(2023 年 4 月),上述 MWE 中的修补行需要替换为以下内容:

\xpatchbibmacro{name:apa:family-given}{%
    \usebibmacro{name:hook}{#4#1}%
}{%
    \usebibmacro{name:hook}{#4#1}\begingroup\usebibmacro{name:bemph}{#1}{#2}%
}{}{}

\xpatchbibmacro{name:apa:family-given}{%
    \usebibmacro{name:hook}{#1}%
}{%
    \usebibmacro{name:hook}{#1}\begingroup\usebibmacro{name:bemph}{#1}{#2}%
}{}{}

\xpatchbibmacro{name:apa:family-given}{%
    \ifdefvoid{#5}{}{%
        \toggletrue{apablx@wantcomma}%
        \addcomma\bibnamedelimd\mkbibnamesuffix{#5}\isdot%
    }%
}{%
    \ifdefvoid{#5}{}{%
        \toggletrue{apablx@wantcomma}%
        \addcomma\bibnamedelimd\mkbibnamesuffix{#5}\isdot%
    }%
    \usebibmacro{name:eemph}{#1}{#2}\endgroup%
}{}{}

相关内容