轉義特殊字元以用作 URL

轉義特殊字元以用作 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}

相關內容