Passando caracteres especiais de escape para uso como URL por meio de macros

Passando caracteres especiais de escape para uso como URL por meio de macros

De uma pergunta anterior emEscapando caracteres especiais para uso como URL, usar \stringpara escapar dos caracteres especiais do URL no segundo exemplo funciona muito bem:

\DefineMyFormatLinkParameters{%
    *[Main Search Site]{Google}{http://www.google.com},
    {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}
}

\DefineMyFormatLinkParametersdefine uma lista de parâmetros em \MyFormatLinkParameters:

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

No entanto, se eu usar

\newcommand*{\DefineMyFormatLinkParametersNew}[2][TypeB]{%
    %\edef\MyFormatLinkParametersNew{#2}% Don't assign it here directly, 
                                        % but pass it along 
    \IfStrEqCase{#1}{%
        {TypeA}{\DefineMyFormatLinkParametersTypeA{#2}}%
        {TypeB}{\DefineMyFormatLinkParametersTypeB{#2}}%
    }[\typeout{*** ERROR: Unknown link type: #1}]%
}%

que passa a lista de parâmetros para outra macro, \DefineMyFormatLinkParametersTypeAou \DefineMyFormatLinkParametersTypeB, há um problema no processamento dos caracteres especiais. As coisas funcionam bem para casos que não usam caracteres especiais do TeX conforme o MWE abaixo:

insira a descrição da imagem aqui

Notas

  • No MWE, o caso do problema é comentado na chamada para \DefineMyFormatLinkParametersNew. Remova o comentário da people.brunel.ac.uklinha para reproduzir o problema.

Código:

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


%% \DefineMyFormatLinkParametersNew (compared to \DefineMyFormatLinkParameters) has 
%% one extra level of macro processing based on the optional #1 parameter
\newcommand*{\Title}{}%
\newcommand*{\DefineMyFormatLinkParametersTypeA}[1]{%
    \edef\MyFormatLinkParametersNew{#1}%
    \def\Title{Type A Links}%
}%
\newcommand*{\DefineMyFormatLinkParametersTypeB}[1]{%
    \edef\MyFormatLinkParametersNew{#1}%
    \def\Title{Type B Links}%
}%
\newcommand*{\DefineMyFormatLinkParametersNew}[2][TypeB]{%
    %\edef\MyFormatLinkParametersNew{#2}% Don't assign it here directly, 
                                        % but pass it along 
    \IfStrEqCase{#1}{%
        {TypeA}{\DefineMyFormatLinkParametersTypeA{#2}}%
        {TypeB}{\DefineMyFormatLinkParametersTypeB{#2}}%
    }[\typeout{*** ERROR: Unknown link type: #1}]%
}%


\begin{document}
% ------------------------------- First version: works fine.
\DefineMyFormatLinkParameters{%
    *[Main Search Site]{Google}{http://www.google.com},
    {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}
}

\foreach \x in \MyFormatLinkParameters {%
    \typeout{DEBUG: "\x"}
    \expandafter\FormatLinks\x
}%


%------------------------------- Second version: Not working if uncomment 
%                                people.burnel.ac.uk.  Only difference
%                                is that it has one extra level of macro
%                                processing which causes some problem!
\DefineMyFormatLinkParametersNew[TypeB]{%
    *[Main Search Site]{Google}{http://www.google.com},
    %{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\Title:% 
\foreach \x in \MyFormatLinkParametersNew {%
    \typeout{DEBUG: "\x"}
    \expandafter\FormatLinks\x
}

\end{document}

Responder1

Não tenho certeza se usaria xstring se você já possui l3 e pgf carregados, mas de qualquer maneira:

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


%% \DefineMyFormatLinkParametersNew (compared to \DefineMyFormatLinkParameters) has 
%% one extra level of macro processing based on the optional #1 parameter
\newcommand*{\Title}{}%
\newcommand*{\DefineMyFormatLinkParametersTypeA}[1]{%
    \edef\MyFormatLinkParametersNew{#1}%
    \def\Title{Type A Links}%
}%
\newcommand*{\DefineMyFormatLinkParametersTypeB}[1]{%
    \edef\MyFormatLinkParametersNew{#1}%
    \def\Title{Type B Links}%
}%
\newcommand*{\DefineMyFormatLinkParametersNew}[2][TypeB]{%
    %\edef\MyFormatLinkParametersNew{#2}% Don't assign it here directly, 
                                        % but pass it along 
\edef\tempz{\unexpanded{#2}}%
    \IfStrEqCase{#1}{%
        {TypeA}{\DefineMyFormatLinkParametersTypeA{\tempz}}%
        {TypeB}{\DefineMyFormatLinkParametersTypeB{\tempz}}%
    }[\typeout{*** ERROR: Unknown link type: #1}]%
}%


\begin{document}
% ------------------------------- First version: works fine.
\DefineMyFormatLinkParameters{%
    *[Main Search Site]{Google}{http://www.google.com},
    {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}
}

\foreach \x in \MyFormatLinkParameters {%
    \typeout{DEBUG: "\x"}
    \expandafter\FormatLinks\x
}%


%------------------------------- Second version: Not working if uncomment 
%                                people.burnel.ac.uk.  Only difference
%                                is that it has one extra level of macro
%                                processing which causes some problem!
\DefineMyFormatLinkParametersNew[TypeB]{%
    *[Main Search Site]{Google}{http://www.google.com},
    {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\Title:% 
\foreach \x in \MyFormatLinkParametersNew {%
    \typeout{DEBUG: "\x"}
    \expandafter\FormatLinks\x
}

\end{document}

informação relacionada