2

I need a hyperlink reference with % in it. Using \usepackage{hyperref} the format \href{https://www.wikibooks.org}{Wikibooks home} breaks down with a % in the url. How to avoid this problem?

  • 2
    you should be able to use \% – David Carlisle Apr 11 '18 at 19:49
  • 2
    \bgroup\catcode`\%=12\href{https://www.foo.bar%baz}{Foobar}\egroup should work, too. – Skillmon Apr 11 '18 at 19:51
  • 1
    See also the manual of the url package and in particular its \urldef command. – GuM Apr 11 '18 at 19:56
3

If \href or \url is not used inside the argument of other macros, then % just works, because hyperref reads the URL argument verbatim.

However, if \href or \url are used inside the argument of other commands, the category changes for verbatim category codes come too late, the string is already read by TeX. For this case, some problematic characters like % or # can be protected by escaping them with the backslash.

Example:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
  \url{http://example.org/a%20b.html}

  \href{http://example.org/a%20b.html}{a b}

  \textbf{\url{http://example.org/a\%20b.html}}

  \textbf{\href{http://example.org/a\%20b.html}{a b}}
\end{document}

The PDF file contains the correct URLs:

/Subtype/Link/A<</Type/Action/S/URI/URI(http://example.org/a%20b.html)>>
/Subtype/Link/A<</Type/Action/S/URI/URI(http://example.org/a%20b.html)>>
/Subtype/Link/A<</Type/Action/S/URI/URI(http://example.org/a%20b.html)>>
/Subtype/Link/A<</Type/Action/S/URI/URI(http://example.org/a%20b.html)>>
3

Three comments, three viable solutions: all work, at least with the pdftex driver.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{hyperref}

\urldef{\MYurl}\url{http://www.w3.org/file%query}



\begin{document}

See \url{http://www.w3.org/file\%query}.

See \MYurl.

See
\begingroup
    \catcode`\% = 12
    \url{http://www.w3.org/file%query}.
\endgroup

\showboxbreadth = 1000
\showboxdepth = 10
\showlists

\end{document}

The \showlists command will write in the transcript file a description of the internal lists generated by pdfTeX: you can check that the link is always correctly encoded.

Edit: As Heiko Oberdiek remarked, the above is not particulary exciting; more interesting is the fact that all those three solutions, including that based on \urldef, work inside the argument of a command too.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{hyperref}

\urldef{\MYurl}\url{http://www.w3.org/file%query}

\newcommand*{\WrapInArgument}[1]{%
    \typeout{}%
    \typeout{\unexpanded{#1}}%
    \typeout{}%
    #1%
}



\begin{document}

See \WrapInArgument{\url{http://www.w3.org/file\%query}}.

See \WrapInArgument{\MYurl}.

See
\begingroup
    \catcode`\% = 12
    \WrapInArgument{\url{http://www.w3.org/file%query}}.
\endgroup

\showboxbreadth = 1000
\showboxdepth = 10
\showlists

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.