Is my new command incompatible with hyperref?

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{
linktoc=page,     %set to all if you want both sections and subsections linked
}


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}
$$\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}},$$

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}

• you should show a complete example that can be tested, not only some snippets. With hyperref \newlabel needs an extended syntax: \newlabel{a}{{1}{1}{ab}{section.1}{}} – Ulrike Fischer Mar 7 '20 at 0:15
• unrelated but \@bsphack\@esphack can not do anything following \text{} – David Carlisle Mar 7 '20 at 0:33
• Hi @Learning from master and welcome to TeX-SE. As said before, please provide a MWE. As thumb rule, load hyperref last, see Which packages should be loaded after hyperref instead of before?. – FHZ Mar 7 '20 at 0:37
• I explained the concepts related to cross-referencing in LaTeX 2e with and without hyperref in my answer to the question "How to prevent reference to enumeration inside new environment?". There you can read about the differences when using/not using hyperref. Beside this: If you wish to make up your own cross-referencing-mechanism independent from the LaTeX 2e-kernel and hyperref, Heiko Oberdiek's zref-package might be of interest to you. By the way: amsmath-things tend to override kernel-things like \label/\ref. – Ulrich Diez Mar 7 '20 at 14:26
• BTW, you meed \makeatletter and \makeatother around any code using @ symbols in macro names. – John Kormylo Mar 7 '20 at 22:12

hyperref redefines a few LaTeX commands, including \label. Indeed, it needs to store more data associated to each label in the .aux file, in order to provide the features it offers.

Your command is essentially \text{#1} followed by the definition of the \label command from the LaTeX kernel. Thus, it writes data to the .aux file in a format that is incompatible with hyperref. In order to avoid such compatibility problems, the easiest way is to simply use \label to do the delicate work (the one that has to be different according to whether hyperref is loaded or not). Since the definition of \label in the LaTeX kernel is:

\def\label#1{\@bsphack
\protected@write\@auxout{}%
{\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
\@esphack}


and since amsmath also renames \label to \ltx@label, your command can be translated as follows:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}

\makeatletter
\newcommand*{\mytag}[2]{\text{#1}\def\@currentlabel{#1}\ltx@label{#2}}
\makeatother

\begin{document}

$$\label{eq:myeq} E = \underbrace{ \underbrace{\frac{1}{2} mv^2}_{\mytag{kinetic}{eq:myeq_kinetic}} + \underbrace{mgh}_{\mytag{potential}{eq:myeq_potential}} }_{\mytag{mechanical energy}{eq:myeq_mechanicalE}}$$

Eq.~\ref{eq:myeq} is the expression for~\ref{eq:myeq_mechanicalE}, which is
composed of~\ref{eq:myeq_kinetic} and~\ref{eq:myeq_potential} energies.

\end{document}


Note: as @DavidCarlisle pointed out, your \@bsphack ... \@esphack pair was useless in math mode.

• It doesn't work at all. I get this error: "Package amsmath Error: Multiple \label's: label 'eq:myeq' will be lost. ...{eq:myeq1}}+piece of equation}". The idea is to have different brackets that take some part of the equation, and print there a text and be able to refer to this portion with a label. The text is printed with yours, but then I lose the label. I'll put some of my code in my first message now. – Learning from masters Mar 7 '20 at 10:09
• My code showed that it worked within $...$. Part of the problem is that, as Ulrike pointed out, you (at first) didn't provide a minimal working example—not even any example. Therefore, you didn't help me test the new command in all relevant conditions. Not only does hyperref redefine \label, amsmath also does so; I know a few things, but not everything! I have updated the answer. It works both within $...$ and in the equation environment. – frougon Mar 7 '20 at 11:19
• Note: the example you added is not minimal; please don't include irrelevant packages, it only obscures the question and makes it harder to find via search engines. Also, I modified your formula because it would be incorrect, AFAICT, without the 1/2 factor. – frougon Mar 7 '20 at 11:19