0

This question is related to a question I asked earlier here. I wanted to cite references in order of appearance without Bibtex and the answer did exactly that. However, consider I have the following bibliography:

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

The code \cite{ref2} and \cite{ref3, ref4, ref5} will give [1] and [2,3,4] and what I want is [1] and [2-4] instead. How can I alter the code given in the previous answer? Note that using \usepackage{cite} will output [1] and [4-6] which is not in the order of appearance.

Lod
  • 167

1 Answers1

1

The implementation in Sorting bibliography in order of appearance without Bibtex naively assumed that the argument of \citation is always only a single key. That is a valid assumption for the standard implementation of \cite, but not for the implementation of cite's \cite.

The following simple extension should also deal with comma-separated lists. The only difference is the introduction of a CSV loop in \citation and the resulting move of the core of the macro to \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}

The citations read "[1] and [2–4]"

moewe
  • 175,683