
Если 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}
}
С комментарием к оскорбительной строке я получаю:
Это отлично работает для 1-го и 3-го случая, но не для того, где есть специальные символы 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}