2

I need to remove any type of formatting from a text to get an URL multi-formatting.
By keeping the following model, I have tried various solutions but without having the desired result.
Would anyone know how to find a solution?

\newcommand{\link}[1]{
    %
    % ?
    %
    \href{#1_WITHOUT_FORMATTING_AND_COMMANDS}{#1_WITH_FORMATTING}
}

\begin{document}
    \link{http://www.site.com/page.php?key1=\textcolor{red}{value1}\&\textit{key2=value2}/\textbf{page2}/page3}
\end{document}

Thanks.

BnG
  • 49
  • You should define a fixed set of formatting commands for showing a URL, in order to get better consistency across your document. Then you can apply David's suggestion with the set of formatting commands. – egreg Dec 31 '18 at 15:04

1 Answers1

5

enter image description here

\documentclass{article}

\usepackage{hyperref,color}


\newcommand{\link}[1]{%
    {%
    \def\textcolor##1##2{##2}%
    \def\textbf##1{##1}%
    \def\textit##1{##1}%
    \edef\&{\string&}%
    \xdef\tmp{\noexpand\href{#1}}}%
 \tmp{#1}%
}
\begin{document}
    \link{http://www.site.com/page.php?key1=\textcolor{red}{value1}\&\textit{key2=value2}/\textbf{page2}/page3}
\end{document}
David Carlisle
  • 757,742
  • replacing \link by \url in your code (hence \link definition can also be removed) gives very strange error with \module undefined in file supp-pdf.mkii –  Dec 31 '18 at 14:50
  • any idea about how undefined \module pops out from using \url (I did not trace it) –  Dec 31 '18 at 14:52
  • @jfbu the supp-pdf file gets read with % not being a comment – David Carlisle Dec 31 '18 at 14:57
  • 2
    I'd prefer the first brace being \begingroup and finishing with \edef\tmp{\endgroup\noexpand\href{#1}}\tmp{#1} – egreg Dec 31 '18 at 15:06
  • Yes! It works great. It doesn't split only long url in tabular environment, like \url does. Thanks David. – BnG Jan 03 '19 at 11:37
  • @egreg it's only typeset no assignments so no real need to lift the local definitions out of the group is there? – David Carlisle Jan 03 '19 at 11:43
  • @DavidCarlisle The proposed code doesn't leave \tmp defined. – egreg Jan 03 '19 at 12:08
  • @egreg anyone who uses \tmp assuming a particular definition deserves what they get:-) in package code I'd use \@tempa or \TX@tempa rather than \tmp of course. – David Carlisle Jan 03 '19 at 13:39
  • Can someone help me change the definition of link to automatically wrap the output? – BnG Jan 03 '19 at 13:56