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
     Google... http://google.com

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:

enter image description here




\usepackage[linktoc=all,frenchlinks,pdfborderstyle={/S/U/W .5},citebordercolor={1 1 1},linkbordercolor={1 1 1},urlbordercolor={1 1 1}]{hyperref}


% Input style specifiers
keyword "\\urlentry"
% Output style specifiers
preamble "\\begin{theurls}"
postamble "\\end{theurls}\n"
group_skip ""
headings_flag 0
item_0 "\n\\urlitem{"
delim_0 "}{"
delim_t "}"
line_max 500




\newcommand*{\listurlname}{List of URLs}
  \begin{center}{\Huge \listurlname}%
\vspace{.7in}\\ \end{center}
{All of these links were active as of \today.} \\
\quad \\ \noindent

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

\section*{Index of URLs}

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.




% Help counters for numbering URLs

% Each URL gets a reference with text, url and page number.
\zref@addprops{UrlList}{UrlList@Text, UrlList@HexLink, page}

% Switch is needed in the list of URLs to disable URL recording.

% Hyperref internal is redefined to write the label with the URL data
  % #1: text
  % #2: URL

% \UrlList checks, whether are URL references and prints the list of URLs
  \ifnum\UrlList@Max<0 %
    \@latex@warning@no@line{Rerun LaTeX to get list of URLs}%

% USER configuration

% Environment UrlListEnv surrounds the list of URLs, if
% URLs are available.

% \UrlListPage{<page>}
% Sets the page header
  \item[\hyperlink{page.#1}{Page #1}]\mbox{}%

% \UrlListItem{<page>}{<URL>}{<text>}
% Formats a URL entry
  \\\relax#3 \dots\ \href{#2}{\nolinkurl{#2}}%

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

\section*{Index of URLs}

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!

\hrefold{#1}{#2}\index{\thepage@{p. \thepage}%
!{\theIndexItemCounter}@{#2\ldots\ \url{#1}}}}

A few comments:

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

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

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.