字母(而不是数字)引用和 natbib 的压缩选项

字母(而不是数字)引用和 natbib 的压缩选项

不久前,您对以下帖子给出了非常有用的答案:

需要将引用格式从 [1] 更改为 (a)

我有一个关于你回答的后续问题。当我实现你的 TeX 时,一切都运行正常,[compress]但是cite包停止工作。也就是说,我得到

一些文本(a、b、c、d)

代替

一些文字(广告)

我怎样才能恢复该选项的功能[compress]

答案1

修改后的答案:

我已经开发出一种更紧凑的解决方案来解决这个问题,我认为它比原始答案更通用(我保留了原始答案,以防我过度解读这个答案)。它相当于natbib.sty通过以下两种方式进行修补:

  1. 它用 替换所有出现的\svcitenumfont{}\svcitenumfont{\alphalph{}}并且

  2. \@lbibitem在一个地方进行修补。

\alphalph已编辑,通过使用宏来捕获编译过程中的第 1 次传递中的空参数\xalphalph

此修复所需的代码如下:

\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

这些更改产生的结果与我原来的答案相同。此外,已确认当compress和/或numbers包选项打开或关闭时,它们可以工作。以下是 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}

在此处输入图片描述

原始答案:

通过反复试验,我发现了natbib.sty这个 MWE 的三个地方,其中(在原始解决方案中)宏\NAT@num被替换,从而\char\numexpr\NAT@num+96\relax有效地将“1”变成“a”,将“2”变成“b”等等。如果设置了不同的包选项,则可能有更多地方需要进行这种更改(困难在于弄清楚是哪些地方)。

针对此 MWE 修改的三个包宏包括\NAT@citea@mbox\def@NAT@last@yr\@lbibitem。以下是 MWE。

编辑:根据 Werner 的建议使用该alphalph包,如果有超过 26 个引用,则将\usepackage{alphalph}改为针对受影响的位置\NAT@num进行更改。\alphalph{\NAT@num}

\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}

相关内容