TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to create a command with the following features:


In the doc body:

\mycom{firstitem}{This is the first item}

and later:


Which would be equivalent to:

\hyperref[firstitem]{This is the first item}

and link to the position where I wrote \mycom.


I played around with phantomsec, but it seems to miss a naming feature. Thanks for any hint.

share|improve this question

migrated from stackoverflow.com Apr 4 '11 at 19:53

This question came from our site for professional and enthusiast programmers.

This question is a good candidate for tex.stackexchange.com – Yasir Arsanukaev Mar 20 '11 at 12:34
This is clearly a question about programming in latex, which makes it on-topic either here or on tex.se. You might be better choosing the specialty site in the future simply because you get a specialized audience, but the question is OK as it is. – dmckee Mar 20 '11 at 15:05
shall I repeat the question there or is there a way to move questions around sites? – Fuligginoso Mar 20 '11 at 19:17
Why don't you use \hyperlink and \hypertarget? – Gonzalo Medina Apr 4 '11 at 21:29
Thanks Gonzalo, I was missing these two commands, which bring the question a step further. There is still the problem: if I define \hyperlink{label}{my nice caption} I'd like to be able to write something like: \hypertarget{label} which should then be rendered as a clickable "my nice caption" this is not possible directly, as the caption field for \hyperlink is mandatory – Fuligginoso Apr 5 '11 at 11:44

The commands could be defined like this:

\@ifundefined{mycom@#1}{\textbf{??}\@latex@warning{Reference `#1' on page \thepage \space undefined}}%

\mycom{...}{...} creates a target named like the first argument with the text of the second argument and saves the caption in the macro \mycom@<first argument>.

\linktomycom{...} checks if \mycom@<first argument> is defined - if it isn't, two question marks and a LaTeX warning are produced, otherwise, a link to the specified target with the previously saved text is generated.

(The definitions are enclosed in \makeatletter ... \makeatother because internal LaTeX macros with an @ in their name are used.)


I couldn't find out if you want the \mycom command to produce a visible caption or just an (invisible) anchor - if you want the latter, you must define a target with an empty text:

share|improve this answer

Your Answer


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.