1

In preparing answer for some mathematics question, I link the answer to some Wolfram Alpha link for checking. The error appears when I put some Wolfram Alpha link in inside a \href, in turn, inside a \marginnote. I just guess the reason is somme % character in the link.

Could anyone help me about these?

enter image description here

\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}
2
  • 1
    Sorry, but you need to escape your %'s.
    – egreg
    Aug 31, 2021 at 9:20
  • @egreg Thank you! that is a bad news! % is quite popular in web links
    – Black Mild
    Aug 31, 2021 at 9:32

2 Answers 2

1

href has no chance to change the catcode of % if you use it in the argument of another command. But you could store the url first in a macro:

\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}
1
  • Thanks! that is definitely a comfortable way to go
    – Black Mild
    Aug 31, 2021 at 10:29
1

You can define a \marginhref command:

\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}

enter image description here

1
  • Thank you! it works well!
    – Black Mild
    Aug 31, 2021 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .