[Tex/LaTex] Macro for formatting names (initials or full name)

biblatexcitingmacros

I want to write full names of authors in the LaTeX source, but assign different roles to them, and then via macro decide whether to print the full first name or just initials:

In the work of \artist{Iannis Xenakis}, as analysed by \researcher{John Doe}.

I think in the end I want as output:

In the work of Iannis Xenakis, as analysed by J Doe.

But I might want J. Doe or John Doe or just Doe — and I want to keep this decision open, therefore the macros…

I am using biblatex, but I don't want to use \citeauthor because often the mentions are not directly related to a particular paper cited. Nevertheless, maybe I can reuse some macros that come with biblatex?


Here is my idea: I see that \DeclareNameFormat might provide a good way to define custom formats. But how would a macro using such a format to insert the name into the text look like? Might I use \citename{⟨key⟩}[⟨format⟩]{⟨name list⟩}? I understand that name list would be my source name as above, but what should I use as key?

Best Answer

You could add dummy bibentries in your bib file (and add options = {dataonly=true}, to ensure that they are not included in the bibliography or used for label creation). Based on that, you can use either \citename as suggested in your question or for convenience create a custom macro (say, \formatname) that will save you specifying author as required name list.

\documentclass{article}

\usepackage{biblatex}

\DeclareNameFormat{firstinits-last}{%
  \usebibmacro{name:first-last}{#1}{#4}{#5}{#7}%
  \usebibmacro{name:andothers}}

\DeclareNameAlias{artist}{first-last}
\DeclareNameAlias{researcher}{firstinits-last}

\newcommand*{\formatname}[2]{%
  \citename{fn#1}[#2]{author}%
}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@misc{Doe12,
  author = {Doe, John},
  year = {2012},
  title = {A macro for formatting names},
}
@misc{fnDoe,
  options = {dataonly=true},
  author = {Doe, John},
}
\end{filecontents}

\addbibresource{\jobname.bib}

\nocite{*}

\begin{document}

\formatname{Doe}{artist}

\formatname{Doe}{researcher}

\printbibliography

\end{document}

enter image description here