5
\documentclass[oneside,10pt]{book}
\usepackage{amsmath, amsfonts, amsthm, amssymb}
\usepackage{hyperref}
\usepackage{blindtext}

\begin{document}
\chapter{something}
    \blindtext[1]

\chapter{something else}
\section{a section}
    This is the most important equation in this chapter
    \begin{equation}\label{important-equation}
        1=1
    \end{equation}

\chapter{chapter 3}
    As we have seen in Equation \ref{important-equation} in Chapter (REF-chapter-of-equation) on page (REF-page-of-equation), we have something important
\end{document}

What I would like to achieve is to be able to automatically substitute (and link) the correct chapter and the correct page in the output instead of (REF-chapter-of-equation) and (REF-page-of-equation).

I would obviously like to do it also with other environments (figures, theorems, definitions), not just equations.

And last obvious thing, I would like to avoid to "manually link the chapter", because if I add a chapter at the beginning, or worse if I move the equation to another chapter, I then would have to re-enumerate everything and this looks like a vary bad move.

I already have a look at hyperref manual, but with no too much success.

I don't know if this is relevant, I use pdfLaTeX on TeXLive 2015 on Ubuntu machines. The solution has to work with amsmath.

Any help on how to achieve this, will be greatly appreciated :)

Werner
  • 603,163
dadexix86
  • 651
  • 1
    Have you tried with varioref ? – Jérôme Dequeker Jan 18 '16 at 16:35
  • Nice! I didn't know. This seems to work for the page, so it answers half of my question. Do you know anything about the Chapter? – dadexix86 Jan 18 '16 at 16:41
  • It is possible with nameref. See : http://tex.stackexchange.com/questions/6238/get-the-title-instead-of-the-number-of-a-referenced-chapter-section – Jérôme Dequeker Jan 18 '16 at 16:51
  • Mh.. no, that is definitely not what I was looking for :) Using the counter is ok with me, but somehow what I would like to do is to "recover" it from the equation reference (definition, theorem or whatever), so that if I move the equation to another chapter the reference gets updated too (as it happens with equation's number, page and such) – dadexix86 Jan 18 '16 at 16:52
  • If you want to be aware to transform your chapter into a section, you could also use cleveref. See : http://tex.stackexchange.com/questions/109843/cleveref-and-named-theorems – Jérôme Dequeker Jan 18 '16 at 16:55
  • This is nice, but it is not what I am asking :) – dadexix86 Jan 18 '16 at 17:26
  • With the nameref package you are able to have the chapter name, right ? and with the varioref package, you have the chapter first page ?

    So I don't understand where the problem is.

    – Jérôme Dequeker Jan 18 '16 at 17:35
  • The problem is that I never said that I want the chapter name... Quite the opposite in fact: "Using the counter is ok with me" (two comments ago). Probably I wasn't clear: What I want is to be able to use the counter of the Chapter an equation belongs to in a reference (or a theorem, definition, ...). So that if I move the equation to another Chapter, the ref gets updated too. I hope that now is more clear. – dadexix86 Jan 18 '16 at 17:44
  • 1
    for the page you need \pageref{important-equation}. for chapter, I will see. – touhami Jan 18 '16 at 17:47

1 Answers1

7

One option is to update \chapter and insert a regular \label. The structure should be something that you're not going to use elsewhere in the document, of course, since \labels have to be unique. I've chosen to insert \label{chapter-\thechapter}, which is similar to \label{chapter-2} in Chapter 2 if \thechapter is \arabic{chapter} (the defaut).

To insert this label, we redefine \chapter and use some refcount to extract it from the default equation number - (\thechapter.\arabic{equation}) within \chapref:

enter image description here

\documentclass[oneside]{book}
\usepackage{amsmath,hyperref,refcount}
\usepackage{blindtext}

\AtBeginDocument{
  \let\oldchapter\chapter
  \renewcommand{\chapter}[1]{%
    \clearpage
    \oldchapter{#1}% Regular chapter
    \label{chapter-\thechapter}% \label this chapter
  }
}

\makeatletter
\def\extract@chapter#1.#2\@nil{#1}%
\newcommand{\chapref}[1]{{%
  \edef\x{\noexpand\edef\noexpand\chapnum{\noexpand\extract@chapter\getrefnumber{#1}\noexpand\@nil}}\x%
  \ref{chapter-\chapnum}%
}}
\makeatother

\begin{document}

\chapter{something}
\blindtext[1]

\chapter{something else}
\section{a section}
This is the most important equation in this chapter
\begin{equation}
  1=1 \label{eq:important-equation}
\end{equation}

\chapter{chapter 3}
As we have seen in Equation~\eqref{eq:important-equation} in Chapter~\chapref{eq:important-equation} on page~\pageref{eq:important-equation}, we have something important
\end{document}

The above solutions assumes you'll only use numbered chapters. However, if you're also using unnumbered chapters, then you can use the following redefinition of \chapter:

\usepackage{xparse}
\AtBeginDocument{
  \let\oldchapter\chapter
  \RenewDocumentCommand{\chapter}{s o m}{%
    \clearpage
    \IfBooleanTF{#1}
      {\oldchapter*{#3}}% \chapter*[..]{...}
      {\IfValueTF{#2}
         {\oldchapter[#2]{#3}}% \chapter[..]{...}
         {\oldchapter{#3}}% \chapter{...}
       \label{chapter-\thechapter}% \label this chapter
      }%
  }
}

The only difference is the conditioning on whether or not you use \chapter or \chapter*, together with the conditional placement of \label.


Another solution, not compatible with amsmath: zref is an easy option for this as it allows the user to specify more than the single \ref value via "properties".

Below I've created a new property chapter, which stored \thechapter. Additionally, each \chapter command has been redefined to insert a \label to allow for an appropriate hyperlink. Finally, \label is also updated to store the current chapter its used within.

If you just want the chapter number, and not a hyperlink to the start of the chapter, then there is no need to redefine \chapter (...life would be easier).

enter image description here

\documentclass[oneside]{book}
\usepackage{zref,hyperref}
\usepackage{blindtext}

\makeatletter
\zref@newprop{chapter}{\thechapter}% Chapter property holds \thechapter

\AtBeginDocument{%
  \let\oldlabel\label
  \renewcommand{\label}[1]{%
    \zref@labelbyprops{#1}{chapter}% Special label
    \oldlabel{#1}% Old label
  }%
}

\let\oldchapter\chapter
\renewcommand{\chapter}[1]{%
  \clearpage
  \oldchapter{#1}% Regular chapter
  \oldlabel{chapter-\thechapter}% \label this chapter
}
\newcommand{\chapref}[1]{%
  \begingroup\edef\x{\endgroup\noexpand\hyperref[chapter-\zref@extract{#1}{chapter}]{%
    \zref@extract{#1}{chapter}}}\x%
}

\makeatother

\begin{document}

\chapter{something}
\blindtext[1]

\chapter{something else}
\section{a section}
This is the most important equation in this chapter
\begin{equation}
  1=1 \label{eq:important-equation}
\end{equation}

\chapter{chapter 3}
As we have seen in Equation~\ref{eq:important-equation} in Chapter~\chapref{eq:important-equation} on page~\pageref{eq:important-equation}, we have something important
\end{document}

Some related references:

Werner
  • 603,163