Uncompressing cite references locally (with square brackets)

I removed hyperref for the screenshot, but it works with it too.

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

enter image description here


I suggest you switch from using the cite citation management package to using the natbib citation management package. The natbib package should be loaded with the options super, square, comma, and sort&compress.

This permits setting up a new dedicated citation macro -- called \mcitex in the code below -- that takes two arguments: bib entries before and after a separating comma. The entries in each of the arguments will be sorted and compressed as usual.

enter image description here

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

Here, using my prior answer, with changes for your current situation, I did two things:

  1. I did not add brackets to the modified \@cite macro, but instead did this: \renewcommand{\@cite}[1]{\textsuperscript{#1}}; and

  2. I added the brackets to the \mcite macro directly, as in \newcommand\mcite[1]{$^[$\mcitehelp#1&\relax$^]$}.

Here is the 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}

enter image description here

Tags:

Bibtex

Citing