无需 Bibtex 即可按出现顺序引用多个参考文献

无需 Bibtex 即可按出现顺序引用多个参考文献

这个问题与我之前问的一个问题相关这里。我想按出现的顺序引用参考文献,而不用 Bibtex 和回答确实如此。但是,请考虑一下我有以下参考书目:

\begin{thebibliography}{C}
\bibitem{ref1}{AAAA}
\bibitem{ref2}{BBBB}
\bibitem{ref3}{CCCC}
\bibitem{ref4}{DDDD}
\bibitem{ref5}{EEEE}
\end{thebibliography}

代码\cite{ref2} and \cite{ref3, ref4, ref5}将给出[1] and [2,3,4]我想要的[1] and [2-4]。我如何改变前面答案中给出的代码?请注意,使用\usepackage{cite}将输出[1] and [4-6]不按出现顺序排列的内容。

答案1

实施不使用 Bibtex 按出现顺序对参考书目进行排序天真地假设 的参数\citation始终只有一个键。这对于 的标准实现是一个有效的假设,但对于的\cite实现则不是。cite\cite

以下简单扩展也应处理逗号分隔列表。唯一的区别是引入了 CSV 循环\citation,并将宏的核心移至\citation@i

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{cite}
\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
\newcommand*{\lodbib@citeorder}{}
\newcommand*{\lodbib@notcited}{}% catch entries that were not cited

% macro in aux file
\def\citation{%
  \forcsvlist{\citation@i}}

\def\citation@i#1{%
  \ifinlist{#1}{\lodbib@citeorder}
    {}
    {\listxadd{\lodbib@citeorder}{#1}}}

\let\ltxorig@lbibitem\@lbibitem
\let\ltxorig@bibitem\@bibitem

% save bibitems
\def\@lbibitem[#1]#2#3{%
  \csdef{lodbib@savedlabel@#2}{#1}%
  \@bibitem{#2}{#3}}

\def\@bibitem#1#2{%
  \xifinlist{#1}{\lodbib@citeorder}
    {}
    {\listadd{\lodbib@notcited}{#1}}%
  \csdef{lodbib@savedentry@#1}{#2}}

\renewenvironment{thebibliography}[1]
     {\settowidth\labelwidth{\@biblabel{#1}}}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \section*{\refname}%
      \@mkboth{\MakeUppercase\refname}{\MakeUppercase\refname}%
      \list{\@biblabel{\@arabic\c@enumiv}}%
           {\leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \@openbib@code
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\@arabic\c@enumiv}}%
      \sloppy
      \clubpenalty4000
      \@clubpenalty \clubpenalty
      \widowpenalty4000%
      \sfcode`\.\@m
      \lodbib@biblistloop
      \endlist}

\def\lodbib@biblistloop{%
  \forlistloop{\lodbib@bibitem}{\lodbib@citeorder}%
  \ifdefvoid{\lodbib@notcited}
    {}
    {\forlistloop{\lodbib@bibitem}{\lodbib@notcited}}}

\def\lodbib@bibitem#1{%
  \ifcsundef{lodbib@savedlabel@#1}
    {\ltxorig@bibitem{#1}}
    {\ltxorig@lbibitem[\csuse{lodbib@savedlabel@#1}]{#1}}%
  \csuse{lodbib@savedentry@#1}}
\makeatother


\begin{document}
\cite{ref2} and \cite{ref3, ref4, ref5}

\begin{thebibliography}{C}
\bibitem{ref1}{AAAA}
\bibitem{ref2}{BBBB}
\bibitem{ref3}{CCCC}
\bibitem{ref4}{DDDD}
\bibitem{ref5}{EEEE}
\end{thebibliography}

\cite{ref2}
\end{document}

引文如下:“[1] 和 [2–4]”

相关内容