嵌套宏和超链接问题

嵌套宏和超链接问题

我是 LaTeX 新手,使用该hyperref软件包编写文档。下面是一个简单的场景,我不明白。我必须使用类似\ccc中的宏\hyperlink,但它会导致 6 个错误,我无法修复该问题:

\documentclass[a4paper,12pt,oneside]{book}
\usepackage{hyperref}
\begin{document}
\hypertarget{link:two}{targettwo}

\newcommand{\aaa}{link:one}
\newcommand{\ccc}{\renewcommand{\aaa}{link:two} \aaa}
\hyperlink{\ccc}{something} %no link produced, only text: "link:two" and 6 errors
\end{document}

log:
.
.
.
! Use of \hyper@link@ doesn't match its definition.
\@ifnextchar ... \reserved@d =#1\def \reserved@a {
                                                  #2}\def \reserved@b {#3}\f...
l.21 \hyperlink{\ccc}{aaa}
                           %6 errors
If you say, e.g., `\def\a1{...}', then you must always
put `1' after `\a', since control sequence names are
made up of letters only. The macro here has not been
followed by the required stuff, so I'm ignoring it.

! Argument of \@firstoftwo has an extra }.
<inserted text> 
                \par 
l.21 \hyperlink{\ccc}{aaa}
                           %6 errors
I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

Runaway argument?
! Paragraph ended before \@firstoftwo was complete.
<to be read again> 
                   \par 
l.21 \hyperlink{\ccc}{aaa}
                           %6 errors
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

! Extra }, or forgotten \endgroup.
\hyper@link@ ...\Hy@safe@activestrue \edef \x {#3}
                                                  \ifx \Hy@tempa \@empty \to...
l.21 \hyperlink{\ccc}{aaa}
                           %6 errors
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

Runaway argument?
{\let \reserved@d =*\def 
! Paragraph ended before \hyper@link was complete.
<to be read again> 
                   \par 
l.21 \hyperlink{\ccc}{aaa}
                           %6 errors
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

! Too many }'s.
\x ...r@link {link}{\let \reserved@d =*\def \par }
                                                  {\Hy@safe@activesfalse aaa}
l.21 \hyperlink{\ccc}{aaa}
                           %6 errors
You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.
.
.
.

我正在使用 TeXnicCenter 和 MikTeX 2.9。

答案1

正确的语法是,\hyperlink{<name>}{<text>}其中<name>必须是有效、可扩展的标签名称。它不能包含任何不可扩展的命令。这排除了任何形式的赋值,例如\renewcommand。名称必须直接扩展为某些文本。

所以你的:

\newcommand{\aaa}{link:one}
\newcommand{\ccc}{\renewcommand{\aaa}{link:two} \aaa}
\hyperlink{\ccc}{something} 

是错误的,永远不会起作用。您需要像这样编写它:

\newcommand{\aaa}{link:one}
\newcommand{\ccc}{\renewcommand{\aaa}{link:two}}
\ccc
\hyperlink{\aaa}{something} 

\aaa这意味着:之前进行的任何重新定义\hyperlink并确保\aaa仅扩展为名称。

如果您给出更具体的用途示例,那么我们可以进一步为您提供帮助。

相关内容