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?

4
  • 1
    Do you really need verbatim or is \texttt{} enough for you?
    – Sigur
    Dec 10, 2014 at 16:48
  • 3
    I'd try \cprotect{\href{www.numpy.org}}{\verb|numpy|}, but \href{www.numpy.org}{\texttt{numpy}} is much easier.
    – egreg
    Dec 10, 2014 at 16:48
  • Possible duplicate: How to put \verb command inside of \textbf block? (different error message, but same cause/solution).
    – Werner
    Dec 10, 2014 at 17:15
  • @Werner No, the OP already know about \cprotect.
    – egreg
    Dec 10, 2014 at 17:25

2 Answers 2

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

2
  • Thanks. It works for me without the http:// though, should it not?
    – will
    Dec 10, 2014 at 18:47
  • 1
    @will This depends on features of the PDF viewer. It doesn't with Skim, for instance.
    – egreg
    Dec 10, 2014 at 19:01
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

2
  • A comment here to remind myself that within beamer, \begin{myverbbox} should be put outside of a \begin{frame}{Title} environment.
    – Matsmath
    Nov 1, 2022 at 8: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 2, 2023 at 4:14

You must log in to answer this question.

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