itemize/enumerate - 如何将环境中的第一个 \item(包括项目符号/项目编号)转换为超链接?

itemize/enumerate - 如何将环境中的第一个 \item(包括项目符号/项目编号)转换为超链接?

在 LaTeX2e 中使用超链接-package,您可以应用命令

\hyperref[⟨referencing-label⟩]{⟨tokens that form the text of the hyperlink⟩}
将文本短语转换为可点击的超链接,导航到引用标签所指示的文档位置。

如何才能把一个完整的\item(即,项目符号/项目编号以及项目文本作为第一项出现在枚举环境/逐项环境中变成这样一个可点击的超链接?

我可以提供一个最小的例子,它并不完全符合我的要求::-)

\documentclass[a4paper]{article}
\usepackage[colorlinks=true, linkcolor=red]{hyperref}
\begin{document}

\section{A section}\label{Section}

Some text.

\newpage
\enlargethispage{2cm}

% This does work but the bullet is not part of the hyperlink:

\begin{verbatim}
\begin{itemize}
\item\hyperref[Section]{Both the bullet and the text of this item are
                        to be turned into a hyperlink for navigating
                        to the section ``A section''.\\
                        But---as expected with this approach---the 
                        bullet will not be part of the hyperlink.}
\end{itemize}
\end{verbatim}

yields:

\begin{itemize}
\item\hyperref[Section]{Both the bullet and the text of this item are
                        to be turned into a hyperlink for navigating
                        to the section ``A section''.\\
                        But---as expected with this approach---the 
                        bullet will not be part of the hyperlink.}
\end{itemize}

\noindent\hrulefill

% This does work but the item that is to be turned into a hyperlink 
% is not the first item of the environment:

\begin{verbatim}
\begin{itemize}
\item Some dummy item.
\hyperref[Section]{\item Both the bullet and the text of this item are
                         to be turned into a hyperlink for navigating
                         to the section ``A section''.\\
                         This works out while the item that is to be turned 
                         into a hyperlink is not the first item of the
                         environment.}
\end{itemize}
\end{verbatim}

yields:

\begin{itemize}
\item Some dummy item.
\hyperref[Section]{\item Both the bullet and the text of this item are
                         to be turned into a hyperlink for navigating
                         to the section ``A section''.\\
                         This works out while the item that is to be turned 
                         into a hyperlink is not the first item of the
                         environment.}
\end{itemize}

\noindent\hrulefill

% This leads to ! LaTeX Error: Something's wrong--perhaps a missing \item.

\begin{verbatim}
\begin{itemize}
\hyperref[Section]{\item Both the bullet and the text of this item are
                         to be turned into a hyperlink for navigating
                         to the section ``A section''.\\
                         This does not work out while the item that is to 
                         be turned into a hyperlink is the first item
                         of the environment.\\
                         You get \texttt{! LaTeX Error: Something's wrong%
                         \string-\string-perhaps a missing \string\item}.}
\end{itemize}
\end{verbatim}

yields:

%\begin{itemize}
%\hyperref[Section]{\item Both the bullet and the text of this item are
%                         to be turned into a hyperlink for navigating
%                         to the section ``A section''.\\
%                         This does not work out while the item that is to 
%                         be turned into a hyperlink is the first item
%                         of the environment.\\
%                         You get \texttt{! LaTeX Error: Something's wrong%
%                         \string-\string-perhaps a missing \string\item}.}
%\end{itemize}
\begin{verbatim}
! LaTeX Error: Something's wrong--perhaps a missing \item.
\end{verbatim}

\newpage
\enlargethispage{2cm}

% This does work but the bullet is not part of the hyperlink:

\begin{verbatim}
\begin{enumerate}
\item\hyperref[Section]{Both the item-number and the text of this item
                        are to be turned into a hyperlink for navigating
                        to the section ``A section''.\\
                        But---as expected with this approach---the 
                        item-number will not be part of the hyperlink.}
\end{enumerate}
\end{verbatim}

yields:

\begin{enumerate}
\item\hyperref[Section]{Both the item-number and the text of this item
                        are to be turned into a hyperlink for navigating
                        to the section ``A section''.\\
                        But---as expected with this approach---the 
                        item-number will not be part of the hyperlink.}
\end{enumerate}

\noindent\hrulefill

% This does work but the item that is to be turned into a hyperlink 
% is not the first item of the environment:

\begin{verbatim}
\begin{enumerate}
\item Some dummy item.
\hyperref[Section]{\item Both the item-number and the text of this item
                         are to be turned into a hyperlink for navigating
                         to the section ``A section''.\\
                         This works out while the item that is to be turned 
                         into a hyperlink is not the first item of the
                         environment.}
\end{enumerate}
\end{verbatim}

yields:

\begin{enumerate}
\item Some dummy item.
\hyperref[Section]{\item Both the item-number and the text of this item
                         are to be turned into a hyperlink for navigating
                         to the section ``A section''.\\
                         This works out while the item that is to be turned 
                         into a hyperlink is not the first item of the
                         environment.}
\end{enumerate}

\noindent\hrulefill

% This leads to ! LaTeX Error: Something's wrong--perhaps a missing \item.

\begin{verbatim}
\begin{enumerate}
\hyperref[Section]{\item Both the item-number and the text of this item
                         are to be turned into a hyperlink for navigating
                         to the section ``A section''.\\
                         This does not work out while the item that is to 
                         be turned into a hyperlink is the first item
                         of the environment.\\
                         You get \texttt{! LaTeX Error: Something's wrong%
                         \string-\string-perhaps a missing \string\item}.}
\end{enumerate}
\end{verbatim}

yields:

%\begin{enumerate}
%\hyperref[Section]{\item Both the item-number and the text of this item
%                         are to be turned into a hyperlink for navigating
%                         to the section ``A section''.\\
%                         This does not work out while the item that is to 
%                         be turned into a hyperlink is the first item
%                         of the environment.\\
%                         You get \texttt{! LaTeX Error: Something's wrong%
%                         \string-\string-perhaps a missing \string\item}.}
%\end{enumerate}
\begin{verbatim}
! LaTeX Error: Something's wrong--perhaps a missing \item.
\end{verbatim}

\end{document}

第 1 页——因此,您拥有一些通过超链接导航的内容:

在此处输入图片描述

第 2 页——使用 itemize-environment 进行测试:

在此处输入图片描述

第 3 页——使用枚举环境进行测试:

在此处输入图片描述

如果你需要了解软件包版本等类似信息,这里是 .log 文件的摘录:

This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2018/Debian) (preloaded format=pdflatex 2018.4.29)  28 NOV 2018 17:44
entering extended mode
 restricted \write18 enabled.
 %&-line parsing enabled.
[...]
LaTeX2e <2018-04-01> patch level 2
Babel <3.18> and hyphenation patterns for 12 language(s) loaded.
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
File: size10.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
Package: hyperref 2018/02/06 v6.86b Hypertext links for LaTeX
Package: hobsub-hyperref 2016/05/16 v1.14 Bundle oberdiek, subset hyperref (HO)
Package: hobsub-generic 2016/05/16 v1.14 Bundle oberdiek, subset generic (HO)
Package: hobsub 2016/05/16 v1.14 Construct package bundles (HO)
Package: infwarerr 2016/05/16 v1.4 Providing info/warning/error messages (HO)
Package: ltxcmds 2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
Package: ifluatex 2016/05/16 v1.4 Provides the ifluatex switch (HO)
Package ifluatex Info: LuaTeX not detected.
Package: ifvtex 2016/05/16 v1.6 Detect VTeX and its facilities (HO)
Package ifvtex Info: VTeX not detected.
Package: intcalc 2016/05/16 v1.2 Expandable calculations with integers (HO)
Package: ifpdf 2017/03/15 v3.2 Provides the ifpdf switch
Package: etexcmds 2016/05/16 v1.6 Avoid name clashes with e-TeX commands (HO)
Package etexcmds Info: Could not find \expanded.
(etexcmds)             That can mean that you are not using pdfTeX 1.50 or
(etexcmds)             that some package has redefined \expanded.
(etexcmds)             In the latter case, load this package earlier.
Package: kvsetkeys 2016/05/16 v1.17 Key value parser (HO)
Package: kvdefinekeys 2016/05/16 v1.4 Define keys (HO)
Package: pdftexcmds 2018/01/30 v0.27 Utility functions of pdfTeX for LuaTeX (HO
Package pdftexcmds Info: LuaTeX not detected.
Package pdftexcmds Info: \pdf@primitive is available.
Package pdftexcmds Info: \pdf@ifprimitive is available.
Package pdftexcmds Info: \pdfdraftmode found.
Package: pdfescape 2016/05/16 v1.14 Implements pdfTeX's escape features (HO)
Package: bigintcalc 2016/05/16 v1.4 Expandable calculations on big integers (HO
Package: bitset 2016/05/16 v1.2 Handle bit-vector datatype (HO)
Package: uniquecounter 2016/05/16 v1.3 Provide unlimited unique counter (HO)
Package hobsub Info: Skipping package `hobsub' (already loaded).
Package: letltxmacro 2016/05/16 v1.5 Let assignment for LaTeX macros (HO)
Package: hopatch 2016/05/16 v1.3 Wrapper for package hooks (HO)
Package: xcolor-patch 2016/05/16 xcolor patch
Package: atveryend 2016/05/16 v1.9 Hooks at the very end of document (HO)
Package atveryend Info: \enddocument detected (standard20110627).
Package: atbegshi 2016/06/09 v1.18 At begin shipout hook (HO)
Package: refcount 2016/05/16 v3.5 Data extraction from label references (HO)
Package: hycolor 2016/05/16 v1.8 Color options for hyperref/bookmark (HO)
Package: keyval 2014/10/28 v1.15 key=value parser (DPC)
Package: ifxetex 2010/09/12 v0.6 Provides ifxetex conditional
Package: auxhook 2016/05/16 v1.4 Hooks for auxiliary files (HO)
Package: kvoptions 2016/05/16 v3.12 Key value format for package options (HO)
File: pd1enc.def 2018/02/06 v6.86b Hyperref: PDFDocEncoding definition (HO)
File: hyperref.cfg 2002/06/06 v1.2 hyperref configuration of TeXLive
Package: url 2013/09/16  ver 3.4  Verb mode for urls, etc.
File: hpdftex.def 2018/02/06 v6.86b Hyperref driver for pdfTeX
Package: rerunfilecheck 2016/05/16 v1.8 Rerun checks for auxiliary files (HO)
Package uniquecounter Info: New unique counter `rerunfilecheck' on input line 2
Package: color 2016/07/10 v1.1e Standard LaTeX Color (DPC)
File: color.cfg 2016/01/02 v1.6 sample color configuration
Package color Info: Driver file: pdftex.def on input line 147.
File: pdftex.def 2018/01/08 v1.0l Graphics/color driver for pdftex
Package: nameref 2016/05/21 v2.44 Cross-referencing by name of section
Package: gettitlestring 2016/05/16 v1.5 Cleanup title references (HO)

答案1

值得注意的是,\item的标签(\textbullet或 下的数字enumerate)实际上可以作为链接点击,因为它设置在零宽度框内。这是为了确保\items 上的适当右(水平)对齐。

下面的代码提供了按以下方式\hyperrefitem[<link>]{<item>}设置泛型的方法:\item

\item[{\hyperref[<link>]{<label>}}]
  \hyperref[<link>]{<item>}

取决于<label>您是否使用itemizeenumerate(无论级别/深度如何。使用此方法 - 使用超链接明确设置列表标签 - 即使标签也是\item可点击的。

在此处输入图片描述

\documentclass{article}

\usepackage[colorlinks=true, linkcolor=red]{hyperref}

\makeatletter
\newcommand{\hyperrefitem}[2][]{%
  % Check if you're inside an enumerate environment
  \ifnum\pdfstrcmp{\@currenvir}{enumerate}=0
    \stepcounter{\@enumctr}%
    \item[{\hyperref[#1]{\csname label\@enumctr\endcsname}}]
  \fi
  % Check if you're inside an itemize environment
  \ifnum\pdfstrcmp{\@currenvir}{itemize}=0
    \item[{\hyperref[#1]{\csname\@itemitem\endcsname}}]
  \fi
  % Set \item content
  \hyperref[#1]{#2}%
}
\makeatother

\begin{document}

\section{A section}\label{Section}

\begin{itemize}
  \item First
  \hyperrefitem[Section]{Section}
\end{itemize}

\noindent\hrulefill

\begin{itemize}
  \hyperrefitem[Section]{Section}
  \item Second
\end{itemize}

\noindent\hrulefill

\begin{enumerate}
  \item First
  \hyperrefitem[Section]{Section}
\end{enumerate}

\noindent\hrulefill

\begin{enumerate}
  \hyperrefitem[Section]{Section}
  \item Second
\end{enumerate}

\end{document}

相关内容