3

From an earlier question on Escaping special characters for use as a URL, using \string to escape the special URL characters in the second example works great:

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

\DefineMyFormatLinkParameters defines a list of parameters in\MyFormatLinkParameters:

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

However, if I instead use

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

which passes the list of parameters to another macro, \DefineMyFormatLinkParametersTypeA or \DefineMyFormatLinkParametersTypeB, there is a problem in processing the special characters. Things works fine for cases which do not use special TeX characters as per the MWE below:

enter image description here

Notes

  • In the MWE, the problem case is commented out in the call to \DefineMyFormatLinkParametersNew. Uncomment the people.brunel.ac.uk line to reproduce the problem.

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


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

1 Answer 1

4

Not sure I'd use xstring if you already have l3 and pgf loaded but anyway:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .