2

I am trying to define a bespoke citation command which mimics the behaviour of \citep[][]{} but for the case when I am using some citation alias, e.g. when the author is an institution and I want its acronym.

The problem I have is that, when there is no text after the citation, Latex is still displaying a space between the year and the closing parenthesis. I have tried defining a \checkEmpty command based on this post but to no avail.

My code is below:

\documentclass{article}

\usepackage[]{natbib} \usepackage[colorlinks=true,allcolors=blue]{hyperref}

\defcitealias{BCBS2011}{BCBS} \newcommand{\checkEmpty}[1]{ \if\relax\detokenize{#1}\relax{\ignorespaces}\else{#1}\fi } \newcommand{\mycitepalias}[3]{ \citepalias[#2][\citeyear{#1}\checkEmpty{#3}]{#1} }

\begin{document}

\noindent% \mycitepalias{BCBS2011}{}{}\ \mycitepalias{BCBS2011}{with text before}{}\ \mycitepalias{BCBS2011}{with text before}{and after} \bibliography{bibliography.bib}

\end{document}

The output is displayed below. As you can see in the first and second line, there is an undesirable space.

enter image description here

PS: As a bonus, I would like to add a comma after the date in case there is text after the citation.

1 Answers1

3

Your {\ignorespaces} does nothing, because \ignorespaces is anyway followed by }. But it's irrelevant nonetheless.

You are adding spaces by not protecting endlines.

\begin{filecontents*}{\jobname.bib}
@misc{BCBS2011,
  author={{Basel Committee on Bank Supervision}},
  title={Basel {III}: {A} global regulatory framework for
         more resilient banks and banking system},
  year=2011,
}
\end{filecontents*}

\documentclass{article}

\usepackage[]{natbib} \usepackage[colorlinks=true,allcolors=blue]{hyperref}

\defcitealias{BCBS2011}{BCBS} \newcommand{\checkEmpty}[1]{% \if\relax\detokenize{#1}\relax \else \ #1% \fi } \newcommand{\mycitepalias}[3]{% \citepalias[#2][\citeyear{#1}\checkEmpty{#3}]{#1}% }

\begin{document}

\mycitepalias{BCBS2011}{}{}

\mycitepalias{BCBS2011}{with text before}{}

\mycitepalias{BCBS2011}{with text before}{and after}

\bibliographystyle{plainnat} \bibliography{\jobname}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thank you. I had tried adding %... but at the wrong place that is after \relax instead of the very beginning of the command. Cheers. – Daneel Olivaw Feb 29 '24 at 11:51