# Defining some newcommand with \href, Problems of ##

I want to set a new command which aims at using href (from hyperref package) but by providing some hypertarget as argument of the new command for a specific pdf.

Classical use of hypperref and href is : In first document A, I put some hypertarget :

\hypertarget{fileAhypertarget}{Should come to this}


In the second document B, I make a reference to hypertarget of document FileA.pdf

\href{fileA.pdf#fileAhypertarget}{Let's go to file A!}


Please notice that syntax of href for url in pdf use a # symbol

It works, but now I want to define a command of this type

\newcommand{\myhref}[2]{\href{fileA.pdf##1}{#2}}


But it does not work as # symbol of href command enters in conflict with # symbol of arguments of \newcommand.

Have you any idea, I try different syntax, \protect and thought to \expandafter but I failed.

If you have any idea, Thanks a lot

• you need ## for the # and #1 for the argument, so 3 # Apr 16 '14 at 15:34
• @DavidCarlisle Did you try? Apr 16 '14 at 15:42
• @egreg no I went home:-) (I thought hyperref rescanned it, I guess not:-) Apr 16 '14 at 17:28

You need that LaTeX sees a “stringified” #; here's a way

\documentclass{article}
\usepackage{hyperref}

\begingroup\lccode?=# \lowercase{\endgroup
\newcommand{\myhref}[2]{\href{fileA.pdf?#1}{#2}}
}

\begin{document}
\myhref{fileAhypertarget}{Let's go}
\end{document}


Of course you lose the special treatment \href does to special characters, but it's not a problem if argument #1 has none of them.