rangecite.sty 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. %From: reingold@emr.cs.uiuc.edu (Edward M. Reingold)
  2. %Newsgroups: comp.text.tex
  3. %Subject: Re: short form references like [1-4]
  4. %Date: 26 Mar 91 22:44:32 GMT
  5. %Organization: University of Illinois at Urbana-Champaign
  6. %
  7. %In article <880@imec.UUCP> symons@imec.be (Jan Symons) writes:
  8. %
  9. % with LaTeX, the \cite{key1,key2,key3,key4} puts a [1,2,3,4] reference
  10. % in the text. However, what I want to see is more like [1-4].
  11. %
  12. % Can somebody please explain how this can be achieved with LaTeX ?
  13. %
  14. %
  15. %Here is rangecite.sty that we cobbled together for just this purpose:
  16. %
  17. %-----------------------------rangecite.sty---------------------------------
  18. % RANGECITE DOCUMENT-STYLE OPTION FOR ANY STYLE WITH NUMERIC CITATIONS
  19. %
  20. % This redefines the LaTex \@cite macro to gather consecutive citation numbers
  21. % i,i+1,...,j into a range i-j
  22. %
  23. % Nachum Dershowitz (nachum@cs.uiuc.edu)
  24. % Edward M. Reingold (reingold@cs.uiuc.edu)
  25. %
  26. % These macros are in the public domain. You may use them and copy them at
  27. % will, provided you retain the authorship information. We wrote these for
  28. % temporary use in a single paper, so use them at your own risk!
  29. %
  30. % Written 12 Apr 1990
  31. %
  32. %
  33. \newif\if@first%
  34. \newcounter{@@@first}%
  35. \newcounter{@@@state}%
  36. %
  37. % A simple four-state finite automaton:
  38. % State 0: No references in current range
  39. % State 1: One reference in current range
  40. % State 2: Two references in current range
  41. % State 3: More than two references in current range
  42. %
  43. \newcounter{@@@next}%
  44. \newcounter{@@@cur}%
  45. \newcounter{@@@curt}%
  46. \newcommand{\b@@@ENDLIST}{9999}%
  47. \def\@citex[#1]#2{%
  48. \if@filesw\immediate\write\@auxout{\string\citation{#2}}\fi%
  49. \def\@citea{}%
  50. \@cite{%
  51. \@firsttrue%
  52. \setcounter{@@@state}{0}%
  53. \@for\@citeb:=#2,@@ENDLIST\do{%
  54. \@ifundefined{b@\@citeb}%
  55. {{\bf ?}%
  56. \@warning{Citation `\@citeb' on page \thepage \space undefined}}%
  57. % else%
  58. {\setcounter{@@@next}{\csname b@\@citeb\endcsname}%
  59. \ifcase\the@@@state%
  60. % state 0:
  61. \setcounter{@@@state}{1}%
  62. \or% state 1:
  63. \ifnum\value{@@@next}=\value{@@@curt}%
  64. \setcounter{@@@state}{2}%
  65. \setcounter{@@@first}{\value{@@@cur}}%
  66. \else%
  67. \if@first{}\else{,\ }\fi%
  68. \the@@@cur%
  69. \@firstfalse%
  70. \fi%
  71. \or% state 2:
  72. \ifnum\value{@@@next}=\value{@@@curt}%
  73. \setcounter{@@@state}{3}%
  74. \else%
  75. \setcounter{@@@state}{1}%
  76. \if@first{}\else{,\ }\fi%
  77. \the@@@first,\ \the@@@cur%
  78. \@firstfalse%
  79. \fi%
  80. \or% state 3:
  81. \ifnum\value{@@@next}=\value{@@@curt}%
  82. \else%
  83. \setcounter{@@@state}{1}%
  84. \if@first{}\else{,\ }\fi%
  85. \the@@@first-\the@@@cur%
  86. \@firstfalse%
  87. \fi%
  88. \fi%
  89. \setcounter{@@@cur}{\value{@@@next}}%
  90. \setcounter{@@@curt}{\value{@@@cur}}%
  91. \addtocounter{@@@curt}{1}}}}%
  92. {#1}}%