9

I would like to include links inside source code listings by providing URLs within the listing source text.

That is, I would like to be able to generate something that looks like the following:

enter image description here

with source code something like the following:

\begin{lstlisting}
int main(){
int x;
\href{http://foo.com/MyFunctionDocs.html}{MyFunction}(x);
}
\end{lstlisting}

The pink text is a hyperlink to an arbitrary URL. This would ideally be compatible with the listings package, but any well-supported verbatim-style environment would be acceptable. How can I accomplish this?

5
+50

The simplest method I can think of is to just use listings's escapechar key. This requires choosing a character that doesn't appear in the code. For example, I've used the pipe character | below, but this would need to be changed to something else if the code fragment contained, say, a logical or bitwise or operation.

\documentclass{article}

\usepackage{listings}
\usepackage[colorlinks]{hyperref}

\begin{document}

\begin{lstlisting}[language=C,escapechar=|]
int main(){
int x;
|\href{http://foo.com/MyFunctionDocs.html}{MyFunction}|(x);
}
\end{lstlisting}

\end{document}

This produces:

image of result

5

The following employs moredelim and some boxing to have any (syntax-formatted) part of a listing as hyperlink:

int main(){
  @foo@(); // <-- identifier is hyperlink
}

The link target (URL) is specified with the macro \btSetUrl{ theurl }. You could setup it once or redefine it inside the listing using any of listings's escape-to-latex options. In the example, I simply use mathescape, so that $\btSetUrl{https://tex.stackexchange.com}$ inside the listing sets the URL.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor,beramono}
\usepackage{tikz}
\usepackage{hyperref}


\makeatletter
\gdef\bt@HR@url{https://tex.stackexchange.com}
\newcommand\btSetUrl[1]{\gdef\bt@HR@url{#1}}
\newenvironment{btHyperref}
{\begingroup\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HR@box{\@tempboxa}\endgroup}

\newcommand\btHR{%
  \begin{btHyperref}\bgroup\aftergroup\bt@HR@endenv%
}
\def\bt@HR@endenv{%
  \end{btHyperref}%   
  \egroup
}
\newcommand{\bt@HR@box}[1]{%
  \href{\bt@HR@url}{\usebox{#1}}%
}
\makeatother

\lstdefinestyle{C}{
    language={C},basicstyle=\ttfamily, 
    mathescape,
    moredelim=**[is][\btHR]{@}{@},
}

\begin{document}

\begin{lstlisting}[style=C]
int main(){
  int x;
  $\btSetUrl{http://tex.stackexchange.com/questions/314903/inline-links-in-code-listings}$@MyFunction@( x ); // <-- links to cool question
  $\btSetUrl{http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c}$@return 0@;        // <-- never forget!
}
\end{lstlisting}

\end{document}

enter image description here

  • 4
    The advantage over the (much easier) method suggested by Nicola is that the hyperlinked element is still syntax-formatted (such as the return keyword in the example.) – Daniel Jun 20 '16 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.