使用 Hyperref 格式但禁用链接

使用 Hyperref 格式但禁用链接

我怎样才能得到hyperref格式化链接,但实际上不链接到任何内容。例如,在传奇在下面 MWE 部分中,我希望单击链接时实际上不会尝试打开任何内容。但同时,我需要对hyperref链接进行格式化,以使其与文档的其余部分保持一致。

在此处输入图片描述

使用\hypersetup{draft}好像没有任何效果。

参考:

代码:

\documentclass{article}

\usepackage{xcolor}
\usepackage[colorlinks=false, pdfborderstyle={/S/U/W 1}, runbordercolor=red]{hyperref}

\begin{document}
\begingroup
    \hypersetup{draft}% <-- This has no effect.
    \textbf{Legend:} Want these two links to \emph{not} be clickable
    \begin{itemize}
        \item Web links are displayed as \href{http://www.google.com}{site name} and 
        \item links to local pdf file are displayed as \href{run:foo.pdf}{PDF name}
    \end{itemize}
\endgroup
\medskip

\textbf{Main Document}\par
A good search engine: \href{http://www.google.com}{Google}

A good place for news: \href{http://www.yahoo.com}{Yahoo}

Here is an important PDF file: \href{run:foo.pdf}{My PDF}
\end{document}

答案1

我认为完全避免链接会很困难,但您可以使用内部链接:

\documentclass{article}

\usepackage{xcolor}
\usepackage[colorlinks=false, pdfborderstyle={/S/U/W 1}, runbordercolor=red]{hyperref}

\begin{document}
\begingroup
    \textbf{Legend:} Want these two links to \emph{not} be clickable
    \begin{itemize}
        \item Web links are displayed as {\hypersetup{linkbordercolor=cyan}\label{anchor1}\hyperref[anchor1]{site name}} and
        \item links to local pdf file are displayed as {\label{anchor2}\hyperref[anchor2]{PDF name}}
    \end{itemize}
\endgroup
\medskip

\textbf{Main Document}\par
A good search engine: \href{http://www.google.com}{Google}

A good place for news: \href{http://www.yahoo.com}{Yahoo}

Here is an important PDF file: \href{run:foo.pdf}{My PDF}
\end{document}

在此处输入图片描述

答案2

这是一个解决方案。

关键点是\IfBeginWith来自字符串包裹

很难找到使用下划线的正确语法\pdfliteral,但多亏了这个技巧

\documentclass{article}

\usepackage{xcolor}
\usepackage[colorlinks=false, pdfborderstyle={/S/U/W 1}, runbordercolor=red]{hyperref}

\usepackage{xstring}
\newlength\mtlength

\begin{document}
\begingroup
\makeatletter
\renewcommand{\href}[2]{%
\settowidth{\mtlength}{#2}%
\def\mtl{\strip@pt\mtlength}%
\IfBeginWith{#1}{run}{%
\def\mtcolor{1 0 0 RG}}{%  red
\def\mtcolor{0 1 1 RG}}%   light blue ??!
\pdfliteral{q 1 w 1 J \mtcolor\space 0 -2 m \mtl\space -2 l S Q}#2}
\makeatother
    \textbf{Legend:} Want these two links to \emph{not} be clickable
    \begin{itemize}
        \item Web links are displayed as \href{http://www.google.com}{site name} and 
        \item links to local pdf file are displayed as \href{run:foo.pdf}{PDF name}
    \end{itemize}
\endgroup
\medskip

\textbf{Main Document}\par
A good search engine: \href{http://www.google.com}{Google}

A good place for news: \href{http://www.yahoo.com}{Yahoo}

Here is an important PDF file: \href{run:foo.pdf}{My PDF}
\end{document}

相关内容