3

I use zref, but the numbers are not correctly displayed for align environments (they are for equation, though).

example:

\documentclass{report}
\usepackage{amsmath}
\usepackage{zref}
\usepackage{zref-user}

\begin{document}
    \chapter{a}
    \begin{align}
        a \zlabel{eq:a} \\
        b \zlabel{eq:b}
    \end{align}
    test: \zref{eq:a}, \zref{eq:a}
    \begin{equation}
        c \zlabel{eq:c}
    \end{equation}
    test2: \zref{eq:c}
\end{document}

enter image description here

I need the zref package since I want to reference other subfiles.

Also: figure references don't seem to work at all.

What am I doing wrong?

2
  • Welcome to TeX.SX! \numberwithin has nothing to do with the issue, so I removed it from your question. The output is exactly the same.
    – egreg
    Nov 20, 2017 at 18:15
  • The issue isn't limited to instances of align environments -- it crops us with gather environments as well.
    – Mico
    Nov 20, 2017 at 19:30

2 Answers 2

2

First question: Environments of package amsmath

The environments of amsmath process the contents twice. The first run measures the width and the second run sets the equations. Also, they support \notag and friends. The is that the support of \label is quite hard-wired into the environments. Other label as \zlabel are not supported.

A (crude) workaround:

  1. Use of \label inside the environments of package amsmath.
  2. Import of the \labels as \zlabels via zref-xr.
  3. Then the imported labels can be accessed by \zref with or without prefix, specified in the second step.

Example with prefix:

\documentclass{report}
\usepackage{amsmath}
\usepackage{zref}
\usepackage{zref-user}
\usepackage{zref-xr}

\zexternaldocument*[self:]{\jobname}

\begin{document}
    \chapter{a}
    \begin{align}
        a \label{eq:a} \\
        b \label{eq:b}
    \end{align}
    test: \zref{self:eq:a}, \zref{self:eq:b}
    \begin{equation}
        c \zlabel{eq:c}
    \end{equation}
    test2: \zref{eq:c}
\end{document}

Result

Second question: Figure references

References for \captions in figures or tables work as expected:

\documentclass{report}
\usepackage{zref}
\usepackage{zref-user}

\begin{document}
\chapter{a}
\begin{figure}[h]% "h" as exception to get a smaller image for TeX.SX.
  \caption{Figure caption}
  \zlabel{fig:caption}
\end{figure}
Test: \zref{fig:caption}
\end{document}

Result

1

Here is an alternative method for setting \zlabels in align, and any other math environment of amsmath. The idea is to let amsmath store \label as it usually does, but make it set both a regular \label and a \zlabel when actually setting the label. This can be achieved by redefining \ltx@label which is the macro responsible for the actual label setting in amsmath. The only care to take here is to use the internal \zref@label for the task, since \zlabel renders itself no-op when \label is set to \@gobble, which is precisely the case in the multline environment. Finally, if you happen to be using hyperref you should ensure your redefinition will not be overwritten by its call of nameref. With that in hand, you use the regular \label in the environment, but both labels get defined (with the same name, with is not a problem, since the label systems are independent).

\documentclass{book}
\usepackage{amsmath}
\usepackage[user]{zref}
\usepackage{hyperref}

\makeatletter
\AddToHook { begindocument }
  {
    \@ifdefinable\myold@ltx@label{\let\myold@ltx@label\ltx@label}
    \newcommand*{\mybothlabels}[1]{%
      \myold@ltx@label{#1}%
      \zref@wrapper@babel\zref@label{#1}%
    }
    \renewcommand*{\ltx@label}[1]{\mybothlabels{#1}}
  }
\makeatother

\begin{document}
\chapter{Chapter}
\begin{align}
  a \label{eq:a} \\
  b \label{eq:b}
\end{align}
test: \zref{eq:a}, \ref{eq:a}, \zref{eq:b}, \ref{eq:b}
\begin{equation}
  c \label{eq:c}
\end{equation}
test2: \zref{eq:c}, \ref{eq:c}
\end{document}

enter image description here

3
  • @UlrichDiez Thanks for the suggestion. I agree that usually that is the correct procedure. I just happen to have checked exactly what \ltx@label looks like, and who defines it. So I did some of the work \renewcommand would do for me. Besides nameref does exactly \let\ltx@label\label, so I'm following.
    – gusbrs
    Oct 13, 2021 at 16:47
  • Well, you should tell that to the author of nameref ;-) Furthermore, it knows it is redefining an existing macro defined by someone else, since it is an internal macro to amsmath. It is just providing support for amsmath and hyperref to get along.
    – gusbrs
    Oct 13, 2021 at 16:53
  • @UlrichDiez Anyway, I guess you are right, no harm in spreading good practices. Specially considering the diversity of the public here at TeX.SX. I edited for more "civil" procedures. Thanks for suggesting.
    – gusbrs
    Oct 13, 2021 at 17:25

You must log in to answer this question.

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