38

In LaTeX, we can say something like:

"See equation \ref{eq} on page \pageref{eq}..."

Is there any way to do:

"See equation \ref{eq} in chapter \chapterref{eq}..."

migrated from stackoverflow.com Mar 16 '11 at 13:37

This question came from our site for professional and enthusiast programmers.

  • This is more suitable on latex.SE – Zsub Mar 16 '11 at 13:31
  • @Zsub Don’t comment, flag for moderators. (But I’ve already done that) – Konrad Rudolph Mar 16 '11 at 13:32
  • So had I, of course :) – Zsub Mar 16 '11 at 13:39
  • 3
    @Zsub: Doing both often leads to the question be migrated and reposted here on tex.sx, so we get exactly exact duplicates. So please don't comment! – Hendrik Vogt Mar 16 '11 at 14:23
  • 4
    @brian: What should \chapterref return? The number or the name of the chapter? – Martin Scharrer Mar 16 '11 at 14:34
33

hyperref offers \autoref which will turn \autoref{chap:foo} into "chapter X"

\documentclass{book}
\usepackage{hyperref}
\begin{document}
\chapter{Foo}
\label{chap:foo}

This is the beginning of \autoref{chap:foo}
\end{document}

Produces:

chapter foo

To edit how hyperref references appear, see answers to this question

cleveref is another package that offers this sort of functionality.

The LaTeX wikibook has details on various cross-referencing options.

  • 2
    This does not seem to address the original question, which asks about extracting the chapter reference from the item (eg, equation) within the chapter. – Lev Bishop Mar 17 '11 at 3:30
  • 1
    @Lev I don't think it's obvious what the question is asking for. Certainly not to the extent that this answer ought to be downvoted for being off topic. – Seamus Mar 18 '11 at 9:50
  • @Seamus Well you won't for sure, I hope, subdue what is true to what majority think is true. The question asks explicitely for a way to quote a chapter number through a reference label declared in the chapter. Your answer provides a way to automatically get chapter number through the chapter label. You do agree I hope that the two statements are completely different. Thus, you don't answer the question at all. But I do agree that you can not care as you get upvotes. – ujsgeyrr1f0d0d0r0h1h0j0j_juj Oct 3 at 19:30
12

You can do this with the zref package. Here is an example use:

\documentclass{book}
\usepackage{zref-user}
\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@newprop{chaptertype}{\@chapapp}% as suggested by Danie Els
\zref@addprop{main}{chapter,chaptertype}
\makeatother
\begin{document}
\chapter{ch1}
  ch1
\chapter{ch2}
  ch2
  \begin{equation}\zlabel{x1}
     x=1
  \end{equation}
\chapter{ch3}
  See equation~\zref{x1} in chapter~\zref[chapter]{x1}.
  Or, Danie's suggestion (in case you move equations between
  chapters and appendices): 
  See equation~\zref{x1} in \zref[chaptertype]{x1}~\zref[chapter]{x1}
\end{document}
  • The hardcoded chapter~.... is a problem if the equation is inside an appendix. Maybe a \zref@newprop{chaptertype}{\@chapapp} for the book classes. Note that AMSbook uses \chaptername – Danie Els Mar 17 '11 at 7:09
  • @Danie Well, that wasn't part of the original question, but I edited my answer as you suggested to automatically show the chapter/appendix type. – Lev Bishop Mar 18 '11 at 0:35
  • Please stop the sarcasm. I had tried to help by suggesting points that will make the overall solution better, even if the OP didn’t asked for it. – Danie Els Mar 18 '11 at 4:48
  • 1
    @Danie, Sorry, I didn't intend any sarcasm, but I can see how you saw it that way, so I have edited again to make it clear (hopefully) that I was giving proper credit for your suggestion, and to separate the part that is necessary for the original question versus the improved version that deals with an issue that maybe the OP didn't even consider. – Lev Bishop Mar 18 '11 at 12:14
  • It works fine just that I needed to replace \zref@addprop{main}{chapter,chaptertype} by \zref@addprop{main}{chapter} \zref@addprop{main}{chaptertype}. – Marcel Aug 12 '12 at 16:49
3

As meep.meep already posted the nameref can be used to reference to the name of the chapter. However, to get it right you need to label and reference the chapter manually and not use the label of the equation.

\documentclass{report}
\usepackage{nameref}
\usepackage[english]{babel} 
\begin{document}

\chapter{Introduction}\label{chap:intro}

\begin{equation}
  E = m\cdot c^2 \label{eq:emcsq}
\end{equation}

\chapter{Other}\label{chap:other}

% By name: "See equation 1.1 in chapter Introduction."
See equation~\ref{eq:emcsq} in chapter~\nameref{chap:intro}.


% By number: "See equation 1.1 in chapter 1."
See equation~\ref{eq:emcsq} in chapter~\ref{chap:intro}.

% Or both: "See equation 1.1 in chapter 1 ``Introduction''."
See equation~\ref{eq:emcsq} in chapter~\ref{chap:intro} ``\nameref{chap:intro}''.


\end{document}

The ~ before \ref is to prevent a line break just at this position.

  • Thanks for your answer, however, I think this doesn't really my answer. I would like to use \chapterref{label_of_some_equation} and not ...label_of_some_chapter. This is because if I reorganize things, I won't be able to refer to the right chapter anymore. Thanks! – brian Mar 17 '11 at 19:56
  • @brian: The reason why \pageref works is that \label writes the page number to the .aux file. But the name or number of the chapter is not written. However, this could be added. – Martin Scharrer Mar 18 '11 at 0:50
  • Do you know how to add this? Thanks a lot! – brian Mar 18 '11 at 15:58
  • @brian: I do not know any package which does this already. The \label command must be changed to also write the chapter information into the .aux file which then can be used by \chapterref. – Martin Scharrer Mar 18 '11 at 16:04
2

You can solve this using the package nameref (which you don't have to load, if you use hyperref:

\documentclass{article}
\usepackage{nameref}
\usepackage[english]{babel} 
\begin{document}

\section{Testsection}

\begin{equation}
x=y 
\label{eq:test}
\end{equation}

See equation  \ref{eq:test}  in  section \nameref{eq:test}.

\end{document}

enter image description here

EDIT: I just noted, that if you also have a \subsection{text} then \namerefwould refer to this rather than the section. If I find the solution to this, I'll supplement it.

  • Thanks for your answer. I would love it if you could find a way to fix it. – brian Mar 17 '11 at 19:57
  • @brian: I had a look at the documentation of nameref and I suspect that this problem would be solvable. However, my knowlege of this LaTeX level is basic at best. So, I would recommend asking a new question like How to modify nameref to always refer to the current chapter? or something like that. Sorry, that I can't help you further. – meep.meep Mar 18 '11 at 10:41
1

You can use labels to sections and subsections, then refer to them like you'd refer to any other float, see below:

\section{Introduction}
\subsection{Background}
\label{sec:bg}

tesagahgshlgasghagö as seen in \ref{sec:bg} ......
  • This does not seem to address the original question, which asks about extracting the chapter reference from the item (eg, equation) within the chapter – Lev Bishop Mar 17 '11 at 3:32
1

I can see two somewhat manual ways to do it:
1) Introducing an additional counter, that passes the number of the chapter containing the equation:

% in preamble
\newcounter{einst-chap}

% around the equation
E=mc^2 \label{einst} \setcounter{einst-chap}{\value{chapter}}

% and to reference
See equation \ref{einst} in chapter \arabic{einst-chap}...

However, you have to take care of the format (eg. you'd have to type \Roman{einst-chap} if you had chapter I, chapter II etc.), which makes this solution not fully automatic.

2) Storing the adequate value of \thechapter (which includes the format as well as possible superdivisions, eg. chapter A#3), using \edef:

% just after the equation
\edef\einstChap{\thechapter}

% and to reference
See equation \ref{einst} in chapter \einstChap...
1

You can also use a package called cleveref. It automatically recognise what you are referring to and adds the appropriate name and numbering style.

in the previous example (by Daniel Els) you use it like this.

\documentclass{book}
\usepackage{cleveref}

\begin{document}
\mainmatter
\chapter{First chapter}\label{chp:Chp1}
\cref{eq:Einst} in \cref{chp:Chp1} and \cref{eq:Feyn} in 
\cref{chp:App1} it is ...
\begin{equation}
   E=mc^2 \label{eq:Einst}
\end{equation}

\appendix
\chapter{First Appendix}\label{chp:App1}
\begin{equation}
  e^{i\pi}-1 = 0 \label{eq:Feyn}
\end{equation}

\end{document}
  • This requires two labels: One for the chapter and the one for the equation -- this might get tedious – user31729 Jan 20 '18 at 18:10
0

The refstyle package has this functionality. It will correctly reference an Appendix also if your label is such. You can also link it to varioref. Please read the documentaion.

\documentclass{book}
\usepackage[nokeyprefix]{refstyle}

\begin{document}
\mainmatter
\chapter{First chapter}\label{chp:Chp1}
\Eqref{eq:Einst} in \chapref{chp:Chp1} and \eqref{eq:Feyn} in \chapref{chp:App1} it is ...
\begin{equation}
  E=mc^2 \label{eq:Einst}
\end{equation}

\appendix
\chapter{First Appendix}\label{chp:App1}
\begin{equation}
  e^{i\pi}-1 = 0 \label{eq:Feyn}
\end{equation}

\end{document}

This gives:

enter image description here

  • This does not seem to address the original question, which asks about extracting the chapter reference from the item (eg, equation) within the chapter – Lev Bishop Mar 17 '11 at 3:32
  • This answer does actually address the question in a different way. It is was I was looking for anyway, thanks. – Gigili Aug 30 '16 at 9:57
-1

Put these commands in the preamble:

\newcommand{\chap}[1]{\hyperref[#1]{Chapter \ref{#1}}}
\newcommand{\Chap}[1]{\hyperref[#1]{Chapter \ref{#1}}, \nameref{#1}}
\newcommand{\chaps}[2]{\hyperref[#1]{Chapters \ref{#1} \& \ref{#2}}}

then the text looks something like

In \chap{C4} we proved that... and in \chaps{C2}{C1} we claimed that...

etc...

  • @user1999 This seems an odd approach to take. hyperref defines its own \autoref command that effectively does this. (See my answer) – Seamus Mar 16 '11 at 15:50
  • This does not seem to address the original question, which asks about extracting the chapter reference from the item (eg, equation) within the chapter – Lev Bishop Mar 17 '11 at 3:31

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.