1

By default, the hyperref package will add a cyan box around a URL:

\documentclass{amsart}
\usepackage{hyperref}
\begin{document}
    \url{https://www.baidu.com}
\end{document}

How can I add such a cyan box around any piece of text that makes it looks like a URL but is not clickable? This box should not show in the printed version. A "demo":

\documentclass{amsart}
%???????????????????????????
\begin{document}
    \fakeurl{any piece of code}
\end{document}

1 Answer 1

2

Hyperref boxed links are implemented using PDF annotations of subtype /Link. Most PDF viewers recognize this type and change the mouse cursor to the link icon on such an annotation, even if the link target is actually empty. This creates the appearance of being clickable, even if clicking such a 'link' actually does not have any effect when the link target is empty.

There are also other subtypes of annotations. One subtype is /Square, which is defined as follows in the PDF 1.4 reference:

Square and circle annotations (PDF 1.3) display, respectively, a rectangle or an ellipse on the page. When opened, they display a pop-up window containing the text of the associated note.

While at first this does not seem to satisfy the requirement "not clickable", in every PDF viewer that I tried this type of annotation without an action specification is indeed not clickable.

Moreover, you can set various Annotation Flags to the annotation, one of which is ReadOnly (set with /F 64). This flag is described as follows:

If set, do not allow the annotation to interact with the user. The annotation may be displayed or printed (depending on the settings of the NoView and Print flags), but should not respond to mouse clicks or change its appearance in response to mouse motions.

So, presumably (but maybe not), using the subtype /Square with the readonly flag set will convince most PDF viewers to show the box but don't change the mouse cursor or perform any action on click.

Hyperref has a separate definitions file for each compiler and toolchain (pdflatex, xelatex, lualatex, dvips, dvipdfm, ...). The following code is taken from hpdftex.def and is supposed to be used with pdflatex. Modify accordingly for other compilers. The code is copied from the definition of regular hyperlinks (\hyper@linkurl) where I changed the subtype, removed the action specification, and added the readonly flag.

\documentclass{amsart}
\usepackage{hyperref}
\makeatletter
\def\fakeurl#1{%
  \begingroup
    \hyper@chars
    \leavevmode
    \pdfstartlink
      attr{%
        \Hy@setpdfborder
        \Hy@setpdfhighlight
        \ifx\@urlbordercolor\relax
        \else
          /C[\@urlbordercolor]%
        \fi
      }%
      user{%
       /Subtype/Square%
       /F 64%
      }%
      \relax
    \Hy@colorlink\@urlcolor#1\Hy@xspace@end
    \close@pdflink
  \endgroup
}
\makeatother


\begin{document}
    \url{https://www.baidu.com}
    
    \vspace{5mm}
    \fakeurl{any piece of code}
\end{document}

Result:

enter image description here

The second box is not clickable and does not show up when printed for the viewers that I tested (Adobe Reader 9, Evince, Okular, Firefox pdf viewer).

You must log in to answer this question.

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