我确信我忽略了一些非常明显的东西。但是,为什么这个很好的答案有效,但如果我删除循环,我会收到有关分组不匹配的错误?
考虑以下最小的非工作示例:
\documentclass{article}
\begin{filecontents}{\jobname.bib}
@article{testref,
author = {Miller, Alice and Highlight, Shine and Smith, Bob},
title = {The title},
journal = {The journal},
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
% does not work
\newbibmacro*{name:bbold}[2]{%
\iffieldequalstr{hash}{01b588ba4e4ad753feae6c81709fc04b}%
{\bfseries\setbox\savenamebox\hbox\bgroup\listbreak}{}%
}
\newbibmacro*{name:ebold}[2]{%
\iffieldequalstr{hash}{01b588ba4e4ad753feae6c81709fc04b}%
{\egroup\uline{\usebox\savenamebox}\listbreak}{}%
}
% ===
\xpatchbibmacro{name:given-family}{\usebibmacro{name:delim}{#2#3#1}}{\usebibmacro{name:delim}{#2#3#1}\begingroup\usebibmacro{name:bbold}{#1}{#2}}{}{}
\xapptobibmacro{name:given-family}{\usebibmacro{name:ebold}{#1}{#2}\endgroup}{}{}
\begin{document}
\nocite{*}
\printbibliography
\end{document}
原始代码改为
\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}%
}
\newcommand*{\boldnames}{}
\forcsvlist{\listadd\boldnames}{
{4fcd4bca11ef811f3aef17c792b6ef3e},
{01b588ba4e4ad753feae6c81709fc04b}}
效果很好。但为什么会这样?循环是否以某种方式延迟了扩展,从而使分组不匹配变得无关紧要?我怎样才能得到一个不使用循环的工作示例?
答案1
问题在于,正如评论中所指出的那样(感谢用户691586),是因为我忘记从原始代码中删除\listbreak
宏,这些宏以某种方式用于处理 CSV 列表,但这里不再需要它们。如果没有这些,代码可以正常工作:
\documentclass{article}
\begin{filecontents}{\jobname.bib}
@article{testref,
author = {Miller, Alice and Highlight, Shine and Smith, Bob},
title = {The title},
journal = {The journal},
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]{%
\iffieldequalstr{hash}{01b588ba4e4ad753feae6c81709fc04b}%
{\bfseries\setbox\savenamebox\hbox\bgroup}{}%
}
\newbibmacro*{name:ebold}[2]{%
\iffieldequalstr{hash}{01b588ba4e4ad753feae6c81709fc04b}%
{\egroup\uline{\usebox\savenamebox}}{}%
}
\xpatchbibmacro{name:given-family}{\usebibmacro{name:delim}{#2#3#1}}{\usebibmacro{name:delim}{#2#3#1}\begingroup\usebibmacro{name:bbold}{#1}{#2}}{}{}
\xapptobibmacro{name:given-family}{\usebibmacro{name:ebold}{#1}{#2}\endgroup}{}{}
\begin{document}
\nocite{*}
\printbibliography
\end{document}