# Macro expansion and href with hyperlink

I have very simple question I hope! I am writing a book on MPASM (PIC) assembler and there will be very many similar links to programs, hex files, videos etc. These will be enclosed in a standard tabular, which will be used many times. The tabular works fine when I type in the links directly within the tabular, but inside a macro the only one which works is the \href{{#1}}. This I think is fully expanded as the URL is passed to the macro directly. The others such as \href{http://stbarnabaschapel.net/hex/PROJ{#2}.hex} do not work as the #2 which will be something like 0x00 I think does not get expanded properly. I am hoping there is a simple way around this.

\newcommand{\linktable}[3]{

\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
\cellcolor{red}{\href{{#1}}{\color{white}Video {\large \eye}}}&\cellcolor{orange}
\href{http://stbarnabaschapel.net/hex/PROJ{#2}.hex}{Hex File {\Hexasteel}}&
\cellcolor{yellow} \href{http://stbarnabaschapel.net/asm/proj{#2}.asm}
{Listing {\rightpointleft}} &\cellcolor{green}\href{http://stbarnabaschapel.net
/package/PROJ{#2}.zip}{Project  {\satellitedish}}&\cellcolor{blue}
\href{http://stbarnabaschapel.net/dfiles/d16F1847_{#3}.inc}{{\color{white}'d' file {\smallpencil}}}\\
\hline
\end{tabular}
\end{center}


In short: Omit the curly braces around #1, #2, ...

Example:

\href{http://stbarnabaschapel.net/asm/proj{#2}.asm}


If #2 is foobar, then it expands to:

\href{http://stbarnabaschapel.net/asm/proj{foobar}.asm}


The curly braces are not going away, because they are not parsed as argument. Thus the curly braces become part of the URL.

Also \href{{#1}} is wrong in the same way. It is possible, that the implementation strips another set of braces, but this is accidental and it cannot be relied on.

PS: A comment character is needed after:

\newcommand{\linktable}[3]{


to remove the line end:

\newcommand{\linktable}[3]{%


Otherwise there can be an unwanted line space:

Some text of the previous paragraph.


The empty line in \linktable ends the previous paragraph. TeX removes one white space at the end of a paragraph (using \unskip). Usually it would be the line end of the last line of the paragraph. But if \linktable inserts another space, then this space is removed and the previous is kept. Depending on the line breaking it can result in an extra empty line with an underfull \hbox warning.

• Yes that does work. Had the \href{{#1}} also not worked it would have set me on the right track. The question is now answered Dec 4, 2014 at 10:31