# Using hypertarget caption in hyperlink caption

I want to use hypertarget caption in hyperlink caption automatically.

Example:

\hypertarget{myTarget}{My Target}



Wanted result:

My Target

See My Target.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Apr 1 '14 at 6:34

You can redefine \hypertarget so that it defines a new command corresponding to its first argument:

\let\oldhypertarget\hypertarget
\renewcommand{\hypertarget}[2]{%
\oldhypertarget{#1}{#2}%
\protected@write\@mainaux{}{%
\string\expandafter\string\gdef
\string\csname\string\detokenize{#1}\string\endcsname{#2}%
}%
}


In this way, when you issue

\hypertarget{myTarget}{My Target}


this also saves a command named \myTarget in the .aux file.

Then we define a new command \myhyperlink that takes care of that:

\newcommand{\myhyperlink}[1]{%
}


When you issue

\myhyperlink{myTarget}


this results in

\hyperlink{myTarget}{\myTarget}


MWE:

\documentclass{article}

\makeatletter
\let\oldhypertarget\hypertarget
\renewcommand{\hypertarget}[2]{%
\oldhypertarget{#1}{#2}%
\protected@write\@mainaux{}{%
\string\expandafter\string\gdef
\string\csname\string\detokenize{#1}\string\endcsname{#2}%
}%
}
}
\makeatother

\begin{document}

\hypertarget{myTarget}{My Target}

\end{document}


Output:

If you want to customize it, you can change it, for example, to (requires xcolor package):

\newcommand{\myhyperlink}[1]{%
`