0

I would like to edit a custom cite command to check whether or not a prenote is specified.

At the moment, it looks like this:

\DeclareCiteCommand{\MyCommand}
{
    \usebibmacro{prenote} %
}
{
    some stuff %
}
{}
{
    \usebibmacro{postnote} %
    \adddot %
}

Is it possible to check the presence of an argument and change the precode accordingly? Like print A if there is no argument and B if an argument is specified.

I have a second problem related to this: when I use \MyCommand[argument]{resource}, the argument gets printed at the end of the command just before the dot, as a postnote. Is there a way to force LaTeX to consider that a single argument should be considered as a prenote and thus printed at the beginning?

Dunno
  • 329

1 Answers1

1

Within \DeclareCiteCommand and friends the prenote and postnote arguments can be accessed as if they were fields from the .bib file. So you can test for the presence or absence of prenote with \iffieldundef.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[backend=biber, style=authoryear]{biblatex}

\usepackage{xcolor} \DeclareFieldFormat{red}{\textcolor{red}{#1}} \DeclareCiteCommand{\MyCommand} {\iffieldundef{prenote} {\printtext[red]{replacement for prenote}% \setunit{\prenotedelim}} {\usebibmacro{prenote}}} {\printtext{something here}} {\multicitedelim} {\usebibmacro{postnote}}

\addbibresource{biblatex-examples.bib}

\begin{document} \MyCommand{sigfridsson}

\MyCommand[post]{sigfridsson}

\MyCommand[pre][post]{sigfridsson}

\MyCommand[pre][]{sigfridsson}

\printbibliography \end{document}

replacement for prenote something here
replacement for prenote something here, post
pre something here, post
pre something here

moewe
  • 175,683
  • Thank you. Why do you use setunit? Is there a way to consider a single argument as prenote? – Dunno Jul 12 '23 at 10:34
  • @Dunno With \setunit we make use of biblatex's punctuation tracker, which means that undesirable double punctuation and punctuation clashes are avoided. See for example https://tex.stackexchange.com/q/409148/35864. – moewe Jul 12 '23 at 21:04
  • It's not easily possible to have biblatex treat a single argument as prenote (without messing with the internal implementation of the cite commands, at least). A single argument is considered a postnote. But you can test if there is a postnote and no prenote and just pretend it is a prenote in that case. If you need help with that open a new question (preferably with a usable example document and a precise description of what you want to achieve). – moewe Jul 12 '23 at 21:07