# Modify smartcite from biblatex to change behaviour based on prenote

I have a question about the smartcite command from biblatex.

I redefined it in the following way:

\documentclass{article}
\usepackage[style=alphabetic, autocite=footnote]{biblatex}

\DeclareCiteCommand{\smartcite}[\iffootnote\mkbibparens\mkbibfootnote]
{\usebibmacro{prenote}}
{\usebibmacro{citeindex}%
\printtext[brackets]{\usebibmacro{cite} \usebibmacro{postnote}}}
{\multicitedelim}
{}

\DeclareMultiCiteCommand{\smartcites}
[\iffootnote\mkbibparens\mkbibfootnote]{\smartcite}{\multicitedelim}

\DeclareAutoCiteCommand{footnote}[f]{\smartcite}{\smartcites}

\begin{filecontents*}{bibliography.bib}
@BOOK{ABC,
author = {Cornelisse, J. W. and Schöyer, H. Ferry R. and Wakker, Karel F.},
title = {Rocket Propulsion and Spaceflight Dynamics},
year = {1979},
publisher = {Pitman},
}
\end{filecontents*}

\bibliography{bibliography.bib}

\begin{document}
Normal text\footnote{Some text \autocite[][20]{ABC}}.}
\end{document}

With this I get a footnote citation within the normal text and a parenthesized citation in a footnote when using the \autocite command.

The problem is when I use e.g. \footnote{Some text \autocite[][20]{ABC}}.} I get as result Some text ([ABC, S. 20]). but what I would like to see is Some text [ABC, S. 20]. when I don't give a prenote. But when there is a prenote like \footnote{Some text \autocite[see][20]{ABC}}.} I would like to get Some text (see [ABC, S. 20]). as a result.

I tried to redefine the \smartcite command with:

\DeclareCiteCommand{\smartcite}[\iffootnote{\ifthenelse{\iffieldundef{prenote}}{\textnormal}{\mkbibparens}}{\mkbibfootnote}]
{\usebibmacro{prenote}}
{\usebibmacro{citeindex}%
\printtext[brackets]{\usebibmacro{cite} \usebibmacro{postnote}}}
{\multicitedelim}
{}

but it seems like the \ifthenelse command has no effect.

Has anybody an idea how to fix it?

Regards

-
It seems as though the \iffieldundef{prenote} is not available in the wrapper argument of \DeclareCiteCommand. –  moewe Mar 30 '14 at 13:32

\iffieldundef{prenote} does not seem to be available to biblatex in the wrapper argument to \DeclareCiteCommand, so we have to delay the test until it is possible.

This can be done as follows

\newtoggle{weareinfootnote}
\settoggle{weareinfootnote}{false}

\newbibmacro*{smartprenote}{%
\iffieldundef{prenote}
{}
{\iftoggle{weareinfootnote}
{\bibopenparen}%
{}%
\printfield{prenote}%
\setunit{\prenotedelim}}}

\DeclareCiteCommand{\smartcite}[\iffootnote{\settoggle{weareinfootnote}{true}}{\settoggle{weareinfootnote}{false}\mkbibfootnote}]
{\usebibmacro{smartprenote}}
{\usebibmacro{citeindex}%
\printtext[brackets]{\usebibmacro{cite}\usebibmacro{postnote}}}
{\multicitedelim}
{\ifboolexpr{not test {\iffieldundef{prenote}} and test {\iftoggle{weareinfootnote}}}
{\bibcloseparen}%
{}}

smartprenote prints an opening parenthesis additionally to the prenote if it is in a \footnote. Finally in the "postcode" section, we close the parentheses if a prenote is present in a footnote. We cannot use the standard \iffootnote test in the citation command since it will always yield true (if it was called in a footnote it obviously does, while if it was called in-text, we start a footnote ourselves resulting in \iffootnote evaluating to true), so we define a toggle weareinfootnote that checks whether we were in a footnote before the command was issued.

Note that this might go horribly wrong with multiple citations or \smartcites code.

\documentclass{article}
\usepackage[style=alphabetic, autocite=footnote]{biblatex}

\newtoggle{weareinfootnote}
\settoggle{weareinfootnote}{false}

\newbibmacro*{smartprenote}{%
\iffieldundef{prenote}
{}
{\iftoggle{weareinfootnote}
{\bibopenparen}%
{}%
\printfield{prenote}%
\setunit{\prenotedelim}}}

\DeclareCiteCommand{\smartcite}[\iffootnote{\settoggle{weareinfootnote}{true}}{\settoggle{weareinfootnote}{false}\mkbibfootnote}]
{\usebibmacro{smartprenote}}
{\usebibmacro{citeindex}%
\printtext[brackets]{\usebibmacro{cite}\usebibmacro{postnote}}}
{\multicitedelim}
{\ifboolexpr{not test {\iffieldundef{prenote}} and test {\iftoggle{weareinfootnote}}}
{\bibcloseparen}%
{}}