# How do I include the book/part name in the cross-references (if cross-book/part)?

My document structure is basically a collection of other smaller documents. I decided not to go down the xr-hyper road and having individual PDF documents, because the requirement in the end is a single file anyway. So, instead, I decided to create one big LaTeX document with my sub-documents as books, e.g.:

\documentclass{memoir}
\renewcommand*{\thebook}{\Alph{book}} % "Book A", "Book B", etc.
\begin{document}
\book{maindoc}
\chapter{maindoc first chapter}
\label{chap:maindoc-first}
\chapter{maindoc second chapter}
\label{chap:maindoc-second}

\setcounter{chapter}{0} % reset chapter counter with each book
As shown in \ref{chap:maindoc-first}, and further research in \ref{chap:extradoc-first} ...

\book{anotherdoc}
\setcounter{chapter}{0}
\chapter{anotherdoc chapter}
\end{document}


Now this As shown in \ref{chap:maindoc-first} will render as if the chapter was in the scope of the current part. What I'd like to accomplish is something like:

• As shown in chapter 1 of the maindoc and chapter 1...
• As shown in chapter 1 (maindoc) and chapter 1 ...
• As shown in (1, maindoc) and (1) ...
• As shown in A-1 and 1 ...

However, as most references are within the documents themselves, I don't want to prefix all of the references as in this Q&A.

How can I accomplish this? Is there a way to determine the scope of the reference and include the prefix conditionally?

Ultimately, I'd like to do the same for references to theorems, figures, tables, etc.

Since you are using memoir, you should make use of the cross-referencing facilities provided by the class. These are covered in chapter 16 of the user manual.

memoir does, however, have some fairly definite ideas about formatting and so does not always provide much flexibility beyond those ideas. In this case, it thinks that cross-references to other parts of the document should be capitalised. If you prefer to avoid this, you need to provide alternatives.

The following are based on the versions provided by memoir:

\aref% based on \Aref
\bref% \Bref
\cref% \Cref
\sref% \Sref


and the lower-cased names are provided by:

\lcbookrefname% based on \bookrefname
\lcchapterrefname% \chapterrefname
\lcsectionrefname% identical to \sectionrefname
\lcappendixrefname% based on \appendixrefname


This allows you to set references as desired when combined with the \titleref command provided by memoir.

To automate this, a \chapref{} command is provided which should produce references formatted according to the first possibility you listed. You can create other commands to achieve the other formats in a similar way.

Note: I suspect that the way I've coded the \chapref command, at the very least, may not be the best way to do it. Caveat emptor...

\documentclass{memoir}
\renewcommand*{\thebook}{\Alph{book}} % "Book A", "Book B", etc.
\newcommand*{\aref}[1]{\lcappendixrefname\ref{#1}}
\newcommand*{\bref}[1]{\lcbookrefname\ref{#1}}
\newcommand*{\cref}[1]{\lcchapterrefname\ref{#1}}
\newcommand*{\sref}[1]{\lcsectionrefname\ref{#1}}
\newcommand*{\lcbookrefname}{book~}
\newcommand*{\lcchapterrefname}{chapter~}
\newcommand*{\lcsectionrefname}{\S}
\newcommand*{\lcappendixrefname}{appendix~}
\makeatletter
\def\chapref@book#1:#2-#3\@nil{#2}
\newcommand*{\chapref}[1]{%
\edef\tempa{\expandafter\chapref@book#1\@nil}%
\edef\tempb{\ref{book:\tempa}}%
\edef\tempc{\thebook}%
\ifx\tempb\tempc
\cref{#1}%
\else
\cref{#1} of the \titleref{book:\tempa}%
\fi}
\makeatother
\begin{document}
\book{maindoc}
\label{book:maindoc}
\chapter{maindoc first chapter}
\label{chap:maindoc-first}
\chapter{maindoc second chapter}
\label{chap:maindoc-second}

\setcounter{chapter}{0} % reset chapter counter with each book

Manually using the facilities of the class:

As shown in \cref{chap:maindoc-first} of the \titleref{book:maindoc}, and further research in \cref{chap:extradoc-first} ...

\noindent Or using the custom command:

As shown in \chapref{chap:maindoc-first}, and further research in \chapref{chap:extradoc-first} \dots

\book{anotherdoc}
\setcounter{chapter}{0}
\chapter{anotherdoc chapter}
\end{document}