本地解压引用参考文献(带方括号)

本地解压引用参考文献(带方括号)

我的情况与这个问题描述的类似:解压本地引用参考文献,但我有点复杂。我认为最好是开始一个新问题,而不是在原始问题上发表评论。所以这里是:

问题

我需要在本地解压缩引用的参考文献(就这一次...needs citation [1-5]),这样,我得到的不是 ,而是...needs citation [1,2-5]。请注意,参考文献 1 现在已与主要“参考文献组”分离。请参阅下面的 MWE。

MWE 和尝试

考虑以下 MWE(与上面链接中的 MWE 略有修改,并且下面应用了该链接中的一种解决方案):

\documentclass{article}
\usepackage{cite}
\usepackage{hyperref,cleveref} % I am using these packages as well
\usepackage{filecontents}

% References
\begin{filecontents}{samplebib.bib}
    @article{testref1,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref2,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref3,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref4,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref5,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
\end{filecontents}

\makeatletter % Cite references to be in square brackets, superscript
\renewcommand{\@cite}[1]{\textsuperscript{[#1]}} 
\makeatother


% The accepted solution from the original question - Credit to Steven B. Segletes
% \newcommand\mcite[1]{\expandafter\mcitehelp\noexpand#1&\relax} % (Old)
\newcommand\mcite[1]{\mcitehelp#1&\relax}  % (Updated Code)    
\def\mcitehelp#1&#2\relax{\cite{#1}\ifx\relax#2\relax\else$^,$\mcitehelp#2\relax\fi}

\begin{document}
    This is some text that requires citation
    \mcite{testref1&testref2, testref3, testref4, testref5}. 

    But this is unsatisfactory because I'd prefer it to look like:

    This is some text that requires citation \textsuperscript{[1,\,2--5]}.

    \bibliographystyle{plain}
    \bibliography{samplebib}
\end{document}

快速浏览一下输出: 在此处输入图片描述

我把\hyperref\cleveref包留在了 MWE 中,因为希望您的回答不会影响我使用这些包。

我知道\cite有这个选择,\uncompress但这并不令人满意,因为:

  • 这适用于整个文档。我只需要一次。
  • 它单独列出了所有参考资料([1],[2],[3],[4],[5]),我不想要。我只想要引用应与组隔离。其余的应保持分组。

问题

所以我想我的问题是,我能否获得一些帮助来修改代码,以便我能够实现所需的输出?我对使用 LaTeX 编程还不太熟悉。谢谢!

总结: 我想要一个未压缩的引用参考文献,就像cleveref包允许的\cref{label1, ,label2,label3,label4}普通标签一样,它会给我一个像图片中那样的输出(下面一个)。

答案1

我删除了屏幕截图的 hyperref,但它也可以正常工作。

\documentclass{article}
\usepackage{cite}
%\usepackage{hyperref,cleveref} % I am using these packages as well
\usepackage{filecontents}

% References
\begin{filecontents}{samplebib.bib}
    @article{testref1,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref2,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref3,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref4,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref5,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
\end{filecontents}

\makeatletter % Cite references to be in square brackets, superscript
\renewcommand{\@cite}[1]{\textsuperscript{[#1]}}
\makeatother


\begin{document}
    This is some text that requires citation%
    \begingroup
    \makeatletter    
    \renewcommand{\@cite}[1]{#1}%    
    \textsuperscript{[\cite{testref1},\,\cite{testref2, testref3, testref4, testref5}]}.
    \endgroup

    \bibliographystyle{plain}
    \bibliography{samplebib}
\end{document}

在此处输入图片描述

答案2

我建议您从使用cite引文管理包切换到使用natbib引文管理包。该natbib包应加载选项supersquarecommasort&compress

这允许设置一个新的专用引文宏(\mcitex在下面的代码中调用),它接受两个参数:分隔逗号前后的 bib 条目。每个参数中的条目将像往常一样进行排序和压缩。

在此处输入图片描述

\RequirePackage{filecontents}
\begin{filecontents}{samplebib.bib}
@article{testref1,
   title = {Test title}, journal = {Test Journal},
   author = {Test authors and others}, volume=1, year=2016}
@article{testref2,
   title = {Test title}, journal = {Test Journal},
   author = {Test authors and others}, volume=2, year=2017}
@article{testref3,
   title = {Test title}, journal = {Test Journal},
   author = {Test authors and others}, volume=3, year=2018}
@article{testref4,
   title = {Test title}, journal = {Test Journal},
   author = {Test authors and others}, volume=4, year=2019}
@article{testref5,
   title = {Test title}, journal = {Test Journal},
   author = {Test authors and others}, volume=5, year=2020}
@article{testref6,
   title = {Test title}, journal = {Test Journal},
   author = {Test authors and others}, volume=6, year=2021}

\end{filecontents}

\documentclass{article}
\usepackage[super,square,comma,sort&compress]{natbib}
\bibliographystyle{plain}
\newcommand\mcitex[2]{\textsuperscript{[}\citealp{#1}%
    \textsuperscript{,}\citealp{#2}\textsuperscript{]}}

\usepackage{hyperref,cleveref}
\hypersetup{colorlinks,allcolors=blue} % just for this example
\begin{document}

text\cite{testref1,testref2, testref3, testref4, testref5}

text%
\mcitex{testref1}{testref2, testref3, testref4, testref5}%
\mcitex{testref1, testref2}{testref3, testref4, testref5}%
\mcitex{testref1, testref2, testref3}{testref4, testref5, testref6}

\bibliography{samplebib}
\end{document}

答案3

在这里,使用我之前的回答,并根据您当前的情况进行更改,我做了两件事:

  1. 我没有在修改后的\@cite宏中添加括号,而是这样做:\renewcommand{\@cite}[1]{\textsuperscript{#1}};和

  2. 我直接将括号添加到\mcite宏中,如下所示\newcommand\mcite[1]{$^[$\mcitehelp#1&\relax$^]$}

以下是 MWE:

\documentclass{article}
\usepackage{cite}
%\usepackage{hyperref,cleveref} % I am using these packages as well
\usepackage{filecontents}
% References
\begin{filecontents}{samplebib.bib}
    @article{testref1,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref2,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref3,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref4,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
    @article{testref5,
        title = {Test title},
        journal = {Test Journal},
        author = {Test authors and others},
        volume = {1},
        year = {2016}
    }
\end{filecontents}

\makeatletter % Cite references to be in square brackets, superscript
\renewcommand{\@cite}[1]{\textsuperscript{#1}} 
\makeatother


% The accepted solution from the original question - Credit to Steven B. Segletes
% \newcommand\mcite[1]{\expandafter\mcitehelp\noexpand#1&\relax} % (Old)
\newcommand\mcite[1]{$^[$\mcitehelp#1&\relax$^]$}  % (Updated Code)    
\def\mcitehelp#1&#2\relax{\cite{#1}\ifx\relax#2\relax\else$^,$\mcitehelp#2\relax\fi}

\begin{document}
    This is some text that requires citation
    \mcite{testref1&testref2, testref3, testref4, testref5}. 

    But this is unsatisfactory because I'd prefer it to look like:

    This is some text that requires citation \textsuperscript{[1,\,2--5]}.

    \bibliographystyle{plain}
    \bibliography{samplebib}
\end{document}

在此处输入图片描述

相关内容