4

I want to highlight a specific author in a bibliography and have working code based on the answer given here: https://tex.stackexchange.com/a/73246.

I am using biber and have chosen to use the name hash to indicate the author that should be highlighted. I would like to store the hash in a variable; however, when I use the variable as input the code no longer works.

Here is the code example showing

  • the working method - where the name hash is included explicitly
  • the non-working method - where the hash is stored in a variable

I assume the solution isn't complicated, but I cannot work it out!


% -- 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}
5

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 \expandafters instead of expanding all the way with \edef, since \expandafters 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}

Two bibliographies with (the same) two entries each. The name "S. N. Author" is highlighted in bold in the first bibliography, but not in the second. Apart from that the two bibliographies are identical.

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.

1
  • A simple solution! Thank you for the explanation too. – Haydon Mar 14 '20 at 1:16
5

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.