Escapen von Sonderzeichen zur Verwendung als URL

Escapen von Sonderzeichen zur Verwendung als URL

Wenn eine URL Sonderzeichen ( ~und #) enthält, verwenden Sie sie als

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

funktioniert einwandfrei. Aber ich hätte gerne eine Makroliste, die diese URLs enthält und sie zu einem späteren Zeitpunkt verarbeiten kann, wie ich es inVerwenden Sie eine \foreach-Schleife, um ein Makro mit den in einer Liste angegebenen Makroparametern auszuführen. Ich würde also gerne etwas sagen können wie:

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

Mit dem Kommentar zu der anstößigen Zeile erhalte ich:

Bildbeschreibung hier eingeben

Dies funktioniert im 1. und 3. Fall einwandfrei, aber nicht bei den speziellen TeX-Zeichen. Ich habe versucht, sie zu maskieren, aber das funktioniert nicht ganz, da die Links nicht anklickbar sind.

Was mir also nicht klar ist, ist, wie ich die Sonderzeichen in einer solchen Liste maskieren kann.

Code

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

Antwort1

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

verwandte Informationen