1

Consider the following citation style below:

enter image description here

I would like to have it displayed as ''1, and 3-8'' instead of the above. Any suggestions? I want 3 and 8 to be linked to the pages 3 and 8, respectively. Indeed, there is no longer a direct link to pages 4-7.

Addition: I am using the following:

\usepackage[backref=page]{hyperref}  
\usepackage[sort]{natbib}   
\hypersetup{   
    colorlinks=true,         
    allcolors=blue       
} 

\renewcommand*{\backref}[1]{}  
\renewcommand*{\backrefalt}[4]{\%  
\ifcase #1 (Not cited.)\%  
    \or        \footnotesize (Cited on page~#2.)\%  
    \else      \footnotesize (Cited on pages~#2.)\%  
    \fi  
}

Addition (a minimal example): https://www.dropbox.com/sh/nvg52ixdt0h7bkb/AACMv8tsM1C0xGdRspJ300zga?dl=0

6
  • 2
    What about the hyperlinks? From the blue color of the page numbers I guess, you used hyperref's backref option. Is that correct and if so, what behavior do you expect from the hyperlinks? Do you want to keep them and where should the point to?
    – leandriis
    Nov 2 '20 at 15:06
  • That is correct indeed. I have extended my post. I would like to keep things automatic, and I guess that I want to let 3-8 point to 3?
    – Mark
    Nov 2 '20 at 16:18
  • Any suggestions?
    – Mark
    Nov 16 '20 at 16:43
  • 1
    you shouldn't link to a dropbox. A minimal example should be posted here. May 18 at 16:34
  • @UlrikeFischer Thank you for your feedback. The reason why I shared a dropbox link, is because I were not able to upload a .zip file to StackExchange (or at least, I couldn't find how).
    – Mark
    May 18 at 17:00
4
+50

Because you have not provided a LaTeX example and I don't want to prepare such an example, I show you, how to do it at plain TeX level. The hyperlinks are implemented using OpTeX's \hyperlinks in order to show, that it is possible. You can process this example by optex testfile. You can re-define the macro \cpR to create hyperlinks using LaTeX method.

\hyperlinks\Blue\Blue
% destinations to previous 30 pages:
\fornum 1..30 \do{\null\label[cp-#1]\wlabel{}\vfil\break} 

% macro \citedpages:
\def\afterfi#1#2\fi{\fi#1}
\newcount\cpnum
\def\citedpages#1{\let\next=\cpA \def\cplist{}\cpnum=-1 \cpA#1,,\end}
\def\cpA#1,{\ifx,#1,\expandafter\cpB \else
   \ifnum#1=\cpnum % do nothing:  3,3 -> 3
   \else \ifnum#1=\numexpr\cpnum+1\relax % 3,4 -> 3-4
         \advance\cpnum by1
   \else \edef\cplist{\cplist -\the\cpnum, #1}% 3,5 -> 3, 5
         \cpnum=#1\relax
   \fi\fi \expandafter\cpA \fi
}
\def\cpB#1\end{\edef\cplist{\cplist-\the\cpnum}%
   \expandafter\cpC\cplist, 0-0,
}
\def\cpC#1-#2, #3-#4, {\ifnum#4=0 and \fi % end game
   \ifnum #2=-1 % starting node, ignored
   \else \ifnum #1=#2 \cpR{#1}\cpD{#4}
         \else \cpR{#1}--\cpR{#2}\cpD{#4}
   \fi\fi 
   \ifnum #4=0 \else \afterfi{\cpC #3-#4, }\fi
}
\def\cpD#1{\ifnum#1=0 \else,\fi}
\def\cpR#1{\pgref[cp-#1]}% OpTeX's hyperlink to the page.

% test:
Cited on pages \citedpages{1,3,4,5,6,7,8,20,21,22,25,27,30}.

\end

The first three lines are OpTeX specific, next lines are pure Plain TeX with exception of the macro \cpR which can be defined as \def\cpR#1{#1} in pure plain TeX (without hyperlinks).

The \citedpages macro works in two steps. In the first step it reads

{1,3,4,5,6,7,8,20,21,22,25,27,30}
 

(in our example) and creates the \cplist macro with the contents:

--1, 1-1, 3-8, 20-22, 25-25, 27-27, 30-30

The second step is done by the line \expandafter\cpC\cplist, 0-0, . It creates desired result:

\cpR{1}, \cpR{3}--\cpR{8}, \cpR{20}--\cpR{22}, \cpR{25}, \cpR{27}, and \cpr{30}

Edit: If you want to use this macro with natbib and with re-definition of \backrefalt then this re-definition can look like:

\renewcommand*{\backrefalt}[4]{%
        \ifcase #1 (Not cited.)%
        \or        \footnotesize (Cited   on   {{#2}}.)%
        \else   
               \def\cplist{}\cpnum=-1
               \expandafter\readcplist #2\backrefxxx{0}%
               \footnotesize (Cited on pages~{{\expandafter\cpA\cplist,,\end}}.)%
        \fi
}
\def\readcplist #1\backrefxxx#2{%
    \ifnum#2=0 \else \edef\cplist{\cplist #2,}\expandafter\readcplist\fi}

But this solution adds the list of pages without hyperlinks. If you want to add hyperlinks, then define \cpR as follows:

\def\cpR#1{\backrefxxx{#1}{}{}}% hyperref backref page links.
8
  • Thank you for your ellaborate answer. I am having a little trouble implementing it. First of all, can I still make use of hyperref and backref as in my post? Or do I have to start over. Also, you give an example where you provide the numbers 1,3,4,5,6,7,8,20,21,22,25,27,30, but I of course want this to happen automatically. As provided in my post, I use backref to let this happen automatically. Should I then combine the two approaches?
    – Mark
    May 18 at 12:36
  • 1
    Yes, you can use LaTeX packages with combination of these macros. Because you didn't show, how these packages are used in your project (and I know nothing about these packages), I have shown the plain TeX solution.
    – wipet
    May 18 at 12:46
  • Okay, clear! I'll try to make it work. I am not really familiar with what OpTeX is. Do I need any particular packages to make this work? TeXstudio says that it does not recognize the following commands: \hyperlinks \fornum \wlabel and \pgref. As a result, I get [cp-1] as text.
    – Mark
    May 18 at 12:57
  • 1
    I have mentioned that first three lines are from OpTeX and the body of \cpR is from OpTeX. You can remove these three lines and replace the \cpR as mentioned above or use a construction from hyperref here. The rest is pure plain TeX, it can be used in LaTeX directly (if you are using LaTeX). You didn't provide whole minimal example what did you exactly do in the place "Cited on pages 1,3,4,5,7" so I cannot start from such example. This is reason, why my example is somewhat different. The second reason is that I never used LaTeX in my TeX projects.
    – wipet
    May 18 at 13:11
  • 1
    OK, I added the implementation to your LaTeX project.
    – wipet
    May 18 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.