# macro for hyperref package so that it returns a result like 1-3,5-7,9,11-12 instead of 1,2,3,5,6,7,9,11,12

I would like to request your help with the following: With the package hyperref I get a lot of backreferences in my bibliography like 1,1,1,2,2,3,5,8,9,10,11,11,11,12,12,13,17,19 Now I wanted to write a macro that turns this result into: 1-3,5,8-13,17,19 if Kind = 0 1,3;2,2;3;5;8;9;10;11,3;12,2;13;17;19 if Kind = 1 where Kind is a variable to have a certain layout according to your choice When I run the file PROBEERTEST.TEX the above examples are correct but the bottom one has a lot of 'I assume spaces' in front of it (I have tried adding % after each line and removing all spaces but I still get those spaces). Can anyone help me (I want that the aa is followed immediately by the result and not with this free room after it)

• FILE PROBEER.TEX *

 % To turn 1,1,1,2,2,3,5,8,9,10,11,11,11,12,12,13,17,19 into
%         1-3,5,8-13,17,19 if Kind = 0
% To turn 1,1,1,2,2,3,5,8,9,10,11,11,11,12,12,13,17,19 into
%         1,3;2,2;3;5;8;9;10;11,3;12,2;13;17;19 if Kind = 1
% Based on TeX by Topic p 105

\newcount\Kind % 0: like 1--3,5,7,10--12,
% 1: like 1,2;3,2;number,# of number;
\newcount\CountP % Previous
\newcount\CountN % Next
\newcount\CountT % Temp
\newcount\CTimes % Number of times
\newcount\State % 3: using kind=1 after reading in
%    first element (state=2)
% 2: start, first element printed
% 1: during {--}.
% 0: otherwise (non-sequential)
\CountP=-10000
\CTimes=0
\Kind=0

% --- begin TO STORE THE RESULT

\def\ResultDef{} % def to hold the result

% USING DEF TO STORE THE RESULT

\edef\ResultDef{\ResultDef #1}
}

\def\printResultDef{
\ResultDef
}

% --- end TO STORE THE RESULT

\def\Combine#1{\xCombine#1,xxx,
\printResultDef
\CountP=-10000
\CTimes=0
\def\ResultDef{}
}

\def\endpiece{xxx}

\def\xCombine#1,{\def\temp{#1}
\ifx\temp\endpiece
\advance \CountN by 3 % to process the last element
\Process{\the\CountN}
\else \Process{#1}
\expandafter \xCombine
\fi}

\def\Process#1{
\CountN=#1
\ifnum\CountP=-10000
\CountP=\CountN
\State=2
%\the\CountP
\else
\ifnum\CountP=\CountN
\else             % \CountP<\CountN
\ifnum\Kind=0
\ProcessA{#1}
\else
\ProcessB{#1}
\fi
\CTimes=1
\CountP=\CountN
\fi
\fi
}

\def\ProcessA#1{ % \Kind=0
\CountT=\CountP
\ifnum\CountN=\CountT
\ifnum\State=0
%\the\CountP
\fi
\State=1
\else
\ifnum\State=0
%, \the\CountP
\else
\ifnum\State=1
%{--} \the\CountP
\State=0
\else
\ifnum\State=2
\State=0
\else
\typeout{Invalid State!!}
\fi
\fi
\fi
\fi
}

\def\ProcessB#1{ % \Kind=1
\ifnum\State=2
\ifnum\CTimes=1
\else
\fi
\State=3
\else % State=3
\ifnum\CTimes=1
\else
\fi
\fi
}

• FILE PROBEERTEST.TEX *

%% To test probeer.tex in TeX itself to see if it works accordonly
\magnification\magstep1
\hsize 5.5 true in
\hoffset 0.5 true in
\vsize 8.25 true in
\voffset = 2\baselineskip

\input probeer

\noindent 1,1,1,2,3,4,5,6,7,11,12,13,15,17 \hfill \break
becomes \hfill \break
\Combine{1,1,1,2,3,4,5,6,7,11,12,13,15,17} \hfill \break
\hfill

\noindent New attempt: \hfill \break
1,2,3,4,5,6,7,11,12,12,13,15,17,17,21,22,23 \hfill \break
becomes \hfill \break
\Combine{1,2,3,4,5,6,7,11,12,12,13,15,17,17,21,22,23} \hfill \break
\hfill

\vbox{
\hbox to \hsize{1,2,3,4,5,5,6,7,11,12,13,15,17,21,22,23 \hfill}
\hbox to \hsize {becomes \hfill}
\hbox to \hsize{aa\Combine{1,2,3,4,5,5,6,7,11,12,13,15,17,21,22,23}bb \hfill}
\vfill}

\bye

-
and where should the hyperreference on "1-3,5,8-13,17,19" go to? – wasteofspace Jun 5 '12 at 8:49

Every non-macro, in particular { or } that are followed by a newline inside of a macro definition create a space.

If you don't believe those are responsible, you can put

\endlinechar=-1


at the top of your file and

\endlinechar=13


at the bottom to revert to the previous behavior. In this particular case, you'll have to watch for the 'reverse' problem then: you have several numbers that actually are properly terminated by that end of line and that no longer will be so. You can fix that by writing something like

\ifnum\State=1 %


And the space before the comment sign will be visible even with end line characters switched off.

I have to agree with the comment of wasteofspace though: it is not obvious where this combined hyperlink should be pointing.

-
 it shouldn't be a hyperlink but just text that is placed with each entry in the bibliography where that bibitem is referenced at inside the text. – anonymous Jun 5 '12 at 11:43 If it should not be a hyperlink, use the hyperref option backref=false which should actually be the default. Then there should be no reason for hyperref to tamper with the reference formatting. – user9588 Jun 5 '12 at 12:49