我正在尝试实现一些我认为是基本功能的东西。只是一个出现位置的超链接(在代码列表中)。如果读者可以点击链接就更好了。但它会返回一条错误消息,并造成痛苦的混乱。
\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}
它看起来如何(没有超链接)。
该错误消息似乎与我尝试执行的操作几乎没有任何关系。
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
排版有问题。这似乎是因为 TeX 试图变得聪明,强迫我进入数学模式,而这与我正在做的事情无关。我使用的“解决方案”来自代码清单中的内联链接
随后的尝试也因字体损坏而失败。
$ 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}|;
下面是一个最小的不起作用的示例。很容易看出什么都不起作用。在创建超链接等简单功能的所有尝试中,渲染都失败了。
\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}
答案1
由于文本中的下划线,您会收到错误。下划线仅在数学模式下允许,因此 TeX 会切换到该模式。要么将下划线转义为\_
,要么使用\nolinkurl
或\url
(最后一个自然会产生不同的链接)。
\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}
答案2
我适应了链接问题中的第二个答案有点。这样字体就正确了。
%! 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}