4

I have in my preamble the following code that creates the boxalign* environment, which is a boxed align* environment.

\makeatletter  % Code for the boxalign* environment
% \newcommand\boxalign*autorefname{Box}
\@fleqntrue{}
\usepackage{empheq}
\newenvironment{boxalign*}{\@fleqntrue\empheq[box=\fbox]{align*}}{\endempheq}
\@fleqnfalse{}
\makeatother

However, I want to refer to this box, but hyperref doesn't recognize the environment and therefore doesn't create the hyperlink. For instance:

\documentclass{article}

\usepackage{hyperref}
\usepackage{mathtools}

\makeatletter  % Code for the boxalign* environment
% \newcommand\boxalign*autorefname{Box}
\@fleqntrue{}
\usepackage{empheq}
\newenvironment{boxalign*}{\@fleqntrue\empheq[box=\fbox]{align*}}{\endempheq}
\@fleqnfalse{}
\makeatother

\begin{document}

\begin{boxalign*}\label{eq:linear_equation}
    y &= ax + b \\
    \frac{y - b}{a} &= x
\end{boxalign*}

This \hyperref[eq:linear_equation]{hyperref} doesn't work.

\end{document}

I saw this post that seemed to have an answer to my problem, which is why I tried adding the \newcommand\boxalign*autorefname{Box} line, but it didn't work (so I commented it out).

How can I do this?

4
  • 1
    The align* environment isn't numbered, I don't know much about the implementation of links within documents or hyperref but I'm pretty sure that something like \refstepcounter needs to be called for hyperref to be able to link to it. Removing the * to get align makes the link work but then you have a numbered environment which I assume you don't want
    – Willoughby
    Commented Apr 10, 2021 at 20:10
  • @Willoughby I thought of this but since you can use \label{} on standard text (so, unnumbered) I thought it wouldn't be a problem. Anyway, it does work if I use align, but as you guessed, I don't want the numbers since I use this boxed environment as an aside in another align environment...
    – Thibaut B.
    Commented Apr 10, 2021 at 20:25
  • When you use \label on 'standard text' it picks up the last time \refstepcounter was called, (its actually a bit smarter than this I think, again I don't know much about the implementation of references). This means that it will pick up the section or subsection number for example. If you try to use \label without first using something like section you get an error
    – Willoughby
    Commented Apr 10, 2021 at 20:30
  • the problem is that the environement suppresses the label completly. So hyperref has not real chance. Commented Apr 10, 2021 at 20:32

2 Answers 2

5

You could cheat the math environment to let the \label command by using another name. You should also set a \phantomsection so that there is a destination for the link:

\documentclass{article}

\usepackage{hyperref}
\usepackage{mathtools}

\makeatletter  % Code for the boxalign* environment
% \newcommand\boxalign*autorefname{Box}
\@fleqntrue{}
\usepackage{empheq}
\newenvironment{boxalign*}{\@fleqntrue\empheq[box=\fbox]{align*}\phantomsection}{\endempheq}
\@fleqnfalse{}
\makeatother

\begin{document}
\let\mylabel\label

\begin{boxalign*}\mylabel{eq:linear_equation}
    y &= ax + b \\
    \frac{y - b}{a} &= x
\end{boxalign*}

This \hyperref[eq:linear_equation]{hyperref} doesn't work.

\end{document}



2
  • When I add the \let\mylabel\label line and change the \label{} for mylabel{} I get errors on the \hyperref line: Argument of \@fifthoffive has an extra }. and Paragraph ended before \@fifthoffive was complete. Do you know what could cause this? I copied and pasted the code from this answer, and checked that all opening braces had matching closing braces.
    – Thibaut B.
    Commented Apr 10, 2021 at 20:51
  • 1
    sorry one shouldn't make last minute changes. I thought it would be cleaner if the \let were in the preamble, but this is naturally to early. Commented Apr 10, 2021 at 21:09
3

An alternative to Ulrike Fischer's answer is passing the label as an optional argument.

\documentclass{article}

\usepackage{mathtools}

\makeatletter  % Code for the boxalign* environment
\@fleqntrue
\usepackage{empheq}
\NewDocumentEnvironment{boxalign*}{o}
 {%
  \@fleqntrue\empheq[box=\fbox]{align*}%
  \IfValueT{#1}{\phantomsection}%
 }
 {%
  \endempheq
  \IfValueT{#1}{\label{#1}}%
 }
\@fleqnfalse
\makeatother
\usepackage{hyperref}

\begin{document}

Some text before the alignment
\begin{boxalign*}[eq:linear_equation]
    y &= ax + b \\
    \frac{y - b}{a} &= x
\end{boxalign*}

\newpage

This \hyperref[eq:linear_equation]{hyperref} works.

\end{document}

You must log in to answer this question.

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