![URL として使用するために特殊文字をエスケープする](https://rvso.com/image/309846/URL%20%E3%81%A8%E3%81%97%E3%81%A6%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%81%AB%E7%89%B9%E6%AE%8A%E6%96%87%E5%AD%97%E3%82%92%E3%82%A8%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%97%E3%81%99%E3%82%8B.png)
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}