Variable as input for command
I guess you need to expand the macro before you compare it.
% -- 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}
As shown in Schrödinger's cat's answer, \iffieldequalstr{<field>}{<string>}
really expects a string in the second argument and not a macro that expands to a string. For your definition of \authorhash
one could define a new \iffieldequalsdefstring{<field>}{<macro>}
, that expands the macro once to obtain a string (usually I prefer to expand only as much as necessary with \expandafter
s instead of expanding all the way with \edef
, since \expandafter
s in case the macro one wants to expand contains something unexpandable at a deeper level of expansion that is not interesting to the task at hand, the advantage of \edef
is that you don't need to know the macro structure beforehand; but in this case both methods should be equally safe, since \authorhash
should only ever contain 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
}
An alternative would be to use \iffieldequals{<field>}{<macro>}
, which compares the field to a macro. That almost works here directly, but doesn't quite because the category codes of the characters in the hash field are different from the standard category code setup you use when you say \def\authorhash{78bd3199da266f516ab37d1ae346acd2}
, but this is nothing a sprinkle of \detokenize
can't resolve.
\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}
edited to use a more elegant version to format complete names. \DeclareNameWrapperFormat
and \mkbibcompletename
are only available in biblatex
v3.12 (2018-10-30) and v3.13 (2019-08-17), respectively. Please refer to the edit history if you are using an older version of biblatex
.
This new method could of course also be used in the first part of the answer above.
Note that there are ways to more or less automatically compute the hash for you from the TeX document, so you don't have to look up and handle the hash yourself. See https://tex.stackexchange.com/a/416416/35864 and Highlight an author in bibliography using biblatex allowing bibliography style to format it.