在准备一些数学问题的答案时,我将答案链接到一些 Wolfram Alpha 链接进行检查。当我将一些 Wolfram Alpha 链接放在 中\href
,反过来又放在 中时,会出现错误\marginnote
。我猜原因是%
链接中的一些字符。
有人能帮助我解决这些问题吗?
\documentclass{article}
\usepackage{hyperref}
\usepackage{lipsum}
\usepackage{marginnote}
\begin{document}
\lipsum[1]
\hfill \href{https://www.wolframalpha.com/input/?i=%5B%5B4%2C6%2C6%5D%2C%5B1%2C3%2C2%5D%2C%5B-1%2C-5%2C-2%5D%5D}{Check Wolfram Alpha} % not sure working properly, but seems OK!
\lipsum[2]
\marginnote{\href{www.ams.org}{www.ams.org}} % OK!
\lipsum[3]
\marginnote{\href{https://www.wolframalpha.com/input/?i=%5B%5B4%2C6%2C6%5D%2C%5B1%2C3%2C2%5D%2C%5B-1%2C-5%2C-2%5D%5D}{Check Wolfram Alpha}} %<<< error. Comment this line to get a MWE.
\end{document}
答案1
%
如果您在另一个命令的参数中使用href,则它无法更改 catcode 。但您可以先将 url 存储在宏中:
\documentclass{article}
\usepackage{hyperref}
\usepackage{lipsum}
\usepackage{marginnote}
\makeatletter
\newcommand\saveurl[1]{\begingroup\catcode`\%=12 \catcode`\#=12 \@saveurl#1}
\newcommand\@saveurl[2]{\endgroup\newcommand#1{#2}}
\makeatother
\saveurl\myurl{https://www.wolframalpha.com/input/?i=%5B%5B4%2C6%2C6%5D%2C%5B1%2C3%2C2%5D%2C%5B-1%2C-5%2C-2%5D%5D}
\begin{document}
\lipsum[1]
\hfill \href{\myurl}{Check Wolfram Alpha} %
\lipsum[2]
\marginnote{\href{www.ams.org}{www.ams.org}} % OK!
\lipsum[3]
\marginnote{\href{\myurl}{Check Wolfram Alpha}} %
\end{document}
答案2
您可以定义一个\marginhref
命令:
\documentclass{article}
\usepackage{marginnote}
\usepackage{hyperref}
\makeatletter % we want to use private commands
\newcommand{\marginhref}{%
\begingroup
\@makeother\%\@makeother\#%
\@makemarginhref
}
\def\@makemarginhref#1#2{%
\marginnote{\href{#1}{#2}}%
\endgroup
}
\makeatother
\begin{document}
This has a link
\href{https://www.wolframalpha.com/input/?i=%5B%5B4%2C6%2C6%5D%2C%5B1%2C3%2C2%5D%2C%5B-1%2C-5%2C-2%5D%5D}{Check Wolfram Alpha} % not sure working properly, but seems OK!
This has a link in the margin\marginnote{\href{www.ams.org}{www.ams.org}} % OK!
Also this has a link in the margin
\marginhref{https://www.wolframalpha.com/input/?i=%5B%5B4%2C6%2C6%5D%2C%5B1%2C3%2C2%5D%2C%5B-1%2C-5%2C-2%5D%5D}{Check Wolfram Alpha}
\end{document}