1

I'm having trouble using newcommand with the \href

I would like a newcommand like this:

\newcommand\link[2]{\href{#1}{\underline{#2}}} 

This command works fine quite all, for exemple:

\link{htt://www.impa.br}{IMPA} 

But it does not work when the URL name has code like #1. For example, it does not work in this exemple:

\link{http://matematica.obmep.org.br/index.php/modulo/index#1}{modules}

It does not work because the #1 in the URL name is used with the #1 in the newcommand.

How could I solve this problem?

3
  • Thank you for your answer. I believe this is not a duplicate topic. But may be I did not understand your suggestion. In my case, there are websides that have in your URL codes as # 1, for example: [matematica.obmep.org.br/index.php/modulo/index#1] And there are websites with simpler URL, for example: [impa.br] In all of my cases, I wish that the URL is send to the newcommand as a string. Oct 24, 2014 at 16:19
  • You could use \edef\temp{#1}\href{\temp} Oct 24, 2014 at 17:46
  • Excuse me. Unfortunately I did not understand your suggestion. Could you explain how it would look all the code so that I can pass any URL address for the newcommand? Thank you. Oct 25, 2014 at 12:52

1 Answer 1

4

Really this is a duplicate of Problem with use of '#' in custom \href command. But just to make it clear how to use this in your case here is a complete example:

\documentclass{article}

\usepackage{hyperref}

\makeatletter
\newcommand*{\link}{\begingroup\@makeother\#\@mylink}
\newcommand*{\@mylink}[2]{\href{#1}{\underline{#2}}\endgroup} 
\makeatother

\begin{document}

\link{htt://www.impa.br}{IMPA} 

\link{http://matematica.obmep.org.br/index.php/modulo/index#1}{modules}

\end{document}

As in the other question, one defines a helper command which is called with # given an appropriate category code so it is no longer interpreted as a command argument.

0

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