13

Is there a way i can have verbatim text inside a link?

\href{www.numpy.org}{\verb|numpy|} does not work. I tried using cprotect;

\cprotect\href{www.numpy.org}{\verb|numpy|}, but then I end up with !TeX capacity exceeded, sorry [input stack size=5000].

Is there a simple way to do this?

will
  • 317
  • 2
  • 11

2 Answers2

15

You have to brace \href{www.numpy.org}:

\documentclass{article}
\usepackage{cprotect}
\usepackage{hyperref}

\begin{document}

With \texttt{\string\verb}: \cprotect{\href{http://www.numpy.org}}{\verb|numpy|}

With \texttt{\string\texttt}: \href{http://www.numpy.org}{\texttt{numpy}}

\end{document}

However, using \cprotect doesn't seem necessary here, as the example with \texttt shows.

Don't forget http:// or the link won't work.

enter image description here

egreg
  • 1,121,712
5

Here's a way using a verbatimbox.

\documentclass{article}
\usepackage{verbatimbox,hyperref}
\begin{document}
\begin{myverbbox}{\numpy}n$_umpy\end{myverbbox}
And now we \href{www.numpy.org}{\numpy}\,.
\end{document}

enter image description here

  • A comment here to remind myself that within beamer, \begin{myverbbox} should be put outside of a \begin{frame}{Title} environment. – Matsmath Nov 01 '22 at 08:08
  • 1
    This is the best solution I've found after many hrs googling for an answer. Nothing else I tried worked. Thanks. – Nasser Oct 02 '23 at 04:14