问题

问题

我已经问过一个相关问题,如何通过名称标记/引用包含宏的描述项?

现在,我在使用 的超链接编译描述列表时遇到了困难hyperref

问题

我想制作描述列表项(我们将每个项称为键值对) 的键中包含超链接。

代码

描述列表已被重新定义一次,以允许标签引用宏(即 提供的宏menukeys)。

\documentclass{article}
\usepackage{fontspec} % for xelatex
\usepackage{hyperref}
\usepackage{enumitem}

\setlist[description]{style=nextline,labelwidth=0pt,leftmargin=30pt,itemindent=\dimexpr-20pt-\labelsep\relax} % Global Setup Description List
    
\makeatletter % Redefinition of Description List Items source: https://tex.stackexchange.com/a/1248/13552
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \protected@edef\@currentlabel{#1}%
  %\edef\@currentlabelname{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}
\section{User Interface}
The following is a list of important user interface components:
\begin{description}
  \item [Main Window] The main window. % <-- Not so cool.
  \item [Side Bar] for navigation. % <-- Not so cool.
  \item [Document Tree] A document tree. % <-- Not so cool.
  %\item [\hyperlink{gui:mainwindow}{Main Window}] The main window. % <-- way cooler
  %\item [\hyperlink{gui:sidebar}{Side Bar}] for navigation. % <-- way cooler
  %\item [\hyperlink{gui:documenttree}{Document Tree}] A document tree. % <-- way cooler
\end{description}

\hypertarget{gui:mainwindow}{\subsection{Main Window}}
Some nice long description.

\hypertarget{gui:sidebar}{\subsection{Side Bar}}
Some nice long description.

\hypertarget{gui:documenttree}{\subsection{Document Tree}}
Some nice long description.

\end{document}

答案1

\item是带有移动(可选)参数的命令,即\hyperlink此处易碎。要么使用\protect\hyperlink{...}(每次),要么合并\protect into\item[...] with a redefinition of\item`。

另一种可能性是使用\robustify{\hyperlink}(需要etoolbox),但不确定是否全局强化\hyperlink一个聪明的想法。

\documentclass{article}
\usepackage{fontspec} % for xelatex
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{hyperref}

\setlist[description]{style=nextline,labelwidth=0pt,leftmargin=30pt,itemindent=\dimexpr-20pt-\labelsep\relax} % Global Setup Description List

\AtBeginDocument{%
\let\origitem\item

\RenewDocumentCommand{\item}{o}{%
  \IfValueTF{#1}{%
  \origitem[\protect#1]%
  }{%
    \origitem% 
  }
}
}

\makeatletter % Redefinition of Description List Items source: http://tex.stackexchange.com/a/1248/13552
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \protected@edef\@currentlabel{#1}%
  %\edef\@currentlabelname{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}
\section{User Interface}
The following is a list of important user interface components:
\begin{description}
  \item [Main Window] The main window. % <-- Not so cool.
  \item [Side Bar] for navigation. % <-- Not so cool.
  \item [Document Tree] A document tree. % <-- Not so cool.
  \item [\hyperlink{gui:mainwindow}{Main Window}] The main window. % <-- way cooler
  \item [\hyperlink{gui:sidebar}{Side Bar}] for navigation. % <-- way cooler
  \item [\hyperlink{gui:documenttree}{Document Tree}] A document tree. % <-- way cooler
\end{description}

\clearpage

\hypertarget{gui:mainwindow}{\subsection{Main Window}}
Some nice long description.

\hypertarget{gui:sidebar}{\subsection{Side Bar}}
Some nice long description.

\hypertarget{gui:documenttree}{\subsection{Document Tree}}
Some nice long description.

\end{document}

更新

甚至更短的方法,使用\deschyperlink强大的包装命令:

\documentclass{article}
\usepackage{fontspec} % for xelatex
\usepackage{enumitem}
\usepackage{hyperref}

\setlist[description]{style=nextline,labelwidth=0pt,leftmargin=30pt,itemindent=\dimexpr-20pt-\labelsep\relax} % Global Setup Description List

\DeclareRobustCommand{\deschyperlink}[2]{%
  \hyperlink{#1}{#2}%
}


\makeatletter % Redefinition of Description List Items source: http://tex.stackexchange.com/a/1248/13552
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \protected@edef\@currentlabel{#1}%
  %\edef\@currentlabelname{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}
\section{User Interface}
The following is a list of important user interface components:
\begin{description}
  \item [Main Window] The main window. % <-- Not so cool.
  \item [Side Bar] for navigation. % <-- Not so cool.
  \item [Document Tree] A document tree. % <-- Not so cool.
  \item [\deschyperlink{gui:mainwindow}{Main Window}] The main window. % <-- way cooler
  \item [\deschyperlink{gui:sidebar}{Side Bar}] for navigation. % <-- way cooler
  \item [\deschyperlink{gui:documenttree}{Document Tree}] A document tree. % <-- way cooler
\end{description}

\clearpage

\hypertarget{gui:mainwindow}{\subsection{Main Window}}
Some nice long description.

\hypertarget{gui:sidebar}{\subsection{Side Bar}}
Some nice long description.

\hypertarget{gui:documenttree}{\subsection{Document Tree}}
Some nice long description.

\end{document}

答案2

只需用\hyperref花括号括起来就可以了。

@Tom 在评论中写了这个,但我(不是 TeXpert)认为这是一个合理的答案,因为它按预期工作。这个答案只是为了可见性。

相关内容