带有多个 ## 的 href 链接无法使用 backslah 转义成功转义

带有多个 ## 的 href 链接无法使用 backslah 转义成功转义

我尝试使用 LaTeX 创建一个使用 href 的超链接,其中 url 包含多个 # 符号。我完全知道特殊字符必须转义,例如 % 在链接中甚至相当常见。但是今天我遇到了一个包含两个 # 的链接,不知何故我只能转义第一个,转义第二个不起作用(我尝试的所有解决方案都以某种方式破坏了文档)。这是我忽略的明显的东西吗(即使一个友好的 LaTeX 用户也无法让它运行)还是我不知道的黑魔法?问题如下,下面通过 overleaf 分享了一个示例:

\href{http://www.example.com/hey\#foo}{This link is not broken (should be http://www.example.com/hey\#foo)}
The above works, only one hashtag has to be escaped
\href{http://www.example.com/hey\#foo\#bar}{This link is broken (should be http://www.example.com/hey\#foo\#bar but is http://www.example.com/hey\#foo)}
This does not work, as two hashes would have to be escaped

背面的例子:Overleaf 链接,也在评论中

答案1

嗯,hyperref 并不期望这样,拆分代码会丢弃第二个哈希值之后的所有内容。您可以改用带有 catcode 字母的哈希值:

\documentclass{article}
\usepackage{hyperref}


\begin{document}
\ExplSyntaxOn
\tl_new:N\hashletter
\tl_set:Nx\hashletter{\char_generate:nn{35}{11}}
\ExplSyntaxOff

\href{http://www.example.com/hey#foo\hashletter bar}{blub}

\end{document}

您可以使用新的 pdfmanagement 来\hrefurl代替。它不会试图耍小聪明并拆分 url:

\RequirePackage{pdfmanagement-testphase} %needed until next latex
\DocumentMetadata{}
\documentclass{article}
\usepackage{hyperref}

\begin{document}

\hrefurl{http://www.example.com/hey#foo#bar}{blub}

\end{document}

相关内容