每条引文一对括号

每条引文一对括号

问题

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

给出

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

但我想要

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

我怎样才能实现这个目标?

笔记

因为我要将其上传到 arXiv,所以不能使用 BibLaTeX。

答案1

这是一份工作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}

请注意,列表中逗号周围的空格将被忽略。

如果有可选参数,则只需要一个引用,并且将照常处理。如果没有可选参数,则输入将以逗号分隔,并填充一个序列,该序列可以在项目之间使用不同的分隔符“使用”,如注释和输出中所示。

在此处输入图片描述

注意:我使用thebibliography环境是为了简单起见。该代码也适用于 BibTeX 构建的参考书目,因为程序thebibliography在文件中生成了一个环境.bbl

答案2

将以下几行添加到你的序言中。

\let\citeorig\cite
\def\cite#1{\citeA#1,\relax,}
\def\citeA#1,{\citeorig{#1}\citeB}
\def\citeB#1,{\ifx\relax#1\let\tmp\relax\else\def\tmp{\citeC{#1}}\fi\tmp}
\def\citeC#1#2,{\ifx\relax#2\def\tmp{ and \citeorig{#1}}\else\def\tmp{, \citeorig{#1},\citeD{#2}}\fi\tmp}
\def\citeD#1#2,{\ifx\relax#2\def\tmp{ and \citeorig{#1}}\else\def\tmp{ \citeorig{#1},\citeD{#2}}\fi\tmp}

在此处输入图片描述

此输出是运行的结果

pdflatex test
bibtex test
pdflatex test
pdflatex test

test.tex在包含的文件上

\documentclass{amsart}
\let\citeorig\cite
\def\cite#1{\citeA#1,\relax,}
\def\citeA#1,{\citeorig{#1}\citeB}
\def\citeB#1,{\ifx\relax#1\let\tmp\relax\else\def\tmp{\citeC{#1}}\fi\tmp}
\def\citeC#1#2,{\ifx\relax#2\def\tmp{ and \citeorig{#1}}\else\def\tmp{, \citeorig{#1},\citeD{#2}}\fi\tmp}
\def\citeD#1#2,{\ifx\relax#2\def\tmp{ and \citeorig{#1}}\else\def\tmp{ \citeorig{#1},\citeD{#2}}\fi\tmp}
\begin{document}
I cite \cite{a,b}.
I also cite \cite{a,b,c}.
And I cite \cite{a,b,c,d}.
And even \cite{a,b,c,d,e}.
And again \cite{a}.
\bibliographystyle{plain}
\bibliography{ref}
\end{document}

以及ref.bib包含

@Misc{a,
  key = {a},
  title = {The A Report},
}
@Misc{b,
  key = {b},
  title = {The B Report},
}
@Misc{c,
  key = {c},
  title = {The C Report},
}
@Misc{d,
  key = {d},
  title = {The D Report},
}
@Misc{e,
  key = {e},
  title = {The E Report},
}

相关内容