How can I store a URL in a macro if the URL has a #
and/or a %
in it? The MWE below works fine for the first two, but not the third one.
Notes:
One solution is to replace the
#
with\string#
and the%
with\%
, but wodering if there is an easier way.I realize that this may mess up the syntax highliting, but the
\csdef{}
section shown here will be in a separte.def
file that will be be\input{}
, so not an issue.
Reference:
Code
\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{tikz}
\csdef{url Title 1}{The Google}
\csdef{url Link 1}{http://www.google.com}
\csdef{url Title 2}{TeX.SE}
\csdef{url Link 2}{http://tex.stackexchange.com}
%\csdef{url Title 3}{Algebra and Trigonometry:~History of Trigonometry}
%\csdef{url Link 3}{https://books.google.com/books?id=4tFFDwAAQBAJ&pg=PT233&dq=history+of+trigonometry&hl=en&newbks=1&newbks_redir=0&sa=X&ved=2ahUKEwjIzcj0-tnqAhXwFjQIHZ6XDLUQ6AEwAnoECAQQAg#v=onepage&q&f=false}
\def\MaxNUmberOfURLs{5}
\newcommand{\DisplayURLs}{%
\textbf{List of URLs}
\foreach \xCount in {1,...,\MaxNUmberOfURLs} {%
\ifcsdef{url Title \xCount}{%
\par\xCount:
\href{\csuse{url Link \xCount}}{\csuse{url Title \xCount}}
}{}%
}%
}
\begin{document}
\DisplayURLs
\end{document}
\csedef{url Link 3}{\detokenize{https://books.google.com/books?id=4tFFDwAAQBAJ&pg=PT233&dq=history+of+trigonometry&hl=en&newbks=1&newbks_redir=0&sa=X&ved=2ahUKEwjIzcj0-tnqAhXwFjQIHZ6XDLUQ6AEwAnoECAQQAg#v=onepage&q&f=false}}
should work. edit Hrmpf: It works in the MWE, but breaks with%
.#
and/or a%
in it?" Isn't this precisely what the\urldef
macro of theurl
package is supposed to do? E.g.,\urldef{\myURL}\url{https://books.google.com/books?id=4tFFDwAAQBAJ&pg=PT233&dq=history+of+trigonometry&hl=en&newbks=1&newbks_redir=0&sa=X&ved=2ahUKEwjIzcj0-tnqAhXwFjQIHZ6XDLUQ6AEwAnoECAQQAg#v=onepage&q&f=false}
\csdefurl
defined in moewe's answer works great.