2

I am trying to find out whether two references have the same author with natbib. But I seem to be unable to get basic string comparisons working. Without a deeper understanding, I have tried everything I could find (xstring, \detokenize, \expandafter\relax, ...). Nothing really works. I would be so glad if someone could fix my Test function.

\documentclass{article}
\usepackage{natbib}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
    @article{ArticleX, author = "AuthorA", year = "2000"}
    @article{ArticleY, author = "AuthorA", year = "2010"}
    @article{ArticleZ, author = "AuthorB", year = "2010"}
\end{filecontents}
\newcommand{\Test}[2]{
    \ifx\citeauthor{#1}\citeauthor{#2}
        Yes!
    \else
        No!
    \fi
}
\begin{document}
    Oh, \cite{ArticleX} and \cite{ArticleZ} don't have the same author? \Test{ArticleX}{ArticleZ} Ok!

    But \cite{ArticleX} and \cite{ArticleY} surely have! \Test{ArticleX}{ArticleY} No, either??

    \bibliographystyle{unsrtnat}
    \bibliography{\jobname}
\end{document}
bers
  • 5,404

1 Answers1

0

This works:

% https://tex.stackexchange.com/questions/8981/
\newcommand{\ignoreoutput}[1]{\setbox0\vbox{\everypar{}#1}}
\makeatletter
\newcommand{\Test}[2]{
    \ignoreoutput{\citeauthor{#1}}
    \edef\NumberOne{\NAT@name}
    \ignoreoutput{\citeauthor{#2}}
    \edef\NumberTwo{\NAT@name}
    \ifx\NumberOne\NumberTwo
        Yes!
    \else
        No!
    \fi
}

I still welcome more elegant answers, but this solves my problems.

bers
  • 5,404