使用 URL 作为参数定义宏

使用 URL 作为参数定义宏

我想定义类似的东西

\newcommand{\myhref}[2]{\href{#1}{do something with #1}}

然后将网络上的 URL 粘贴到 中\myhref{...}{hello}

URL 中的特殊字符意味着只有我修复每个 URL(通常在特殊字符前加上 \ )才能起作用。如果我可以告诉myhref继承将参数 #1 中的 URL 视为 just-as-if-pass-verbatim from/to 的规则,那就更令人愉快了href

这个问题与自定义 \href 命令中使用“#”的问题。但是,我需要一个更通用的解决方案。不幸的是,URL 不能只包含 # ,还可能包含 _ 和 & 以及可能还有一些特殊字符。有没有更通用的解决方案?

答案1

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\DeclareRobustCommand*{\myurl}{\hyper@normalise\myurl@}
\def\myurl@#1{\hyper@linkurl{See also: \textbf{#1}}{#1}}
\makeatother
\begin{document}

\myurl{https://github.com/latex3/hyperref/issues?q=is%3Aissue+is%3Aopen+tuburl}

\end{document}

相关内容