Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am new to LaTeX and write a document with the use of the hyperref package. Here is this simple scenario which I do not understand. I have to use a macro like \ccc in \hyperlink but it causes 6 errors and I can not fix the problem:

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

I am Using TeXnicCenter with MikTeX 2.9.

share|improve this question
1  
Welcome to TeX.SX! What's the purpose of this nesting? There may be better ways to do this. –  egreg Apr 11 '12 at 7:46
add comment

1 Answer

up vote 3 down vote accepted

The correct syntax is \hyperlink{<name>}{<text>} where <name> must be a valid, expandable label name. It can not contain any non-expandable commands. This excludes any form of assignment, e.g. \renewcommand. The name must instead expand directly to some text only.

So your:

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

is wrong and will never work. You need to write it some way like this:

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

This means: Do any redefinition of \aaa before \hyperlink and make sure that \aaa expands to only the name.

If you give a more specific example of your exact use, then we can help you further.

share|improve this answer
    
I can't understand why your example (now) and my work. –  Marco Daniel Apr 13 '12 at 13:09
    
@MarcoDaniel: The now gone "example" was the original text posted in the question. I only accidentally pasted it at the end of my answer. ;-) Note that a simple expandable macro works, because it is expanded internally. Either using \csname .. \endcsname or by writing it into an auxiliary file. Both do not support unexpandable stuff. –  Martin Scharrer Apr 13 '12 at 13:10
    
Should I remove my answer? –  Marco Daniel Apr 13 '12 at 13:27
    
@MarcoDaniel: If you think so. Using $ccc doesn't make much sense IMO. –  Martin Scharrer Apr 13 '12 at 13:35
    
@MartinScharrer: Problem is solved I gess, thanks. But in every real case one should be able to get the macro's output in a fully expandable form. Is there any common way to do this?(same question in: tex.stackexchange.com/questions/51770/… ) –  pmks Apr 13 '12 at 14:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.