5

Let's say I have a document with a listings. And I want to convert that listing into a hyperlink (e.g. to the source code).

The first instinctive attempt will be something like:

\documentclass{article}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\lstset{basicstyle=\ttfamily,}
\begin{document}
  \href{file}{
    \begin{lstlisting}[columns=fullflexible, backgroundcolor=\color{black!20}]
      code code
    \end{lstlisting}
  }
\end{document}

This fails because of know limitation (see Strange interaction between hyperref, listings and animate)

The second attempt will be to create a escape inside the code:

\begin{lstlisting}[columns=fullflexible, backgroundcolor=\color{black!20}, escapeinside={\%*}{*)}]
   %*\href{file}{Link}*)code code
\end{lstlisting}

(Note, above the line starting with % is not a comment because but a escape combination in a verbatim/lstlistings)

This works but still doesn't cover the whole environment (or at least something that covers most of the line).

I need something like this (fake code to make the typeset code into a link)

\begin{lstlisting}[columns=fullflexible, backgroundcolor=\color{black!20}, escapeinside={\%*}{*)}]
   "\href"{file}{code code}
\end{lstlisting}

How can I convert most of the listings line into a hyperlink? I am mostly interested in one-line (or very short) listings.

alfC
  • 14,350
  • What do you mean by "most of the listing"? The entire frame? Just the text? A selection of lines? – jub0bs Nov 22 '14 at 11:57
  • @Jubobs, ideally the whole frame (or the whole line). The code text part only (in case it is shorter than the whole line) is also acceptable. It looks like your (great) answer does the second. – alfC Nov 22 '14 at 12:22
  • Ok. See my edit. – jub0bs Nov 22 '14 at 13:11

1 Answers1

4

The following approach (borrowed from egreg's answer) allows you to turn the text of the listing (but not the whole frame/box) into a hyperlink. First, save the listing in an lrbox; then, use the latter in the second argument of \href:

enter image description here

\documentclass{article}

\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}

\newsavebox\lstA

\lstset{basicstyle=\ttfamily,}

\begin{document}
\begin{lrbox}{\lstA}
\begin{lstlisting}[
    columns=fullflexible,
    backgroundcolor=\color{black!20},
    gobble=4,
]
    code code
\end{lstlisting}
\end{lrbox}
\href{http://www.google.com}{\usebox{\lstA}}
\end{document}

Edit: with the same approach but using tcolorbox instead of listings, you can turn the whole box into a hyperlink. You may have to tweak the box's appearance to your liking, but if you're going to typeset your listings inside fancy boxes, you're better off using tcolorbox anyway.

enter image description here

\documentclass{article}

\usepackage{hyperref}
\usepackage[most]{tcolorbox}
\newtcblisting{mylisting}{%
  spartan,
  boxrule = 0pt,
  colback = black!20,
  listing only,
  listing options = {%
    basicstyle = \ttfamily,
    columns    = fullflexible,
    gobble     = 4,
  }
}
\newsavebox\lstA

\begin{document}
\begin{lrbox}{\lstA}
\begin{mylisting}
code code
code code
\end{mylisting}
\end{lrbox}
\href{http://www.google.com}{\usebox{\lstA}}
\end{document}
jub0bs
  • 58,916
  • The tcolorbox solution doesn't compile. I see this Library (tcolorbox): 'tcblistings.code.tex' version '2.60' (/usr/share/texlive/texmf-dist/tex/latex/tcolorbox/tcblistingscore.code.tex Library (tcolorbox): 'tcblistingscore.code.tex' version '2.60' )))) (./hyperref.aux) and after a few lines the interactive LaTeX prompt. The first solution works as expected. – alfC Nov 23 '14 at 19:00
  • @alfC The second solution works fine for me. Is your tex distribution up to date? – jub0bs Nov 23 '14 at 19:11
  • Maybe not. This is pdfTeX, Version 3.1415926-2.6-1.40.14 (TeX Live 2014/dev) (from Fedora 20). tcbox is 2.60. I'll try again after upgrading the system in a few weeks. – alfC Nov 23 '14 at 19:18