自动生成文档中 \url 用法列表描述了一种生成文档中使用的 URL 索引的方法,包括页码。但是,URL 是按字母数字排序的。我想制作一个 URL 索引,按页面对它们进行排序,按照链接在页面上出现的顺序进行排序。此外,我的链接实际上是在\hrefs
,我想将链接文本包含在索引中:
p. 1
Wikipedia... http://wikipedia.org
SXE... http://stackexchange.com
p. 2
Google... http://google.com
我按照 Heiko Oberdiek 在上面链接的帖子中的说明创建了一个 MWE(使用xelatex links.tex
、makeindex links-url
、进行编译xelatex links.tex
)。请注意,我目前得到的输出是按字母顺序而不是页面顺序排列的,正如预期的那样(但不是我想要的):
\documentclass[12pt,article]{memoir}
\settrimmedsize{11in}{8.5in}{*}
\settrims{0in}{0in}
\settypeblocksize{9in}{6.5in}{*}
\setlrmargins{1in}{*}{*}
\setulmargins{1in}{*}{*}
\setheadfoot{\onelineskip}{2\onelineskip}
\setheaderspaces{*}{1.5\onelineskip}{*}
\checkandfixthelayout
\PassOptionsToPackage{hyphens}{url}
\usepackage[linktoc=all,frenchlinks,pdfborderstyle={/S/U/W .5},citebordercolor={1 1 1},linkbordercolor={1 1 1},urlbordercolor={1 1 1}]{hyperref}
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\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 ""
headings_flag 0
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
}
\let\saved@hyper@linkurl\hyper@linkurl
\renewcommand*{\hyper@linkurl}[2]{%
\write@url{#2}%
\saved@hyper@linkurl{#1}{#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.} \\
\quad \\ \noindent
%\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
\space(\if\instring{,}{#1}{pp.\space#1}\else{p.\space\hyperlink{page.#1}{#1}}\fi)}
}
\makeatother
\begin{document}
\href{http://wikipedia.org}{Wikipedia} \href{http://stackexchange.com}{SXE}
\newpage
\href{http://google.com}{Google}
\section*{Index of URLs}
\printurls
\end{document}
答案1
由于 URL 不是按字母顺序排序的,因此无需通过 Makeindex 进行排序。页码只需要标签/参考系统(两次 LaTeX 运行)。
该示例将每个 URL 的 URL、文本和页面存储在引用中。包zref
允许在其引用中输入任意数据字段。
然后\UrlList
打印 URL 列表。这可以通过环境UrlListEnv
和宏\UrlListPage
和进行配置\UrlListItem
。下面的示例使用description
带有页面描述标签的环境。
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\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@newprop{UrlList@HexLink}[3F3F]{\UrlList@HexLink}
\zref@newlist{UrlList}
\zref@addprops{UrlList}{UrlList@Text, UrlList@HexLink, page}
\zref@newprop{UrlList@Max}{\the\value{UrlList}}
\zref@addprops{LastPage}{UrlList@Max}
% 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
\newcommand\saved@hyper@linkurl{}
\let\saved@hyper@linkurl\hyper@linkurl
\renewcommand{\hyper@linkurl}[2]{%
% #1: text
% #2: URL
\ifUrlList@
\else
\begingroup
\refstepcounter{UrlList}%
\protected@edef\UrlList@Text{#1}%
\@onelevel@sanitize\UrlList@Text
\EdefEscapeHex\UrlList@HexLink{#2}%
\zref@labelbylist{\theUrlList}{UrlList}%
\endgroup
\fi
\saved@hyper@linkurl{#1}{#2}%
}
% \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}{%
}{%
\EdefUnescapeHex\UrlList@Link{%
\zref@extract{\theUrlListAux}{UrlList@HexLink}%
}%
\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@Link}%
}{%
\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>}
% Sets the page header
\newcommand*{\UrlListPage}[1]{%
\item[\hyperlink{page.#1}{Page #1}]\mbox{}%
}
% \UrlListItem{<page>}{<URL>}{<text>}
% Formats a URL entry
\newcommand*{\UrlListItem}[3]{%
\\\relax#3 \dots\ \href{#2}{\nolinkurl{#2}}%
}
\makeatother
\begin{document}
\href{http://wikipedia.org}{Wikipedia} \href{http://stackexchange.com}{SXE}
\newpage
\href{http://google.com}{Google}
\section*{Index of URLs}
\UrlList
\end{document}
结果/第 2 页:
答案2
尝试了几种不同的方法后,这里有一个相当有效的解决方案 - 尽管它需要手动编辑文件.ind
,所以还有改进的空间!
\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}}}}
一些评论:
- 没有必要使用标准索引功能来在每个条目后生成页码(现在我只是从
.ind
文件中删除这些行)。 #
URL 内部会使索引机制陷入循环。
为了使显示看起来合理,我做了一些自定义memoir
:
\renewcommand{\indexname}{Web Pages Cited in This Book}
\onecolindex
\printindex
我还在文件\raggedright
顶部添加了.ind
。