0

I'm trying to achieve something I thought would be basic functionality. Just a hyperlink where it appears (in a code listing). It would be better if it was possible to follow the link for a reader. But it returns an error message and is a source of painful confusion.

\begin{lstlisting}[language=bash, breaklines,escapechar=|]
$ wget https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc_0.9.27+git20200814.62c30a4a.orig.tar.bz2

$ wget |\href{http://foo.com/MyFunctionDocs.html}{MyFunction}|(x);

$ wget |\href{http://foo.com/MyFunctionDocs.html}{https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc_0.9.27+git20200814.62c30a4a.orig.tar.bz2}|(x);

$ tar -xvjf tcc_0.9.27+git20200814.62c30a4a.orig.tar.bz2
$ gcc --version
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
$ ./configure --cc=gcc
$ make
$ make install
\end{lstlisting}

How it looks (without the hyperlink).

enter image description here

The error message seems to have little or nothing to do with what I try to do.

Missing $ inserted.

 
‪./sections/3-method.tex, 110‬
You need to enclose all mathematical expressions and symbols with special markers. These special markers create a ‘math mode’.

Use $...$ for inline math mode, and \[...\]or one of the mathematical environments (e.g. equation) for display math mode.

This applies to symbols such as subscripts ( _ ), integrals ( \int ), Greek letters ( \alpha, \beta, \delta ) and modifiers (\vec{x}, \tilde{x}).

Learn more
<inserted text> 
                $
l.110 ...0.9.27+git20200814.62c30a4a.orig.tar.bz2}
                                                  |(x);
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.

! Missing $ inserted.
<inserted text> 
                $
l.110 ...0.9.27+git20200814.62c30a4a.orig.tar.bz2}
                                                  |(x);
I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.
 Expand

The typography is broken. It seems to be because TeX tries to be smart and force me into math mode which has nothing to do with what I'm doing. The "solution" I used was from Inline links in code listings

enter image description here

The following attempts also failed miserably with broken typography.

$ wget |\href{http://foo.com/MyFunctionDocs.html}{https://launchpad.net/ubuntu/+archive}|

$ wget |\url{https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc_0.9.27+git20200814.62c30a4a.orig.tar.bz2}|;

Minimal not working example follows. It is easy to see that nothing works. The rendering is broken in all attempts to create such a simple feature as a hyperlink.

\begin{lstlisting}[language=bash, breaklines,escapechar=|]
$ wget https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc_0.9.27+git20200814.62c30a4a.orig.tar.bz2
$ wget |\href{https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc_0.9.27+git20200814.62c30a4a.orig.tar.bz2}{https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc\_0.9.27+git20200814.62c30a4a.orig.tar.bz2}|
$ wget |\href{https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc\_0.9.27+git20200814.62c30a4a.orig.tar.bz2}{\nolinkurl{https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc\_0.9.27+git20200814.62c30a4a.orig.tar.bz2}}|
$ wget |\url{https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/tcc/0.9.27+git20200814.62c30a4a-1/tcc_0.9.27+git20200814.62c30a4a.orig.tar.bz2}|
\end{lstlisting}

enter image description here

8

2 Answers 2

1

You get the error because of the underscore in the text. Underscores are only allowed in math mode and so TeX switches to it. Either escape the underscore as \_, or use \nolinkurl or \url (the last naturally produce a different link).


\documentclass{article}
\usepackage{listings}
\usepackage{hyperref}
\begin{document}
\begin{lstlisting}[language=bash, breaklines,escapechar=|]
$ wget |\href{http://foo.com/MyFunctionDocs.html}{https://...tcc\_0.9.27}|(x);

$ wget |\href{http://foo.com/MyFunctionDocs.html}{\nolinkurl{https://...tcc_0.9.27}}|(x);

$ wget |\url{https://...tcc_0.9.27}|(x)
\end{lstlisting}
\end{document}

enter image description here

2
  • But it does not render with the same font as the rest of the commands :-( Oct 30, 2022 at 17:23
  • You didn't provide a complete example so I can't know which font you are actually using and which one you want. But you can adapt the fonts, see the docu of url. Oct 30, 2022 at 17:31
0

I adapted the second answer in the linked question a bit. This should leave the font correct.

%! TEX program = lualatex

\documentclass{article}
\usepackage{listings}
\usepackage[margin=1cm]{geometry}
\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

\begin{document}
\begin{lstlisting}[language=bash, breaklines, escapechar=|, moredelim={**[is][\btHR]{@}{@}}]
$ wget |\btSetUrl{http://foo.com/MyFunctionDocs.html}|@https://...tcc_0.9.27@(x); # with the link
$ wget https://...tcc_0.9.27(x); # without the link
$ wget |\href{http://foo.com/MyFunctionDocs.html}{https://...tcc\_0.9.27}|(x); # Ulrike's answer, wrong spacing
\end{lstlisting}


\end{document}

output

1
  • (MWE taken from Ulrike's answer with modifications.)
    – user202729
    Oct 31, 2022 at 8:57

You must log in to answer this question.

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