3

When in LaTeX2e using the hyperref-package, you can apply the command

\hyperref[⟨referencing-label⟩]{⟨tokens that form the text of the hyperlink⟩}
for turning phrases of text into clickable hyperlinks that navigate to the place of the document denoted by the referencing-label.

How can you turn a complete \item (i.e., both the bullet/item number and the text of the item) that occurs as the first item within an enumerate-environment/itemize-environment into such a clickable hyperlink?

I can provide a minimal example which does not do exactly what I desire:      :-)

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

Page 1 - So you have something where hyperlinks navigate:

enter image description here

Page 2 - Tests with itemize-environment:

enter image description here

Page 3 - Tests with enumerate-environment:

enter image description here

In case you need to know about package-versions and the like, here is an excerpt from the .log-file:

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
  • I was about to ask the same question when I found this thread. Has anyone (including the user who originally posted the question) an answer for this issue? thanks! Dec 30, 2021 at 9:53

1 Answer 1

1

What is interesting to note is that the \item's label (\textbullet or a number under enumerate) it's actually clickable as a link, since it's set inside a zero-width box. This is to ensure an appropriate right (horizontal) alignment across \items.

The code below provides \hyperrefitem[<link>]{<item>} that sets a generic \item in the following way:

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

where <label> depends on whether you're using itemize or enumerate (regardless of the level/depth. Using this approach - setting the list label explicitly with a hyperlink - even the \item label is clickable.

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .