我正在尝试创建一个类,除其他功能外,它还根据传递给环境的参数生成 URL。出于某种奇怪的原因,如果 URL 字符串包含/
,hyperref
则会尝试创建一个指向名为 的文件的 URL <URL>.pdf
。
\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{hyperref}
\ExplSyntaxOn
\keys_define:nn { mydocument / myenvironment }
{
link .tl_set:N = \l_myenvironment_link_tl,
print .bool_set:N = \l_myenvironment_print_bool,
}
\NewDocumentEnvironment { myenvironment } { m }
{
\keys_set:nn { mydocument / myenvironment } { #1 }
\minipage{\textwidth}
\tl_clear_new:N \l_item_url_tl
\tl_put_left:Nn \l_item_url_tl { https:// }
\tl_put_right:Nx \l_item_url_tl { \l_myenvironment_link_tl }
\href{\l_item_url_tl}{TeX.StackExchange} \\
\url{\l_item_url_tl} \\
}
{
\endminipage
}
\ExplSyntaxOff
\begin{document}
\begin{myenvironment}
{
link = tex.stackexchange.com,
}
Here is some content in my environment with a good link
\end{myenvironment}
\begin{myenvironment}
{
link = tex.stackexchange.com/users/3351/robbie,
}
Here is some content in my environment with a \textbf{bad} link
\end{myenvironment}
\end{document}
上面的 MWE 展示了我所描述的行为:环境的第一个实例有一个指向 的链接https://tex.stackexchange.com
,但第二个实例创建了一个指向 的链接https://tex.stackexchange.com/users/3351/robbie.pdf
。奇怪的是,如果我使用 ,它不会破坏 URL \url
,但我无法在我的文档中使用该命令,因为链接文本也会生成。
为什么会这样?我该如何让它使用超链接目标中的原始 URL 值?
答案1
hyperref 有一个启发式方法来决定链接类型。如果冒号在文本中含有 catcode 字母而不是其他字母,它就会感到困惑http://
。
\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{hyperref}
\ExplSyntaxOn
\keys_define:nn { mydocument / myenvironment }
{
link .tl_set:N = \l_myenvironment_link_tl,
print .bool_set:N = \l_myenvironment_print_bool,
}
\NewDocumentEnvironment { myenvironment } { m }
{
\keys_set:nn { mydocument / myenvironment } { #1 }
\minipage{\textwidth}
\tl_clear_new:N \l_item_url_tl
\tl_put_left:Nx \l_item_url_tl { http\c_colon_str// }
\tl_put_right:Nx \l_item_url_tl { \l_myenvironment_link_tl }
\href{\l_item_url_tl}{TeX.StackExchange} \\
\url{\l_item_url_tl} \\
}
{
\endminipage
}
\ExplSyntaxOff
\begin{document}
\begin{myenvironment}
{
link = tex.stackexchange.com,
}
Here is some content in my environment with a good link
\end{myenvironment}
\begin{myenvironment}
{
link = tex.stackexchange.com/users/3351/robbie,
}
Here is some content in my environment with a \textbf{bad} link
\end{myenvironment}
\end{document}