One pair of brackets for each citation

bibtexbracketscitingmultiple-citations

Question

\documentclass{amsart}
\begin{document}
I cite \cite{a,b}.
I also cite \cite{a,b,c}.
\bibliographystyle{plain}
\bibliography{ref}
\end{document}

gives

I cite [1, 2]. I also cite [1, 2, 3].

But I want

I cite [1] and [2]. I also cite [1], [2], and [3].

How can I achieve this?

Note

Because I'm going to upload it to arXiv, I can't use BibLaTeX.

Best Answer

This is a job for expl3:

\documentclass{article}
\usepackage{xparse} % for arXiv submission it is needed

% save the old meaning
\NewCommandCopy{\latexcite}{\cite}

% proceed to redefine
\ExplSyntaxOn

\RenewDocumentCommand{\cite}{om}
 {
  \IfValueTF { #1 }
   {% optional argument, only one cite
    \latexcite[#1]{#2}
   }
   {% no optional argument
    \bw_cite_multiple:n { #2 }
   }
 }

\seq_new:N \bw_cite_multiple_seq

\cs_new_protected:Nn \bw_cite_multiple:n
 {
  \seq_clear:N \bw_cite_multiple_seq
  \clist_map_inline:nn { #1 }
   {% populate the sequence
    \seq_put_right:Nn \bw_cite_multiple_seq { \latexcite{##1} }   
   }
  \seq_use:Nnnn \bw_cite_multiple_seq
   {~and\nobreakspace}  % between two items
   {,~}                 % between the first items
   {,~and\nobreakspace} % between last two
 }

\ExplSyntaxOff

\begin{document}

I cite \cite[p.~1]{a}.

I cite \cite{b}.

I cite \cite{a,b}.

I also cite \cite{a,b , c}.

\begin{thebibliography}{3}

\bibitem{a} First

\bibitem{b} Second

\bibitem{c} Third

\end{thebibliography}

\end{document}

Note that spaces around commas in the list are ignored.

If there is the optional argument only one citation is expected and it's treated as usual. Without the optional argument, the input is split at commas and a sequence is populated, which can be “used” with different separators between items as shown in the comments and the output.

enter image description here

Note: I used a thebibliography environment for simplicity. The code also works with the bibliography built by BibTeX, because the program produces a thebibliography environment in the .bbl file.