10

A while back, you provided a very helpful answer to the following post:

Need to change citation format from [1] to (a)

I had a follow-up question regarding your answer. When I implement your TeX, everything works perfectly, but the [compress] option of the cite package stops working. That is, I get

some text (a,b,c,d)

instead of

some text (a-d)

How can I restore the functionality of the [compress] option?

  • 5
    Please consider explaining which of the solutions there you are trying to use. Adding a minimal working example (MWE) to this question would also make it easier to help you. – cfr Oct 21 '14 at 18:02
  • And welcome to TeX.SX! – Adam Liter Oct 22 '14 at 16:05
  • 1
    natbib is a different beast. From p.18 of its manual, "It is impossible to make the cite and natbib packages compatible, since both reimplement \cite from scratch. Instead, I have taken over some of the coding from cite.sty, modifying it for natbib. This coding is activated by including one of the options sort or sort&compress in the \usepackage command." Thus, know that there is a compress option to \usepackage[compress]{natbib}. – Steven B. Segletes Feb 11 '15 at 13:35
  • Please see latest revision, which I believe simplifies the fix and also eliminates the errors on pass 1 of the compile. – Steven B. Segletes Feb 16 '15 at 14:33

1 Answers1

3

REVISED ANSWER:

I have developed a more compact fix to this problem that I think is even more general than the original answer (which is retained below, in case I have over-reached on this answer). It amounts to patching natbib.sty in the following two ways:

  1. It replaces all occurrences of \svcitenumfont{} with \svcitenumfont{\alphalph{}}, and

  2. It patches\@lbibitem in one place.

EDITED to trap null arguments to \alphalph on pass 1 of the compile, by using macro \xalphalph.

The code required for this fix is simply the following:

\usepackage{alphalph}
\usepackage{xpatch}
\def\xalphalph#1{\if\relax#1\relax\else\alphalph{#1}\fi}
\let\svcitenumfont\citenumfont
\renewcommand\citenumfont[1]{\svcitenumfont{\alphalph{#1}}}
\makeatletter
\xpatchcmd{\@lbibitem}{{\NAT@num}}{{\xalphalph{\NAT@num}}}{}{}
\makeatother

These changes produces the same result as my original answer. Additionally, they have been confirmed to work when the compress and/or the numbers package options are turned on or off. Here is the MWE:

\documentclass{article}
\usepackage{xcolor,ifthen,filecontents}
\usepackage[compress,numbers,round]{natbib}
\usepackage{alphalph}
\usepackage{xpatch}
\def\xalphalph#1{\if\relax#1\relax\else\alphalph{#1}\fi}
\let\svcitenumfont\citenumfont
\renewcommand\citenumfont[1]{\svcitenumfont{\alphalph{#1}}}
\makeatletter
\xpatchcmd{\@lbibitem}{{\NAT@num}}{{\xalphalph{\NAT@num}}}{}{}
\makeatother
\begin{filecontents}{mybib.bib}
@misc{A01,
  author = {Author, A.},  year = {2001},  title = {Alpha}}
@misc{A01b,
  author = {Author, A.},  year = {2001},  title = {Alpha TWO}}
@misc{B02,
  author = {Buthor, B.},  year = {2002},  title = {Bravo}}
@misc{C03,
  author = {Cuthor, C.},  year = {2003},  title = {Charlie}}
@misc{D04,
  author = {Duthor, D.},  year = {2004},  title = {Delta}}
\end{filecontents}
\begin{document}
cite \cite{A01, A01b, B02, C03, D04}\par
and \cite{A01} and \cite{A01,D04}
\bibliography{mybib}
\end{document}

enter image description here

ORIGINAL ANSWER:

Through much trial and error, I found three places in natbib.sty for this MWE, in which (in the ORIGINAL solution) the macro \NAT@num gets replaced with \char\numexpr\NAT@num+96\relax which effectively turns "1" into "a", "2" into "b", etc. If different package options are set, there are likely more places where this alteration will be needed (the difficulty is in figuring out which ones).

The three package macros that were revised for this MWE include \NAT@citea@mbox, \def@NAT@last@yr, and \@lbibitem. Here is the MWE.

EDIT: Taking Werner's suggestion to use the alphalph package, in the event that there are more than 26 references, one would instead \usepackage{alphalph} and change \NAT@num into \alphalph{\NAT@num} for the affected locations.

\documentclass{article}
\usepackage{xcolor,ifthen,filecontents}
\usepackage[compress,numbers,round]{natbib}
\usepackage{alphalph}
\makeatletter
\def\NAT@citea@mbox{%
 \@citea\mbox{\NAT@hyper@{{\citenumfont{\alphalph{\NAT@num}}}}}%
}%
\def\def@NAT@last@yr#1{%
 \protected@edef\NAT@last@yr{%
  #1%
  \noexpand\mbox{%
   \noexpand\hyper@natlinkstart{\@citeb\@extra@b@citeb}%
   {\noexpand\citenumfont{\alphalph{\NAT@num}}}%
   \noexpand\hyper@natlinkend
  }%
 }%
}%
\def\@lbibitem[#1]#2{%
  \if\relax\@extra@b@citeb\relax\else
    \@ifundefined{br@#2\@extra@b@citeb}{}{%
     \@namedef{br@#2}{\@nameuse{br@#2\@extra@b@citeb}}%
    }%
  \fi
  \@ifundefined{b@#2\@extra@b@citeb}{%
   \def\NAT@num{}%
  }{%
   \NAT@parse{#2}%
  }%
  \def\NAT@tmp{#1}%
  \expandafter\let\expandafter\bibitemOpen\csname NAT@b@open@#2\endcsname
  \expandafter\let\expandafter\bibitemShut\csname NAT@b@shut@#2\endcsname
  \@ifnum{\NAT@merge>\@ne}{%
   \NAT@bibitem@first@sw{%
    \@firstoftwo
   }{%
    \@ifundefined{NAT@b*@#2}{%
     \@firstoftwo
    }{%
     \expandafter\def\expandafter\NAT@num\expandafter{\the\c@NAT@ctr}%
     \@secondoftwo
    }%
   }%
  }{%
   \@firstoftwo
  }%
  {%
   \global\advance\c@NAT@ctr\@ne
   \@ifx{\NAT@tmp\@empty}{\@firstoftwo}{%
    \@secondoftwo
   }%
   {%
    \expandafter\def\expandafter\NAT@num\expandafter{\the\c@NAT@ctr}%
    \global\NAT@stdbsttrue
   }{}%
   \bibitem@fin
   \item[\hfil\NAT@anchor{#2}{\alphalph{\NAT@num}}]%
   \global\let\NAT@bibitem@first@sw\@secondoftwo
   \NAT@bibitem@init
  }%
  {%
   \NAT@anchor{#2}{}%
   \NAT@bibitem@cont
   \bibitem@fin
  }%
  \@ifx{\NAT@tmp\@empty}{%
    \NAT@wrout{\the\c@NAT@ctr}{}{}{}{#2}%
  }{%
    \expandafter\NAT@ifcmd\NAT@tmp(@)(@)\@nil{#2}%
  }%
}%
\makeatother

\begin{filecontents}{mybib.bib}
@misc{A01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha}
}
@misc{A01b,
  author = {Author, A.},
  year = {2001},
  title = {Alpha TWO}
}
@misc{B02,
  author = {Buthor, B.},
  year = {2002},
  title = {Bravo}
}
@misc{C03,
  author = {Cuthor, C.},
  year = {2003},
  title = {Charlie}
}
@misc{D04,
  author = {Duthor, D.},
  year = {2004},
  title = {Delta}
}
\end{filecontents}
\begin{document}
cite \cite{A01, A01b, B02, C03, D04}

and \cite{A01} and \cite{A01,D04}
\bibliography{mybib}
\end{document}