转义特殊字符以用作 URL

转义特殊字符以用作 URL

如果网址包含特殊字符(~#),则将其用作

\href{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32}{people.brunel.ac.uk}

运行正常。但是,我希望有一个包含这些 URL 的宏列表,并在稍后处理它们,就像我在使用 \foreach 循环执行宏,宏的参数以列表形式提供。因此,我希望能够这样说:

\DefineMyFormatLinkParameters{%
    *[Main Search Site]{Google}{http://www.google.com},
    [people.brunel.ac.uk]{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32},
    {Yahoo}{http://www.yahoo.com}
}

随着冒犯性评论的出现,我得到了:

在此处输入图片描述

对于第一种和第三种情况,这种方法很有效,但对于带有特殊 TeX 字符的情况,这种方法就不行了。我尝试过转义这些字符,但效果并不好,因为链接无法点击。

那么,我不知道该如何转义此类列表中的特殊字符?

代码

\documentclass{article}
\usepackage{url}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{xstring}
\usepackage[colorlinks=true]{hyperref}

\NewDocumentCommand{\FormatLinks}{%
    s%   #1 =* not used yet
    O{}% #2 = optional title
    m%   #3 = Mandatory title
    m%   #4 = URL Link
}{%
    \par
    \hspace*{1.0cm}\href{#4}{#3\IfValueT{#2}{~#2}}%
}%

\newcommand*{\MyFormatLinkParameters}{}% Initialize
\newcommand*{\DefineMyFormatLinkParameters}[1]{%
    \edef\MyFormatLinkParameters{#1}%
}%

\begin{document}
\FormatLinks*[Main Search Site]{Google}{http://www.google.com}
\par\hspace*{1.0cm}\href{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32}{people.brunel.ac.uk}
\FormatLinks{Yahoo}{http://www.yahoo.com}

% Prefer to define a list, and later execute the list:
\DefineMyFormatLinkParameters{%
    *[Main Search Site]{Google}{http://www.google.com},
    %% What changes do I need to make to the following URL to get it pass through the macros.
    % [people.brunel.ac.uk]{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32},
    {Yahoo}{http://www.yahoo.com}
}

\bigskip% 
Following should produce same results as above:\medskip\par
\foreach \x in \MyFormatLinkParameters {%
    \typeout{DEBUG: "\x"}
    \expandafter\FormatLinks\x
}

\end{document}

答案1

\documentclass{article}
\usepackage{url}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{xstring}
\usepackage[colorlinks=true]{hyperref}

\NewDocumentCommand{\FormatLinks}{%
    s%   #1 =* not used yet
    O{}% #2 = optional title
    m%   #3 = Mandatory title
    m%   #4 = URL Link
}{%
    \par
    \hspace*{1.0cm}\href{#4}{#3\IfValueT{#2}{~#2}}%
}%

\newcommand*{\MyFormatLinkParameters}{}% Initialize
\newcommand*{\DefineMyFormatLinkParameters}[1]{%
    \edef\MyFormatLinkParameters{#1}%
}%

\begin{document}
\FormatLinks*[Main Search Site]{Google}{http://www.google.com}
\par\hspace*{1.0cm}\href{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32}{people.brunel.ac.uk}
\FormatLinks{Yahoo}{http://www.yahoo.com}

% Prefer to define a list, and later execute the list:
\DefineMyFormatLinkParameters{%
    *[Main Search Site]{Google}{http://www.google.com},
    %% What changes do I need to make to the following URL to get it pass through the macros.
    {people.brunel.ac.uk}{http://people.brunel.ac.uk/\string~mastmmg/ssguide/set\string_work.html\string#4\string_32},
    {Yahoo}{http://www.yahoo.com}
}

\bigskip% 
Following should produce same results as above:\medskip\par
\foreach \x in \MyFormatLinkParameters {%
    \typeout{DEBUG: "\x"}
    \expandafter\FormatLinks\x
}

\end{document}

相关内容