6

I have a bunch of automatically generated LaTeX code with hypertargets of the form "functionname_2093840289fad1337", i.e the name of a function with a hash appended. I would like to refer to those functions from the rest of the document by only referring to the function name which I know is unique. I would like a lookup function something like this:

\hyperdyperlink{functionname}

that emits

\hyperlink{functionname_2093840289fad1337}{functionname}

Note that I can't calculate the hash but I'm prepared to write a table that maps each functionname to functionname+hash. What's the best way to write this kind of function?

starblue
  • 55,348
  • 14
  • 97
  • 151
Laserallan
  • 11,072
  • 10
  • 46
  • 67

2 Answers2

7

Does this work?

\makeatletter
\newcommand\hashlink[2]{%
    \@namedef{hashlink-#1}{#2}%
}
\newcommand\hyperdyperlink[1]{%
    \hyperlink
    {#1_\@nameuse{hashlink-#1}}
    {#1}%
}
\hashlink{functionname}{2093840289fad1337}
\hyperdyperlink{functionname}
\makeatother

(Untested.)


Later: To branch the code depending if you've defined the link target, you can write something like

\newcommand\hyperdyperlink[1]{%
    \@ifundefined{hashlink-#1}{%
    [whatever else you want to do]
    }{%
    \hyperlink{#1_\@nameuse{hashlink-#1}}{#1}%
    }%
}

(Update: oops; that was pretty broken as first posted, sorry. Now fixed, I hope.)

Eric
  • 95,302
  • 53
  • 242
  • 374
Will Robertson
  • 62,540
  • 32
  • 99
  • 117
  • Only problem I've found is that I get a really hairy error if I haven't defined the link target yet. Is it possible to see if a name exist and do something different (like linking to {} or similar?) – Laserallan Jun 08 '09 at 13:49
5

Since the function names are unique, could you not define the hyperlink targets without the hash appended?

Alternatively, you could create a new LaTeX macro for each function. The code that generates the LaTeX code could do this by outputting code like this:

\newcommand{\linkFoo}{\hyperlink{foo_2093840289fad1337}{foo}}
\newcommand{\linkBar}{\hyperlink{bar_4323812312asf1342}{bar}}

Then use \linkFoo and friends in your hand-written part.

You could also implement a proper lookup table with TeX macros if you really wanted -- see this thread for an example -- but this solution is quite easy and simpler to understand (IMHO).

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • Thanks for the comment. The hashes represents the parameter lists and return types but we work with C symbols only which doesn't support overloading of function names. – Laserallan Jun 08 '09 at 09:36
  • @Will: ah, I guess you mean to point out that \ifcase is only for integer cases... that makes sense now that I think of it with the \today macro :-) – Martin Geisler Jun 08 '09 at 13:28
  • I've removed the \ifcase example since it didn't make much sense. – Martin Geisler Jun 08 '09 at 13:29
  • You also wouldn't have wanted to count from zero all the way up to 2093840289fad1337 number of \or's :) – Will Robertson Jun 08 '09 at 15:31