宏扩展和带超链接的 href

宏扩展和带超链接的 href

我希望我的问题非常简单!我正在写一本关于 MPASM (PIC) 汇编器的书,其中会有很多类似的程序、十六进制文件、视频等链接。这些将包含在标准的 中tabular,将多次使用。tabular当我直接在 中输入链接时, 工作正常tabular,但在宏中,唯一有效的是 \href{{#1}}。我认为这是完全展开的,因为 URL 直接传递给宏。其他的例如\href{http://stbarnabaschapel.net/hex/PROJ{#2}.hex}不起作用,因为#2会像0x00我认为的那样没有正确展开。我希望有一个简单的方法可以解决这个问题。

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

答案1

#1简而言之:省略, #2, ...周围的花括号。

例子:

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

如果#2foobar,则展开为:

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

花括号是不是消失,因为它们不会被解析为参数。因此花括号成为 URL 的一部分。

同样也是\href{{#1}}错误的。实现可能会删除另一组括号,但这是偶然的,不能依赖。

PS: 后面需要添加注释符:

\newcommand{\linktable}[3]{

删除行尾:

\newcommand{\linktable}[3]{%

否则可能会出现不必要的行距:

Some text of the previous paragraph.
\linktable...

空行\linktable结束上一段。TeX 删除 段落末尾的空格(使用\unskip)。通常,该空格位于段落最后一行的行尾。但如果\linktable插入另一个空格,则删除此空格并保留前一个空格。根据换行情况,它可能会导致出现一个额外的空行,并发出未满\hbox警告。

相关内容