# \cite that tolerates whitespace?

Is there a package that provides a variant of the \cite command that tolerates arbitrary whitespace? That is, I would like to be able to write something like

\cite{foo, bar, baz}


or

\cite{
foo, bar, baz,
baf, bah, bam
}


without any ill effect.

To keep things interesting, it should work flawlessly with other commonly used packages such as cite, natbib, and hyperref, and it should also work without those packages.

Edit: Examples...

test.bib:

@MISC{bar,
author={Bar Bar},
title={Bar},
year={1988}
}

@MISC{foo,
author={Foo Foo},
title={Foo},
year={1988}
}


test1.tex:

\documentclass{article}
\usepackage[numbers,sort&compress]{natbib}

\begin{document}
\cite{foo, bar }.

\bibliographystyle{plain}
\bibliography{test}
\end{document}


Here pdflatex test1 works OK but bibtex test1 gives the following error:

White space in argument---line 2 of file test1.aux
: \citation{foo,bar
:                   }
I'm skipping whatever remains of this command


test2.tex:

\documentclass{article}

\begin{document}
\cite{foo, bar }.

\bibliographystyle{plain}
\bibliography{test}
\end{document}


Again, pdflatex test2 works OK but bibtex test2 produces the following error:

White space in argument---line 3 of file test2.aux
: \citation{bar
:               }
I'm skipping whatever remains of this command


test3.tex:

\documentclass{article}
\usepackage{cite}

\begin{document}
\cite{foo, bar }.

\bibliographystyle{plain}
\bibliography{test}
\end{document}


This seems to work fine! But naturally it has all the side-effects of the cite package as well, which isn't always compatible with other packages.

-
As I recall, the only place it doesn't tolerate whitespace is before the closing }. –  TH. Oct 24 '10 at 20:43
@TH.: Thanks, I have never investigated exactly where it tolerates and where it doesn't; I have just noticed that sometimes something fails badly if I have whitespace in \cite. –  Jukka Suomela Oct 24 '10 at 21:06

Sorry if I'm being stupid, but for me the simple answer is: It works if I use the cite package. (This works flawlessly with cite, but it doesn't work without cite ;-).)

-
Gah! I knew there had to be something much simpler! In fact loading either cite or natbib both solve the problem. –  Juan A. Navarro Nov 4 '10 at 12:55
Really?? Whitespace problems with \cite and natbib was what actually inspired me to post this question! –  Jukka Suomela Nov 4 '10 at 13:27
If you have a minimal example please add it to your question. The small example I tried seems to work fine. –  Juan A. Navarro Nov 4 '10 at 13:31
Sure, I have clearly been too lazy with the formulation of the question and doing my homework, I'll try to put together some examples soon. –  Jukka Suomela Nov 4 '10 at 13:48
You're right, it doesn't work with natbib. I just changed cite to natbib and ignored all the compilation errors when running for the second time :S. Anyway, I know that my alternative solution is not nice, but at least it does work with natbib. –  Juan A. Navarro Nov 4 '10 at 20:32

So I decided to give it a go. The problem seems to be with any spaces after each individual entry and before the , or the closing }. If you can avoid spaces there then that's probably the best thing to do.

If you really insist in being able to have spaces anywhere in the list, then I was able to come up with the following very hackish solution.

\makeatletter
\def\tok@scan#1{%
\ifx#1\relax
\let\tok@next\relax
\else
\edef\my@list{\my@list#1}%
\let\tok@next\tok@scan
\fi
\tok@next
}
\newcommand{\@strip}[2]{%
\def\my@list{}\tok@scan#2\relax\let#1\my@list}
\newcommand{\Cite}[1]{\@strip\@args{#1}\cite\@args}
\makeatother


Which allows you then to write

\Cite{  paper1  ,  paper2,  paper3  }


The short explanation is that \@strip will take two arguments: a command name (\@args in the example) and some arbitrary argument. It will then strip away all spaces in the argument and store the result in the command you provided, then you can use \@args as a valid argument of, say, \cite.

Of course, if you use natbib or other packages for citations you will have to define corresponding macros such as \Citet and \Citep with appropriate interfaces (e.g. optional arguments, etc.).

I really hope someone could come up with something better/easier.

-