I have this command that works fine:
\newcommand{\mytag}[2]{%
\text{#1}%
\@bsphack
\protected@write\@auxout{}%
{\string\newlabel{#2}{{#1}{\thepage}}}%
\@esphack
}
but when I add the package hyperref as:
\usepackage{hyperref}
\hypersetup{
colorlinks=true, %set true if you want colored links
linktoc=page, %set to all if you want both sections and subsections linked
linkcolor=blue, %choose some color if you want links to stand out
}
Then latex prompts me this error when I'm using my newcommand and does not compile. The error is:
Paragraph ended before \Hy@setref@link was complete.
I tried to remove all the .aux files but the error does not dissapear. It only dissapears when I comment the hyperref lines. How can I solve this problem?
EDIT:
\documentclass{article}
\usepackage [english] {babel}
\usepackage {anysize}
\usepackage [T1]{fontenc}
\usepackage {amsmath, amsthm, amsfonts, amssymb}
\usepackage [utf8]{inputenc}
\usepackage{mathtools}
\usepackage{lipsum}
\usepackage{hyperref} %If you comment this, it works. If you uncomment it doesn't
\makeatletter
%my command
\newcommand{\mytag}[2]{%
\text{#1}%
\@bsphack
\protected@write\@auxout{}%
{\string\newlabel{#2}{{#1}{\thepage}}}%
\@esphack
}
%frougon command. It does not work as expected. It loses the posibility to refer to portions of the equations.
%\newcommand*{\mytag}[2]{\text{#1}\def\@currentlabel{#1}\label{#2}}
\makeatother
\begin{document}
\begin{equation}\label{eq:myeq}
E=\underbrace{\underbrace{mv^2}_{\mytag{kinetic}{eq:myeq_kinetic}}+\underbrace{mgh}_{\mytag{potential}{eq:myeq_potential}}}_{\mytag{total Energy}{eq:myeq_totalE}},
\end{equation}
Eq. \ref{eq:myeq} is the expresion for \ref{eq:myeq_totalE}, which is composed by \ref{eq:myeq_kinetic} and \ref{eq:myeq_potential} energies.
\end{document}
\newlabel
needs an extended syntax:\newlabel{a}{{1}{1}{ab}{section.1}{}}
\@bsphack\@esphack
can not do anything following\text{}
hyperref
last, see Which packages should be loaded after hyperref instead of before?.\label
/\ref
.