1

I would like to use book class and number equation in that way:

The equation have to carry just number of equation without chapter number. I get this using

    \renewcommand{\theequation}{\arabic{equation}}

Then when using \ref command i would like that

  1. if the \ref is invoked in the same chapter of equation the reference is just (equation)
  2. if \ref is invoked in another chapter of the equation the reference to be (equation)\textsubscript{name-of-the-chapter-containing-equation}

Here there is my attempt using zref

\documentclass{book}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{zref-user}
\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@addprop{main}{chapter}
\makeatother
\numberwithin{equation}{section}
\renewcommand{\theequation}{\arabic{equation}}
\newcommand{\myref}[1]{\ifnum\thechapter=\zref[chapter]{#1} (\zref{#1}) \else (\zref{#1})\textsubscript{something} \fi}
\begin{document}
 \chapter{First}
 \ifnum\myref[chapter]{eq:1}=1 a \else b \fi
  \myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
 \section{First First}
 \begin{equation}\zlabel{eq:1}
 E=\gamma m
 \end{equation}
 \section{First Second}
 \begin{equation}\zlabel{eq:2}
 0=0
 \end{equation}
 \chapter{Second}
  \myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
 \begin{equation}\zlabel{eq:3}
 e^{i\pi}+1=0
 \end{equation}
 \chapter{Third}
  \myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
 \begin{equation}\zlabel{eq:4}
 f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
 \end{equation}
 \chapter{Last}
 \myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\end{document}

I have two problems in the definition of \myref

  1. \ifnum does not work properly because maybe \zref[chapter]{#1} does not return a number and so it's impossible to compare with `\thechapter'

  2. I have to find a command to get the chapter name corresponding to a number (or perhaps to use \nameref)

How can I solve them?

3

You can do this as follows. First use the \zrefused and \zref@extract to get the value to compare to trigger the extra formatting.

Now the package zref-titleref gives you the idea of how to get the title field, but it updates not just at chapters but also at sections etc. So instead we write a modified version of the zref-titleref code, that just grabs chapter titles.

Sample output Second sample

\documentclass{book}

\usepackage{amsmath}
\usepackage{zref-user,gettitlestring}
\usepackage{hyperref}

\newcommand{\setcurrentchaptername}[1]{\GetTitleStringNonExpand{#1}\edef\currentchaptername{\detokenize\expandafter{\GetTitleStringResult}}}
\def\currentchaptername{}
\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@addprop{main}{chapter}
\zref@newprop{chaptername}{\currentchaptername}
\zref@addprop{main}{chaptername}
\ZREF@patch{@chapter}{\def\@chapter[#1]{\setcurrentchaptername{#1}%
  \ZREF@org@@chapter[{#1}]}}
\makeatother

\numberwithin{equation}{chapter}
\renewcommand{\theequation}{\arabic{equation}}

\makeatletter
\newcommand{\myref}[1]{\zrefused{#1}%
\ifnum\zref@extract{#1}{chapter}=\thechapter\relax
(\zref{#1}) \else (\zref{#1})\textsubscript{\zref@extract{#1}{chaptername}}\fi}
\makeatother

\begin{document}
\chapter{First}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\section{First First}
\begin{equation}\zlabel{eq:1}
  E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\zlabel{eq:2}
  0=0
\end{equation}
\chapter{Second}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:3}
  e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:4}
  f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\end{document}

As in your code, \zref@newprop is used to set-up new properties to track. These need to have a way to grab the correct data at the time the \zlabel command is called. For the chapter number this is in \thechapter, which you already used. For the chapter title, we store this in \currentchaptername and modify the \chapter command so that this name is set. If you wish to use a different (e.g. shorter) name, then you can redefine \currentchaptername immediately after the \chapter command. Now in the referencing command \myref, we get the label data by first issuing \zrefused and then \zref@extract to get a particular field.

I have changed your relative numbering of equations to be within chapters rather than sections, otherwise you will get duplicate labels. If you really wanting resetting at each section, then you should also be referencing the relevant section title instead, and could probably use zref-titleref directly.

The above code only grabs titles of numbered chapters. You can apply a similar patch to \chapter* via @schapter if you wish, or alternatively set \currentchatpername directly yourself in such circumstances.

  • I discovered a very strange issue: if I use multline instead of equation environment I get some error message. Other env. like eqnarray work perfectly. I cannot understand why. Can you? – MaPo May 18 '17 at 10:41
  • 1
    Yes, zlabel has all sorts of issues in ams environments which play with fundamentals of the label mechanism. In the multline it does not write any information to the .aux file. You could work around by using multlined from mathtools inside an equation, but that is not satisfactory. Perhaps you should ask a new question asking how to get zref to work with the ams environments. – Andrew Swann May 19 '17 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.