4

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}
5

1 Answer 1

9

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}

\begin{equation}\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}}
\end{equation}

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}

enter image description here

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

3
  • 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. Commented Mar 7, 2020 at 10:09
  • 1
    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
    Commented Mar 7, 2020 at 11:19
  • 1
    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
    Commented Mar 7, 2020 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .