7

I am writing a book with multiple chapters (in different sections, if that is relevant). Chapters are numbered. I want a way to transform the chapter name into the chapter number, someplace else in my text. That is, I want to say "See Chapter 4, which covers magic in depth". The chapter that is responsible for covering magic in depth may change over the course of the editing process: I want the chapter number to change with it automatically. I don't want to refer to the name alone. I am using LaTeX's autonumbering for chapters. Is this possible?

\documentclass[12pt,a4paper]{book}
\usepackage[latin1]{inputenc}
\begin{document}
\chapter{Sample 1}
    This is covered in more detail in Chapter X.
\chapter{Sample 2}
\end{document}
  • Package nameref and \nameref{chapterlabel} should work, where chapterlabel is something you stated right after \chapter{Magic} , say e.g. \label{magicchapter}. But your request is a little confusing since there seem to be wanted two different behaviours of the reference? – user31729 May 17 '14 at 13:49
  • 1
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 May 17 '14 at 13:50
  • @ChristianHupfer I think \nameref*{CurrentSection::Title::<something>} and printing the section counter along with it, is what he's looking for. Similar to how you determined the CurrentSection counter at the Index for the request I made here tex.stackexchange.com/questions/175054/… – 1010011010 May 17 '14 at 14:09
  • @1010011010: Perhaps it is similar, perhaps the answer I gave to your question is useful too. – user31729 May 17 '14 at 14:14
  • @1010011010 , your answer is something I would prefer not to get into: my knowledge of LaTeX is self taught, rather limited, and does not extend to many of the commands used in your version. If there is a simpler way to solve my problem, one that doesn't require pages of code, that would be lovely. I will write it if I have to, but I am hoping that there is a simpler method. – Keller Scholl May 17 '14 at 17:00
7

LaTeX provides this cross-referencing scheme out-of-the-box. Place \label{<label>} after a counter that can be referenced (like a chapter, section, subsection, theorem, ...) and use it elsewhere as \ref{<label>}. This will only return the counter number and nothing else:

enter image description here

\documentclass[openany]{book}
\usepackage[paper=a6paper]{geometry}% Just for this example
\usepackage{xparse,nameref}
\NewDocumentCommand{\chapref}{s m}{Chapter~\ref{#2}\IfBooleanF{#1}{ \nameref{#2}}}
\begin{document}
\chapter{Sample 1}\label{ch:first}
This is covered in more detail in \chapref*{ch:second}. Also see~\ref{ch:second}.

\chapter{Sample 2}\label{ch:second}
This is covered in more detail in \chapref{ch:first}. Also see~\ref{ch:first}.
\end{document}

The above example sets the \labels after every \chapter. They're referenced using \ref{<label>}, which clearly just returns the chapter number. However, with the addition of nameref we can grab hold of the chapter title as well, referencing it as \nameref{<label>}. A combination of \ref and \nameref is incorporated in what I defined as \chapref, which translates to Chapter~\ref{<label>} \nameref{<label>}. The latter \nameref is conditional on whether you call \chapref with/without a star *.

The above example can be made fully-functional with hyperref, providing inter-document hyperlinking.

The option [openany] was just so the two chapters fit on subsequent page and is not needed in your document.

  • Werner, in my thesis I have separated latex files that representing the chapters. How can I refer the chapters in this case? – Gilson Jan 7 '15 at 16:02
  • @Gilson: You have to place the \labels inside the files after the \chapter command. – Werner Jan 7 '15 at 16:54
  • I did it, but couldn't work – Gilson Jan 7 '15 at 17:10
  • @Gilson: That doesn't help much. – Werner Jan 7 '15 at 17:28
2

As an alternative to Werner's code, you could also use one of the packages which set this up for you automatically. fancyref and cleveref are two which spring to mind. For example:

\documentclass[openany]{book}
\usepackage[paper=a6paper]{geometry}% Just for this example
\usepackage{fancyref}
\begin{document}
\chapter{Sample 1}\label{chap:first}
This is covered in more detail in \fref{chap:second}. Also see~\ref{chap:second}. \Fref{chap:second} covers this in excruciating detail.

\chapter{Sample 2}\label{chap:second}
This is covered in more detail in \fref{chap:first}. Also see~\ref{chap:first}. \Fref{chap:first} covers this in more detail.
\end{document}

References using <code>fancyref</code>

Note that chapter is added automatically if the chap prefix is used in the label, and that \Fref and \fref produce capitalised and non-capitalised output. (I believe that cleveref automatises things further but I've not used it myself.)

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.