变量作为命令的输入

变量作为命令的输入

我想在参考书目中突出显示特定作者,并根据此处给出的答案提供工作代码:https://tex.stackexchange.com/a/73246

我正在使用biber并选择使用名称哈希来指示应突出显示的作者。我想将哈希存储在变量中;但是,当我使用该变量作为输入时,代码不再起作用。

以下是代码示例

  • 工作方法 - 其中明确包含名称哈希
  • 不起作用的方法 - 将哈希值存储在变量中

我认为解决方案并不复杂,但我无法解决!


% -- bib file --------------------------------------------------------
\begin{filecontents}{\jobname.bib}
@InProceedings{identifier1,
  Title                    = {Some Awesome Title},
  Author                   = {Author, Some N. and Another Author},
  Booktitle                = {Some Book about the Future},
  Year                     = {2042},
  Pages                    = {1--42}
}

@InProceedings{identifier2,
  Title                    = {Some So-So Title},
  Author                   = {First Author and Author, Some N. and Third Author},
  Booktitle                = {An okay Booktitle},
  Year                     = {2000},
  Pages                    = {1--100}
}
\end{filecontents}
% --------------------------------------------------------------------

\documentclass{article}

\usepackage[
    backend=biber,
    style=authoryear-comp,
    uniquename=false,
    uniquelist=false,
    dashed=false,
    maxnames=99,
    giveninits=true,
    sorting=ynt,
    sortcites=true,
    hyperref=true,
    backref=true,
    backrefstyle=three,
]{biblatex}
\addbibresource{\jobname.bib}

% -- Define switches to turn bold on/off -----------------------------
\newif\ifBoldAuthor
\BoldAuthorfalse
\def\BoldAuthor{\BoldAuthortrue}
\def\NormalAuthor{\BoldAuthorfalse}
% --------------------------------------------------------------------

% -- Works -----------------------------------------------------------
\newbibmacro*{name:bold}[2]{
    \ifBoldAuthor
    \iffieldequalstr{hash}{78bd3199da266f516ab37d1ae346acd2}{\bfseries}{}%
    \fi
}
% --------------------------------------------------------------------

% -- Does not work -------------------------------------------
%% Hash of author name to make bold
%\def\authorhash{78bd3199da266f516ab37d1ae346acd2}
%
%\newbibmacro*{name:bold}[2]{
%   \ifBoldAuthor
%       \iffieldequalstr{hash}{\authorhash}{\bfseries}{}%
%   \fi
%}
% --------------------------------------------------------------------

% --------------------------------------------------------------------
\usepackage{xpatch}
\xpretobibmacro{name:family}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:given-family}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:family-given}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:delim}{\begingroup\normalfont}{}{}

\xapptobibmacro{name:family}{\endgroup}{}{}
\xapptobibmacro{name:given-family}{\endgroup}{}{}
\xapptobibmacro{name:family-given}{\endgroup}{}{}
\xapptobibmacro{name:delim}{\endgroup}{}{}
% --------------------------------------------------------------------

\begin{document}
\nocite{*}

\BoldAuthor{}
\printbibliography
\NormalAuthor{}

\printbibliography

\end{document}

答案1

如图所示薛定谔的猫回答\iffieldequalstr{<field>}{<string>}实际上期望第二个参数为字符串,而不是扩展为字符串的宏。对于您的定义,\authorhash可以定义一个新的\iffieldequalsdefstring{<field>}{<macro>},它将宏扩展一次以获取字符串(通常我更喜欢用 s 只扩展必要的部分,\expandafter而不是用 一直扩展\edef,因为\expandafters 万一想要扩展的宏包含一些在更深的扩展级别上不可扩展的内容,而这对于当前的任务来说并不重要, 的优点\edef是您不需要事先知道宏结构;但在这种情况下,这两种方法都应该同样安全,因为\authorhash应该只包含0-9a-f)。

\def\authorhash{78bd3199da266f516ab37d1ae346acd2}

\makeatletter
\newcommand*{\iffieldequalstr@swap}[2]{\iffieldequalstr{#2}{#1}}

\newcommand*{\iffieldequalsdefstring}[2]{%
  \expandafter\iffieldequalstr@swap\expandafter{#2}{#1}}
\makeatother

\newbibmacro*{name:bold}[2]{%
  \ifBoldAuthor
    \iffieldequalsdefstring{hash}{\authorhash}{\bfseries}{}%
  \fi
}

另一种方法是使用\iffieldequals{<field>}{<macro>},将字段与宏进行比较。几乎直接在这里工作,但不完全是因为哈希字段中的字符的类别代码与您所说的标准类别代码设置不同\def\authorhash{78bd3199da266f516ab37d1ae346acd2},但这并不是什么\detokenize无法解决的问题。

\documentclass{article}

\usepackage[
    backend=biber,
    style=authoryear-comp,
    giveninits=true, uniquename=false,
]{biblatex}

\newif\ifBoldAuthor
\BoldAuthorfalse
\def\BoldAuthor{\BoldAuthortrue}
\def\NormalAuthor{\BoldAuthorfalse}

\edef\authorhash{\detokenize{78bd3199da266f516ab37d1ae346acd2}}
% or if you like that sort of thing
%\expandafter\def\expandafter\authorhash\expandafter{\detokenize{78bd3199da266f516ab37d1ae346acd2}}

\newcommand*{\mkboldifauthorhash}[1]{%
  \ifbool{BoldAuthor}
    {\iffieldequals{hash}{\authorhash}
       {\mkbibbold{#1}}}
       {#1}
    {#1}}

\DeclareNameWrapperFormat{boldifhashinlist}{%
  \renewcommand*{\mkbibcompletename}{\mkboldifauthorhash}%
  #1}

\DeclareNameWrapperAlias{sortname}{default}
\DeclareNameWrapperAlias{default}{boldifhashinlist}

\begin{filecontents}{\jobname.bib}
@InProceedings{identifier1,
  Title                    = {Some Awesome Title},
  Author                   = {Author, Some N. and Another Author},
  Booktitle                = {Some Book about the Future},
  Year                     = {2042},
  Pages                    = {1--42},
}
@InProceedings{identifier2,
  Title                    = {Some So-So Title},
  Author                   = {First Author and Author, Some N. and Third Author},
  Booktitle                = {An okay Booktitle},
  Year                     = {2000},
  Pages                    = {1--100},
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
\nocite{*}

\BoldAuthor
\printbibliography
\NormalAuthor

\printbibliography
\end{document}

两个书目,每个书目都有(相同的)两个条目。第一个书目中“SN 作者”的名称以粗体突出显示,但第二个书目中没有。除此之外,这两个书目完全相同。

編輯使用更优雅的版本来格式化完整名称。\DeclareNameWrapperFormat\mkbibcompletename仅分别在 v3.12 (2018-10-30) 和 v3.13 (2019-08-17) 中可用biblatex。如果您使用的是旧版本的,请参阅编辑历史biblatex

这种新方法当然也可以用于上述答案的第一部分。


请注意,有一些方法可以或多或少自动从 TeX 文档中为您计算哈希值,因此您不必自己查找和处理哈希值。请参阅https://tex.stackexchange.com/a/416416/35864使用 biblatex 突出显示参考书目中的作者,并允许使用参考书目样式对其进行格式化

答案2

我想您需要先扩展宏然后再进行比较。

% -- bib file --------------------------------------------------------
\begin{filecontents}{\jobname.bib}
@InProceedings{identifier1,
  Title                    = {Some Awesome Title},
  Author                   = {Author, Some N. and Another Author},
  Booktitle                = {Some Book about the Future},
  Year                     = {2042},
  Pages                    = {1--42}
}

@InProceedings{identifier2,
  Title                    = {Some So-So Title},
  Author                   = {First Author and Author, Some N. and Third Author},
  Booktitle                = {An okay Booktitle},
  Year                     = {2000},
  Pages                    = {1--100}
}
\end{filecontents}
% --------------------------------------------------------------------

\documentclass{article}

\usepackage[
    backend=biber,
    style=authoryear-comp,
    uniquename=false,
    uniquelist=false,
    dashed=false,
    maxnames=99,
    giveninits=true,
    sorting=ynt,
    sortcites=true,
    hyperref=true,
    backref=true,
    backrefstyle=three,
]{biblatex}
\addbibresource{\jobname.bib}

% -- Define switches to turn bold on/off -----------------------------
\newif\ifBoldAuthor
\BoldAuthorfalse
\def\BoldAuthor{\BoldAuthortrue}
\def\NormalAuthor{\BoldAuthorfalse}
% --------------------------------------------------------------------

% -- Works -----------------------------------------------------------
% \newbibmacro*{name:bold}[2]{
%     \ifBoldAuthor
%     \iffieldequalstr{hash}{78bd3199da266f516ab37d1ae346acd2}{\bfseries}{}%
%     \fi
% }
% --------------------------------------------------------------------

% -- also works -------------------------------------------
%% Hash of author name to make bold
\edef\authorhash{78bd3199da266f516ab37d1ae346acd2}

\newbibmacro*{name:bold}[2]{
  \ifBoldAuthor
      \edef\temp{\noexpand\iffieldequalstr{hash}{\authorhash}{\noexpand\bfseries}{}}%
      \temp
  \fi
}
% --------------------------------------------------------------------

% --------------------------------------------------------------------
\usepackage{xpatch}
\xpretobibmacro{name:family}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:given-family}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:family-given}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:delim}{\begingroup\normalfont}{}{}

\xapptobibmacro{name:family}{\endgroup}{}{}
\xapptobibmacro{name:given-family}{\endgroup}{}{}
\xapptobibmacro{name:family-given}{\endgroup}{}{}
\xapptobibmacro{name:delim}{\endgroup}{}{}
% --------------------------------------------------------------------

\begin{document}
\nocite{*}

\BoldAuthor{}
\printbibliography
\NormalAuthor{}

\printbibliography

\end{document}

在此处输入图片描述

相关内容