# makeindex - print links sorted by page

Auto generate List of \url usages within document describes a method for generating an index of URLs that are used in the document, including page number. However, the URLs are sorted alphanumerically. I'd like to make a URL index that sorts them per page, in the order in which the links appear on the page. Furthermore, my links are actually in \hrefs and I would like to include the link text in the index:

p. 1
Wikipedia... http://wikipedia.org
SXE... http://stackexchange.com
p. 2


I've created a MWE following the instructions from Heiko Oberdiek in the post I linked to above (compile with xelatex links.tex, makeindex links-url, xelatex links.tex). Note, the output I currently get is, as expected (but not desired) in alphabetical order rather than page order:

\documentclass[12pt,article]{memoir}

\settrimmedsize{11in}{8.5in}{*}
\settrims{0in}{0in}
\settypeblocksize{9in}{6.5in}{*}
\setlrmargins{1in}{*}{*}
\setulmargins{1in}{*}{*}

\checkandfixthelayout

\PassOptionsToPackage{hyphens}{url}
\makeatletter
\makeatother

\usepackage{filecontents}

\begin{filecontents*}{\jobname-url.mst}
% Input style specifiers
keyword "\\urlentry"
% Output style specifiers
preamble "\\begin{theurls}"
postamble "\\end{theurls}\n"
group_skip ""
item_0 "\n\\urlitem{"
delim_0 "}{"
delim_t "}"
line_max 500
\end{filecontents*}

\usepackage{pdfescape}

\makeatletter
\newwrite\file@url
\openout\file@url=\jobname-url.idx\relax

\def\instring#1#2{TT\fi\begingroup
\edef\x{\endgroup\noexpand\in@{#1}{#2}}\x\ifin@}

\newcommand*{\write@url}[1]{%
\begingroup
\EdefEscapeHex\@tmp{#1}%
\protected@write\file@url{}{%
\protect\urlentry{\@tmp}{\thepage}%
}%
\endgroup
}
\write@url{#2}%
}
\newcommand*{\listurlname}{List of URLs}
\newcommand*{\printurls}{%
\InputIfFileExists{\jobname-url.ind}{}{}%
}
\newenvironment{theurls}{%
\begin{center}{\Huge \listurlname}%
\vspace{.7in}\\ \end{center}
{All of these links were active as of \today.} \\
%\section*{\listurlname}%
\@mkboth{\listurlname}{\listurlname}%
\let\write@url\@gobble
\ttfamily\footnotesize
\raggedright
\hspace{-1.4em}
}{%
\par
}
\newcommand*{\urlitem}[2]{%
\hangindent=1em
\hangafter=1
\begingroup
\urlpages{#2}%
\EdefUnescapeHex\@tmp{#1}%
\expandafter\url\expandafter{\@tmp}%
\endgroup
\newline
}
\newcommand*{\urlpages}[1]{%
{%
\normalfont
}
\makeatother

\begin{document}
\href{http://wikipedia.org}{Wikipedia} \href{http://stackexchange.com}{SXE}
\newpage

\section*{Index of URLs}
\printurls
\end{document}


Since the URLs are not sorted alphabetically, sorting via Makeindex is not necessary. The page numbers only requires the label/reference system (two LaTeX runs).

The example stores for each URL the URL, text and page in a reference. Package zref allows arbitrary data fields in its references.

Then \UrlList prints the list of URLs. This can be configured by environment UrlListEnv and macros \UrlListPage and \UrlListItem. The example below uses a description environment with page description labels.

\documentclass{article}

\usepackage{zref-base,zref-lastpage}
\usepackage{etexcmds}
\usepackage{pdfescape}

\makeatletter

% Help counters for numbering URLs
\newcounter{UrlList}
\newcounter{UrlListAux}
\renewcommand*{\theUrlList}{UrlList\the\value{UrlList}}
\renewcommand*{\theUrlListAux}{UrlList\the\value{UrlListAux}}

% Each URL gets a reference with text, url and page number.
\zref@newprop{UrlList@Text}{\UrlList@Text}
\zref@newlist{UrlList}
\zref@newprop{UrlList@Max}{\the\value{UrlList}}

% Switch is needed in the list of URLs to disable URL recording.
\newif\ifUrlList@

% Hyperref internal is redefined to write the label with the URL data
% #1: text
% #2: URL
\ifUrlList@
\else
\begingroup
\refstepcounter{UrlList}%
\protected@edef\UrlList@Text{#1}%
\@onelevel@sanitize\UrlList@Text
\zref@labelbylist{\theUrlList}{UrlList}%
\endgroup
\fi
}

% \UrlList checks, whether are URL references and prints the list of URLs
\newcommand*{\UrlList}{%
\zref@refused{LastPage}%
\edef\UrlList@Max{%
\zref@extractdefault{LastPage}{UrlList@Max}{-1}%
}%
\ifnum\UrlList@Max<0 %
\@latex@warning@no@line{Rerun LaTeX to get list of URLs}%
\else
\UrlList@true
\begin{UrlListEnv}%
\let\UrlList@LastPage\@empty
\setcounter{UrlListAux}{0}%
\@whilenum\value{UrlListAux}<\UrlList@Max\do{%
\stepcounter{UrlListAux}%
\zref@refused{\theUrlListAux}%
\zref@ifrefundefined{\theUrlListAux}{%
}{%
}%
\zref@def@extract\UrlList@Text{\theUrlListAux}{UrlList@Text}%
\zref@def@extract\UrlList@Page{\theUrlListAux}{page}%
\edef\UrlList@Next{%
\noexpand\UrlListItem{%
\etex@unexpanded\expandafter{\UrlList@Page}%
}{%
}{%
\etex@unexpanded\expandafter{\UrlList@Text}%
}%
}%
\ifx\UrlList@Page\UrlList@LastPage
\else
\expandafter\UrlListPage\expandafter{\UrlList@Page}%
\let\UrlList@LastPage\UrlList@Page
\fi
\UrlList@Next
}%
}%
\end{UrlListEnv}%
\fi
}
\makeatletter

% USER configuration

% Environment UrlListEnv surrounds the list of URLs, if
% URLs are available.
\newenvironment{UrlListEnv}{%
\begin{description}%
}{%
\end{description}%
}

% \UrlListPage{<page>}
\newcommand*{\UrlListPage}[1]{%
}

% \UrlListItem{<page>}{<URL>}{<text>}
% Formats a URL entry
\newcommand*{\UrlListItem}[3]{%
}
\makeatother

\begin{document}
\href{http://wikipedia.org}{Wikipedia} \href{http://stackexchange.com}{SXE}
\newpage

\section*{Index of URLs}
\UrlList
\end{document}


Result/page 2:

After trying a few different approaches, here is a rather hacky solution that works -- although it requires some by-hand editing of the .ind file, so there's room for improvement!

\usepackage{makeidx}
\makeindex
\let\hrefold\href
\newcounter{IndexItemCounter}
\renewcommand*{\href}[2]{\stepcounter{IndexItemCounter}%
\hrefold{#1}{#2}\index{\thepage@{p. \thepage}%
!{\theIndexItemCounter}@{#2\ldots\ \url{#1}}}}


• There's no real need for the standard indexing feature which produces page numbers following each entry (for now I just delete those lines from the .ind file).
• # inside of URLs throws the indexing mechanism for a loop.

To make the display look reasonable I do a tiny bit of customization with memoir:

\renewcommand{\indexname}{Web Pages Cited in This Book}
\onecolindex
\printindex


I also added \raggedright at the top of the .ind file.

• Note, using \hyperlink{page.\thepage}{p. \thepage} didn't work for me. – Joe Corneli Nov 27 '13 at 13:11