如何在使用 natbib 和 bibtex 时在每个引用项(数字)之前和之后添加命令

如何在使用 natbib 和 bibtex 时在每个引用项(数字)之前和之后添加命令

我正在写一篇论文,其中我使用 natbib 和 bibtex 作为引用,它们显示为带有方括号的编号项目:例如 [1],[2,3,4]

以下是我目前拥有的 MWE:

\documentclass[10pt]{article}
\setlength\parindent{0pt}
\usepackage{hyperref}
\usepackage[square,numbers]{natbib}

\begin{document}    

A single citation: \cite{M} \\
Multiple citations: \cite{M,K,T} \\
Page ref. example: \citep[p. 10]{B} \\
As shown by \citet{M,K} or also by \citet{B} \\

\begin{thebibliography}{9}
    \bibitem[Me(2006)]{M} Me: My book related to the problem (2006), 145 p.
    % The new line is important!

    \bibitem[Karl(2005)]{K} Karl: Karl's paper published in some minor proceedings, 
    a local conferrence organized by his university (2005), 23--25.
    % The new line is important!

    \bibitem[Test(2013)]{T} Test. A test paper, 
    Conf. of testing (2013), 99.
    % The new line is important!

    \bibitem[Someone(2013)]{B} Someone, a nice book, 
    A good publisher (2013).
    % The new line is important!

\end{thebibliography}
\end{document}

除了参考部分之外,这还会生成以下输出:

A single citation: [1]
Multiple citations: [1, 2, 3]
Page ref. example: [4, p. 10]
As shown by Me [1], Karl [2] or also by Someone [4]

考虑到增加了将鼠标悬停在每个参考项目上时显示包含书目信息的弹出窗口的功能(参见此处的描述),我正在寻找一种方法来修改引用命令,以便通过添加此弹出功能精美工具提示包裹。

对于我引用单个参考的命令,这很容易:

\let\oldcite\cite
\renewcommand{\cite}[1]{\tooltip{\oldcite{#1}}{#1}}

但是,当我在一个 cite 命令(或 citet 等)中引用多个项目时,我的方法就失败了。我需要的是访问生成每个单独参考文献的命令(即编号本身以及指向参考文献部分的相应链接)并进行相应修改,即

\let\oldCommandInQuestion\CommandInQuestion
\renewcommand{\CommandInQuestion}{\tooltip{\oldCommandInQuestion{#1}}{#1}}

其中 \CommandInQuestion 是生成方括号内每个数字的命令,并将 citekey 作为第一个参数传递。如果这真的存在?!?

我一直试图解读里面的东西natbib.sty但没有成功。此外,我还在研究电子工具箱关于如何修改某些命令的包,但我的主要问题是我不知道哪个是所讨论的命令......如果它存在的话。

非常感谢您的意见!

极好的

PS:我正在寻找针对我的配置(natbib 和 bibtex)的特定解决方案。

答案1

在找到解决方案后,我可以部分解决我的问题fancytooltips.sty包(大约 172 行)。我的解决方案略有修改(不是使用 \tooltip*{} 而是 \tooltip{}),因为另一个没有正确链接数字:

\makeatletter
\def\NAT@citea@mbox{%
    \@citea\mbox{\tooltip{\NAT@hyper@{{\citenumfont{\NAT@num}}}}{fancy:cite:\@citeb}}}
\makeatother

但是,这仅在使用 \cite 或 \citep 命令引用时才有效,而使用 \citet 命令则无效……

编辑:这似乎效果更好,因为它也适用于 \citet 和其他命令:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\NAT@hyper@}%
    {#1}%   
    {\tooltip{#1}{fancy:cite:\@citeb\@extra@b@citeb}}%      
    {}{}
\makeatother

然而,问题仍然存在,参考数字不再可点击。如果我使用\tooltip*代替,\tooltip它再次可点击,但执行鼠标悬停的区域非常小。

编辑2:最后,为了使按钮(插入鼠标悬停弹出功能)可点击并指向正确的位置(在参考书目部分),我做了以下操作(见下面的代码)。它基本上修改了精美工具提示通过更改按下按钮时执行的 javascript 操作,此包会添加到 PDF 中。单击鼠标时,它会跳转到参考书目中的相应参考资料(this.gotoNamedDest("cite.[ReplaceThisWithCiteKey]";))。虽然肯定有更优雅的解决方案,但在我的特定情况下,它运行良好。

\makeatletter
\patchcmd{\tooltip@NoStar}%
    {{\fancy@a}{\dp0}}% 
    {{\fancy@a}{\dp0}{#2}}%     % passes tooltip key as additional argument
    {}{}
\def\fancy@pushButton#1#2#3#4#5#6#7{%
    \lower #6\hbox to 0 pt{\hss\expandafter\pushButton\expandafter[\fancy@tooltip@options{#1}{#2}\A{\JS{%
                var str = "#7"; this.gotoNamedDest(str.replace("fancy:cite:", "cite."));    % javascript
        }}]{#3}{#4}{#5}}}%
\makeatother

相关内容